feat: add text_match option to HybridSearchConfig for OR keyword matching - #339
Open
ankurjindal (ecomlisters) wants to merge 1 commit into
Open
ankurjindal (ecomlisters) wants to merge 1 commit into
ankurjindal (ecomlisters) wants to merge 1 commit into
Conversation
…hing Hybrid search builds its keyword query with plainto_tsquery, which ANDs every word, so natural-language queries often get no keyword matches and hybrid search silently becomes vector-only. Add HybridSearchConfig.text_match. "all" (default) keeps plainto_tsquery. "any" ORs the lexemes of to_tsvector(<tsv_lang>, :fts_query); each lexeme is quoted and cast directly to tsquery so it is never parsed as tsquery syntax. Other values raise ValueError. Document text_match in the how-to notebook and correct the stated tsv_column default.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hybrid search builds its keyword query with
plainto_tsquery, which ANDs every word. Natural-language questions rarely contain every word of the passage that answers them, so the keyword half often returns no rows and hybrid search quietly becomes vector-only.This adds
HybridSearchConfig.text_match:"all"(default): unchanged, usesplainto_tsquery."any": matches documents containing at least one query term, ranked byts_rank_cdas before.Any other value raises
ValueErrorwhen the config is created.For
"any", the terms come fromto_tsvector(<tsv_lang>, :fts_query), so stemming and stop words behave exactly as they do for"all". Each term is quoted and cast directly totsquery. The query stays a bound parameter. The terms are not passed throughto_tsquery, because that re-parses them as query syntax: a query containing a URL likeexample.com/a:b?x=1would raisesyntax error in tsquery.The GIN index on the TSV column is still used. On a 50,000-row table with
aapply_hybrid_search_index(),EXPLAIN ANALYZEof the generated query shows a Bitmap Index Scan on the GIN index for both custom and generic plans. The term list is computed once per query (InitPlan), not per row.Docs: the hybrid search section of
examples/pg_vectorstore_how_to.ipynbdocumentstext_matchand corrects the statedtsv_columndefault (it is"";init_vectorstore_tablenames a new column<content_column>_tsv). No execution counts or outputs changed.Tests
tests/unit_tests/v2/test_async_pg_vectorstore_search.py:"all"returns no keyword matches,"any"returns the document (with and without a TSV column)."how is the") does not error."third-party vendor's"does not error and matches.& ! | ( ) ') do not error with"any".plainto_tsqueryreturns.tests/unit_tests/v2/test_hybrid_search_config.py:"all"and"any"are accepted; other values raiseValueError.make test: 767 passed.make lintis clean.Note: this conflicts with #338 on one line, the
from typing import ...import intests/unit_tests/v2/test_async_pg_vectorstore_search.py. Whichever merges second needs the union of both imports.