Fix: correct multi-search non-adjacent queries (#13504)

This commit is contained in:
shamoon
2026-08-03 15:03:03 +00:00
committed by GitHub
parent 68bd8f8f63
commit cfa1d3b058
2 changed files with 110 additions and 36 deletions
+57 -11
View File
@@ -163,10 +163,55 @@ class TestSearch:
assert (
len(backend.search_ids("sswo", user=None, search_mode=SearchMode.TEXT)) == 1
)
assert (
len(backend.search_ids("sswo re", user=None, search_mode=SearchMode.TEXT))
== 1
for query in ["sswo re", "re sswo"]:
assert (
len(backend.search_ids(query, user=None, search_mode=SearchMode.TEXT))
== 1
), query
def test_text_mode_matches_all_terms_without_requiring_adjacency(
self,
backend: TantivyBackend,
) -> None:
"""Simple text mode should match all terms in any order or field."""
doc = Document.objects.create(
title="complete-medical-history",
content="Samsung Odyssey curved monitor",
checksum="TXT13",
pk=19,
)
backend.add_or_update(doc)
for query in [
"complete history",
"history complete",
"Samsung curved",
"curved Samsung",
]:
assert backend.search_ids(
query,
user=None,
search_mode=SearchMode.TEXT,
) == [doc.pk], query
def test_text_mode_matches_terms_across_title_and_content(
self,
backend: TantivyBackend,
) -> None:
"""Each simple-search term may match either title or content."""
doc = Document.objects.create(
title="Complete record",
content="Patient history",
checksum="TXT14",
pk=20,
)
backend.add_or_update(doc)
assert backend.search_ids(
"complete history",
user=None,
search_mode=SearchMode.TEXT,
) == [doc.pk]
def test_text_mode_does_not_match_on_partial_term_overlap(
self,
@@ -186,11 +231,11 @@ class TestSearch:
== 0
)
def test_text_mode_anchors_later_query_tokens_to_token_starts(
def test_text_mode_anchors_numeric_tokens_regardless_of_query_order(
self,
backend: TantivyBackend,
) -> None:
"""Multi-token simple search should not match later tokens in the middle of a word."""
"""Numeric tokens must not match in the middle of a larger number."""
exact_doc = Document.objects.create(
title="Z-Berichte 6",
content="monthly report",
@@ -213,13 +258,14 @@ class TestSearch:
backend.add_or_update(prefix_doc)
backend.add_or_update(false_positive)
result_ids = set(
backend.search_ids("Z-Berichte 6", user=None, search_mode=SearchMode.TEXT),
)
for query in ["Z-Berichte 6", "6 Z-Berichte"]:
result_ids = set(
backend.search_ids(query, user=None, search_mode=SearchMode.TEXT),
)
assert exact_doc.id in result_ids
assert prefix_doc.id in result_ids
assert false_positive.id not in result_ids
assert exact_doc.id in result_ids, query
assert prefix_doc.id in result_ids, query
assert false_positive.id not in result_ids, query
def test_text_mode_ignores_queries_without_searchable_tokens(
self,