Skip to content

Commit

Permalink
Feature/pla 216 surface concept filtering changes in the backend (#370)
Browse files Browse the repository at this point in the history
* Integrating the concept filtering from the sdk and surfacing in the backend.

* Typo.

* Updating the browse functionality.

* refactoring.

* Adding test pypi source.

* Updating poetry.lock with temp sdk version.

* Reverting to non-test-pypi

* Updating the cpr_sdk version.

* Updating the search type functionality.

* Change enum import.

* Resolving attribute access bug.

* testing new patch import path.

* Moving identification of search type for code simplicity.

* Update test_vespa_concept_filters.py test_concept_filters to remove label parameter.

* Adding new patch path.

* Downgrade the version.

---------

Co-authored-by: Mark <[email protected]>
  • Loading branch information
THOR300 and Mark authored Oct 14, 2024
1 parent 141713b commit 3c21e17
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 239 deletions.
37 changes: 34 additions & 3 deletions app/api/api_v1/routers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import logging
from enum import Enum
from io import BytesIO
from typing import Annotated, Optional, Sequence, cast

Expand Down Expand Up @@ -50,12 +51,42 @@
search_router = APIRouter()


def _search_request(db: Session, search_body: SearchRequestBody) -> SearchResponse:
is_browse_request = not search_body.query_string
if is_browse_request:
class SearchType(str, Enum):
standard = "standard"
browse = "browse"
browse_with_concepts = "browse_with_concepts"


def identify_search_type(search_body: SearchRequestBody) -> str:
"""Identify the search type from parameters"""
if not search_body.query_string and not search_body.concept_filters:
return SearchType.browse
elif not search_body.query_string and search_body.concept_filters:
return SearchType.browse_with_concepts
else:
return SearchType.standard


def mutate_search_body_for_search_type(
search_body: SearchRequestBody,
) -> SearchRequestBody:
"""Mutate the search body in line with the search params"""
search_type = identify_search_type(search_body=search_body)
if search_type == SearchType.browse:
search_body.all_results = True
search_body.documents_only = True
search_body.exact_match = False
elif search_type == SearchType.browse_with_concepts:
search_body.all_results = True
search_body.documents_only = False
search_body.exact_match = False
return search_body


def _search_request(db: Session, search_body: SearchRequestBody) -> SearchResponse:
"""Perform a search request against the Vespa search engine"""
search_body = mutate_search_body_for_search_type(search_body=search_body)

try:
cpr_sdk_search_params = create_vespa_search_params(db, search_body)
cpr_sdk_search_response = _VESPA_CONNECTION.search(
Expand Down
2 changes: 2 additions & 0 deletions app/api/api_v1/schemas/search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from enum import Enum
from typing import List, Literal, Mapping, Optional, Sequence

from cpr_sdk.models.search import Concept
from cpr_sdk.models.search import SearchParameters as CprSdkSearchParameters
from db_client.models.dfce import FamilyCategory
from pydantic import (
Expand Down Expand Up @@ -144,6 +145,7 @@ class SearchResponseDocumentPassage(BaseModel):
text_block_id: str
text_block_page: Optional[int] = None
text_block_coords: Optional[Sequence[Coord]] = None
concepts: Optional[Sequence[Concept]] = None


class SearchResponseFamilyDocument(BaseModel):
Expand Down
1 change: 1 addition & 0 deletions app/core/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ def _process_vespa_search_response_families(
text_block_id=hit.text_block_id,
text_block_page=hit.text_block_page,
text_block_coords=hit.text_block_coords,
concepts=hit.concepts,
)
)

Expand Down
Loading

0 comments on commit 3c21e17

Please sign in to comment.