Skip to content

Commit 3080562

Browse files
Fixes flaky text word weights (#416)
1 parent c72460c commit 3080562

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

tests/integration/test_query.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,10 @@ def test_text_query_word_weights(index, scorer):
896896
text_field = "description"
897897
return_fields = ["description"]
898898

899-
weights = {"medical": 3.4, "cancers": 5}
899+
weights = {
900+
"medical": 1.1,
901+
"cancers": 1.15,
902+
} # use small weights to not change results
900903

901904
# test we can run a query with text weights
902905
weighted_query = TextQuery(
@@ -905,6 +908,7 @@ def test_text_query_word_weights(index, scorer):
905908
return_fields=return_fields,
906909
text_scorer=scorer,
907910
text_weights=weights,
911+
sort_by="description",
908912
)
909913

910914
weighted_results = index.query(weighted_query)
@@ -917,14 +921,17 @@ def test_text_query_word_weights(index, scorer):
917921
return_fields=return_fields,
918922
text_scorer=scorer,
919923
text_weights={},
924+
sort_by="description",
920925
)
921926

922927
unweighted_results = index.query(unweighted_query)
923928

924929
for weighted, unweighted in zip(weighted_results, unweighted_results):
925-
for word in weights:
926-
if word in weighted["description"] or word in unweighted["description"]:
927-
assert weighted["score"] > unweighted["score"]
930+
# test that the same results are returned when small weights are used
931+
assert weighted["description"] == unweighted["description"]
932+
# test the score still changes slightly with small weights
933+
if any([word in weighted["description"] for word in weights]):
934+
assert weighted["score"] > unweighted["score"]
928935

929936
# test that weights do change the document score and order of results
930937
weights = {"medical": 5, "cancers": 3.4} # switch the weights

0 commit comments

Comments
 (0)