Skip to content

Commit

Permalink
add relevance scores and to search response models (#156)
Browse files Browse the repository at this point in the history
* Update vespa test files

* bump version to 1.9.7

* add hits relevance scoring

* add family relevance

* add rank features to hit response models

* bump version to 1.10.0

---------

Co-authored-by: kdutia <[email protected]>
  • Loading branch information
kdutia and kdutia authored Dec 16, 2024
1 parent cfeec88 commit 92969cc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/cpr_sdk/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ class Hit(BaseModel):
corpus_import_id: Optional[str] = None
metadata: Optional[Sequence[dict[str, str]]] = None
concepts: Optional[Sequence[Concept]] = None
relevance: Optional[float] = None
rank_features: Optional[dict[str, float]] = None

@classmethod
def from_vespa_response(cls, response_hit: dict) -> "Hit":
Expand Down Expand Up @@ -445,6 +447,8 @@ def from_vespa_response(cls, response_hit: dict) -> "Document":
corpus_import_id=fields.get("corpus_import_id"),
metadata=fields.get("metadata"),
concepts=fields.get("concepts"),
relevance=response_hit.get("relevance"),
rank_features=fields.get("summaryfeatures"),
)


Expand Down Expand Up @@ -498,6 +502,8 @@ def from_vespa_response(cls, response_hit: dict) -> "Passage":
text_block_coords=fields.get("text_block_coords"),
metadata=fields.get("metadata"),
concepts=fields.get("concepts"),
relevance=response_hit.get("relevance"),
rank_features=fields.get("summaryfeatures"),
)


Expand All @@ -509,6 +515,7 @@ class Family(BaseModel):
total_passage_hits: int = 0
continuation_token: Optional[str] = None
prev_continuation_token: Optional[str] = None
relevance: Optional[float] = None


class SearchResponse(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions src/cpr_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_MAJOR = "1"
_MINOR = "9"
_PATCH = "8"
_MINOR = "10"
_PATCH = "0"
_SUFFIX = ""

VERSION_SHORT = "{0}.{1}".format(_MAJOR, _MINOR)
Expand Down
2 changes: 2 additions & 0 deletions src/cpr_sdk/vespa.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def parse_vespa_response(vespa_response: VespaResponse) -> SearchResponse:
family_hits: List[Hit] = []
passages_continuation = dig(family, "children", 0, "continuation", "next")
prev_passages_continuation = dig(family, "children", 0, "continuation", "prev")
family_relevance = family.get("relevance")
for hit in dig(family, "children", 0, "children", default=[]):
family_hits.append(Hit.from_vespa_response(response_hit=hit))
families.append(
Expand All @@ -140,6 +141,7 @@ def parse_vespa_response(vespa_response: VespaResponse) -> SearchResponse:
total_passage_hits=total_passage_hits,
continuation_token=passages_continuation,
prev_continuation_token=prev_passages_continuation,
relevance=family_relevance,
)
)

Expand Down
21 changes: 21 additions & 0 deletions tests/test_search_adaptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ def test_vespa_search_adaptor__works(test_vespa):
assert total_passage_count == response.total_hits


@pytest.mark.vespa
def test_vespa_search_adaptor_relevance_scoring(test_vespa):
request = SearchParameters(query_string="the")
response = vespa_search(test_vespa, request)

for family in response.families:
assert isinstance(family.relevance, float)
for hit in family.hits:
assert isinstance(hit.relevance, float)


@pytest.mark.vespa
def test_vespa_search_adaptor_rank_features(test_vespa):
request = SearchParameters(query_string="the")
response = vespa_search(test_vespa, request)

for family in response.families:
for hit in family.hits:
assert isinstance(hit.rank_features, dict)


@pytest.mark.vespa
def test_vespa_search_adaptor__exact_search(test_vespa):
"""Test that exact search works"""
Expand Down

0 comments on commit 92969cc

Please sign in to comment.