OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Feature] Support BM25 keyword search (search_type=keywords) on the local vector engine

Open wyl1988 opened this issue 1 month ago • 5 comments

Summary

search_type="keywords" (BM25, added in #5456) only works with the cloud VikingDB/volcengine backend. On a fully local deployment (local vector engine + llama.cpp embeddings), keyword search is always rejected:

[INVALID_ARGUMENT] Keyword search is not supported by the configured vector backend,
or full-text indexing is not enabled for the content field.

Please consider making keyword/BM25 search work for the local engine as well.

Environment

  • OpenViking server v0.4.23 (pip, local engine)
  • Embeddings: local llama.cpp (Qwen3-Embedding), rerank: local jina-reranker — no cloud services at all
  • Config: default vectordb section (local engine)

Reproduce

ov search "8482" --search-type keywords --limit 5
# -> INVALID_ARGUMENT: Keyword search is not supported by the configured vector backend...
ov find "8482"                    # semantic search works fine
ov grep "8482"                    # regex grep works fine

Root cause (from reading 0.4.23 source)

service/search_service.py -> ensure_keywords_search_supported() gates the feature on:

supported_modes=("volcengine", "vikingdb")

so the local engine is rejected at the request boundary, regardless of what the engine itself can do.

Why this matters for us

Our corpus is customs tariff / HS-code documents where users query exact codes (8482, 8471.30) or exact legal document numbers. Pure semantic recall misses exact-string matches; regex grep works but cannot rank. A local BM25 path would give self-hosted deployments exact-token recall with ranking, without requiring a cloud VikingDB subscription.

I noticed two related attempts that were closed without merge:

  • #1857 local_bm25 sparse provider (maintainers suggested a different direction)
  • #3967 local FTS5 keyword sidecar (followed the direction from #1857, still not merged)

Both show real demand from self-hosted users. Happy to test any implementation on our 27k-document corpus.

Workaround

ov grep (regex over content) for exact matches — works, but no ranking and no fusion with semantic results.

wyl1988 avatar Oct 06 '26 08:10 wyl1988

Hi @ByteDanceLiuYang @ZaynJarvis, before I start: would you accept a contributor PR that implements search_by_keywords for the local backend (search-time BM25 via an opt-in SQLite FTS5 sidecar, following the #5456 backend contract and the direction discussed in #1857)? Or is this already part of your planned retrieval/fusion work? I’d keep it small — no grep or fusion changes. Happy to share a design note first.

kathy1207 avatar Oct 30 '26 22:10 kathy1207

Thanks for offering to contribute, @kathy1207! Yes, a focused PR adding BM25 keyword search for the local backend would be welcome.

The key requirement is that the open-source/local implementation follow VikingDB's API contract. Please build on the existing search_by_keywords backend interface introduced in #5456, keeping the request parameters, filtering and pagination semantics, and result format compatible so it works with the existing search pipeline.

Keeping this scoped to keyword retrieval, with no grep or fusion changes, makes sense. Please share a short design note first, including how the proposed SQLite FTS5 index would stay in sync with data inserts, updates, and deletes.

qin-ctx avatar Nov 02 '26 08:11 qin-ctx

Thanks @qin-ctx! Here is the design note.

Goal

Implement search_by_keywords for the local backend, which currently raises NotImplementedError, so that search_type="keywords" works through the existing #5456 pipeline unchanged.

Scope

  • Changed: LocalCollection.search_by_keywords, a local SQLite FTS5 index, and enabling the local backend in ensure_keywords_search_supported.
  • Not changed: grep, fusion, the search pipeline, the public API.

Compatibility with the #5456 contract

  • Request parameters: same signature (query, keywords, mode, fields, filter, limit, offset, output_fields). It accepts what search_by_keywords_in_tenant sends: mode="bm25", fields=["content"].
  • Filtering: FTS5 finds keyword matches. Every match is then checked with the existing local filter engine, so tenant, ACL, URI-scope and level filters behave exactly as in vector search.
  • Pagination: sorted by BM25 score (higher is better), then id, so offset/limit pages are stable.
  • Result format: the same SearchResult(id, fields, score) as the other backends.

Data

The local backend currently drops content because of an old 64 KB record limit, which #5091 has since removed. It will now keep content in the local record, truncated to 1 MB as for VikingDB, so the FTS5 index has text to index.

Keeping the FTS5 index in sync

All local writes go through LocalCollection. The FTS5 index is updated in the same places, under the same lock:

Operation FTS5 index
Insert / update Replace the record's entry
Delete (including rm, mv) Remove the record's entry
TTL expiry / clear Remove the affected entries

The FTS5 index holds no original data; the text lives in the local record. If the index is ever out of sync, for example after a crash, it is rebuilt from the stored content on startup.

Open questions

  1. Is it OK for local records to store content, as VikingDB does?
  2. For Chinese text: SQLite's built-in trigram tokenizer (no new dependency), or optional jieba?

kathy1207 avatar Nov 03 '26 20:11 kathy1207

For the Chinese text case, I would recommend using jieba rather than relying on SQLite FTS5’s default tokenizer. The default FTS5 tokenizer is unicode61, which is not really suitable for Chinese text: since Chinese does not use spaces between words, a continuous Chinese sentence may be indexed as one large token instead of meaningful words. The built-in trigram tokenizer is dependency-free and better for substring matching, but it is still not real Chinese word segmentation, and it has limitations such as poor handling of two-character Chinese terms in normal FTS queries.

ByteDanceLiuYang avatar Nov 04 '26 10:11 ByteDanceLiuYang

Thanks @ByteDanceLiuYang, agreed. I'll use jieba:

  • Both indexed text and queries are segmented with jieba.cut_for_search and stored in FTS5 (unicode61) as space-separated tokens. Python's sqlite3 can't register a custom FTS5 tokenizer, so pre-segmentation is the practical route.
  • jieba goes in an optional extra (openviking[local-bm25]) and is lazy-imported, as you suggested in #1857. Without it, local keyword search fails with an install hint instead of silently using another tokenizer.
  • The tokenizer name and version are stored with the index. On a mismatch, the index is rebuilt from the stored content.

@qin-ctx one open point before I start: is it OK for local records to store content (as VikingDB does), so the FTS index can always be rebuilt from it? If so, I'll begin the implementation.

kathy1207 avatar Nov 07 '26 17:11 kathy1207