跪拜 Guibai
← All articles
Backend · Python · Database

Milvus Hybrid Search: BM25 Keyword Recall, Dense Vectors, and Cross-Encoder Reranking in One Schema

By Dragon_xjy ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Milvus now handles the entire retrieval stack natively, from embedding and BM25 indexing to Cross-Encoder rerank, so a small app gets hybrid search with a few schema functions instead of wiring external services together.

Summary

Milvus's Function API turns a knowledge base into a retrieval pipeline without explicit embedding calls. A schema declares two functions on one content field: TEXTEMBEDDING sends text to DashScope's text-embedding-v3 for a 1024-dim dense vector, and BM25 produces a sparse keyword vector locally, no external model involved. Both vectors are generated automatically at insert and query time, so application code only ever writes the raw content and scalar fields.

A hybrid_search call makes two AnnSearchRequests, one per vector field, each recalling fetch_k candidates. A RERANK function backed by a Cross-Encoder model (qwen3.7-text-rerank) reads the query against candidate content and returns the top_k results. The full walkthrough covers deploying Milvus on a BaoTa server with Docker Compose, Attu admin setup, credential configuration, and PyMilvus code that enables Chinese tokenization on the content field and caps rerank candidates at 5 per batch.

Takeaways
— TEXTEMBEDDING generates dense vectors via an external embedding model (DashScope text-embedding-v3, 1024 dims); BM25 generates sparse vectors inside Milvus with no external model.
— A schema function declares both vector fields, so inserts only send raw content and scalar fields.
— hybrid_search passes two AnnSearchRequests (dense and sparse), each recalling fetch_k candidates, then a RERANK Cross-Encoder (qwen3.7-text-rerank) reorders the merged candidates and returns top_k.
— The content field uses enable_analyzer with type "chinese" for Chinese tokenization; VARCHAR max_length counts bytes.
— IDs are derived from the low 63 bits of a UUID because Milvus INT64 primary keys are signed integers.
— max_client_batch_size controls how many candidates the rerank model processes per batch.
— Dense vectors use a COSINE AUTOINDEX; BM25 sparse vectors use SPARSE_INVERTED_INDEX with metric_type BM25.
— Attu admin runs on port 3000 and the application connection on 19530; both should only be open to trusted IPs.
Conclusions

Having Milvus generate both vector types from one content field removes the classic two-embedding-pipeline maintenance burden; the database owns indexing and recall.

The reranker runs at query time on candidate text, which is the practical difference from a bi-encoder: relevance is recomputed jointly rather than pre-computed into vectors, buying precision at the cost of latency.

The example targets a specific Chinese deployment path (BaoTa server plus DashScope credentials), so it is a ready reference for Milvus users on Alibaba Cloud tooling but less portable to other embedding providers without rework.

Concepts & terms
BM25
A keyword scoring function that ranks documents by term frequency and inverse document frequency. In Milvus it runs locally to produce a sparse vector representation of the text, requiring no external model.
Cross-Encoder
A model that reads the query and a candidate document together and outputs a joint relevance score. Used here as the RERANK function to reorder candidates after initial hybrid recall.
Sparse vector
A vector with mostly zero values that encodes the presence of keywords rather than semantic density, making it the natural representation for BM25-style matching.
TEXTEMBEDDING
A Milvus schema function that calls an external embedding provider, such as DashScope's text-embedding-v3, to convert stored text into a dense vector automatically at write and query time.
From the discussion
Featured comments
用户566234544033

Looking for an independent developer to build a custom TikTok women's fashion AI material pipeline: needs 3D fingerprint deduplication (image + text + shot timing), ComfyUI batch image generation, data feedback and scoring loop, deliverable source code — not a Codex node package. Anyone who has done similar projects, DM me with case studies.

Dragon_xjy

[striking emoji]

用户566234544033  → Dragon_xjy

?

用户46219025342

Also waiting for cost-effective generation APIs~ If there's a later need for batch video material production, the MiniMax H3 API is worth referencing: cloud-based calls, built-in stereo sound, reference billing around 0.3 yuan/minute, suitable for running at scale. Features and pricing subject to official announcements.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗