Skip to content

Commit

Permalink
Change unstructured vespa score modifier expression (#880)
Browse files Browse the repository at this point in the history
Unstructured Indexes: This expression is a fix for the `0` bug in `multiply_score_by` in score modifiers wherein when a score modifier doesn't exist in a document but is specified in a query, the score becomes 0.
  • Loading branch information
RaynorChavez authored Jun 25, 2024
1 parent aaab5f3 commit 3d7a02d
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ def _generate_unstructured_schema(cls, marqo_index: UnstructuredMarqoIndex) -> s
dimension = str(marqo_index.model.get_dimension())

_score_modifier_expression = (
f'if (count(query(marqo__mult_weights)) == 0, 1, '
f'reduce(query(marqo__mult_weights) '
f'* attribute(marqo__score_modifiers), prod)) * score '
f'+ reduce(query(marqo__add_weights) '
f'* attribute(marqo__score_modifiers), sum)'
f'if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, '
f' 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) '
f'* score '
f'+ reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)'
)

return textwrap.dedent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ schema marqo__test_00unstructured_00schema {
query(marqo__add_weights) tensor<double>(p{})
}
function modify(score) {
expression: if (count(query(marqo__mult_weights)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
expression: if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
}

rank-profile bm25_modifiers inherits modifiers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ schema marqo__test_00unstructured_00schema_00distance_00metric {
query(marqo__add_weights) tensor<double>(p{})
}
function modify(score) {
expression: if (count(query(marqo__mult_weights)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
expression: if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ schema marqo__test_00unstructured_00schema_00distance_00metric {
query(marqo__add_weights) tensor<double>(p{})
}
function modify(score) {
expression: if (count(query(marqo__mult_weights)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
expression: if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ schema marqo__test_00unstructured_00schema_00distance_00metric {
query(marqo__add_weights) tensor<double>(p{})
}
function modify(score) {
expression: if (count(query(marqo__mult_weights)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
expression: if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ schema marqo__test_00unstructured_00schema_00distance_00metric {
query(marqo__add_weights) tensor<double>(p{})
}
function modify(score) {
expression: if (count(query(marqo__mult_weights)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
expression: if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ schema marqo__test_00unstructured_00schema_00distance_00metric {
query(marqo__add_weights) tensor<double>(p{})
}
function modify(score) {
expression: if (count(query(marqo__mult_weights)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
expression: if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ schema marqo__test_00unstructured_00schema_00distance_00metric {
query(marqo__add_weights) tensor<double>(p{})
}
function modify(score) {
expression: if (count(query(marqo__mult_weights)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
expression: if (count(query(marqo__mult_weights) * attribute(marqo__score_modifiers)) == 0, 1, reduce(query(marqo__mult_weights) * attribute(marqo__score_modifiers), prod)) * score + reduce(query(marqo__add_weights) * attribute(marqo__score_modifiers), sum)
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/tensor_search/integ_tests/test_dict_score_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from unittest import mock

from marqo.api.models.update_documents import UpdateDocumentsBodyParams
from marqo.core.exceptions import UnsupportedFeatureError
from marqo.core.models.marqo_index import *
from marqo.core.models.marqo_index_request import FieldRequest
from marqo.core.structured_vespa_index.structured_vespa_index import StructuredVespaIndex
Expand Down Expand Up @@ -216,7 +215,9 @@ def test_multiply_score_by_map_score_modifier(self):
# Search with score modifier
# 0.5 * 0.5 * 4 = 1 (1 and 7)
score_modifier = ScoreModifier(
**{"multiply_score_by": [{"field_name": "map_score_mods.a", "weight": 4}]})
**{"multiply_score_by": [{"field_name": "map_score_mods.a", "weight": 4},
{"field_name": "map_score_mods.d", "weight": 4}]}) # Nonexistent field.
# Nonexistent field should not zero out the whole score
res = tensor_search.search(
index_name=index.name, config=self.config, text="",
score_modifiers=score_modifier,
Expand Down

0 comments on commit 3d7a02d

Please sign in to comment.