Skip to content

Commit

Permalink
Merge pull request #575 from nationalarchives/1751-faceted-search
Browse files Browse the repository at this point in the history
1751 - Faceted Search
  • Loading branch information
CristinaRO authored Apr 8, 2024
2 parents cea8b14 + 32ebbf5 commit a9d9739
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog 1.0.0].

## Unreleased

- Remove fcl_ex_id_ prefix from UUID of reparse execution ID
- Implement handling of facets received from MarkLogic search results

## [Release 22.1.0]

- Add an `enriched_recently` property
- Add a `validates_against_schema` property
- Add a `can_enrich` property
- Only enrich if not recently enriched and valid against current schema
- Allow fetching linked docuements for `Judgement`s and `PressSummary`s
- Allow fetching linked documents for `Judgement`s and `PressSummary`s
- Add function to check if the docx exists for a judgment

## [Release 22.0.2]
Expand Down
17 changes: 17 additions & 0 deletions src/caselawclient/responses/search_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,20 @@ def results(self) -> List[SearchResult]:
"//search:response/search:result", namespaces=self.NAMESPACES
)
return [SearchResult(result, self.client) for result in results]

@property
def facets(self) -> dict[str, str]:
"""
Returns search facets from the SearchResponse as a dictionary
:return: A flattened dictionary of search facet values
"""
# TODO: preserve the name of the facet (e.g. "court", "year")
results = self.node.xpath(
"//search:response/search:facet/search:facet-value",
namespaces={"search": "http://marklogic.com/appservices/search"},
)
facets_dictionary = {
result.attrib["name"]: result.attrib["count"] for result in results
}
return facets_dictionary
15 changes: 14 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def valid_search_result_xml_fixture() -> str:
)


@pytest.fixture(name="valid_facets_fixture_xml")
def valid_facets_xml_fixture() -> str:
return (
'<search:facet name="court" type="xs:string">'
'<search:facet-value name="" count="14"/>'
'<search:facet-value name=" UKUT-AAC" count="1"> UKUT-AAC</search:facet-value>'
'<search:facet-value name="EAT" count="649">EAT</search:facet-value>'
'<search:facet-value name="EWCA-Civil" count="5768">EWCA-Civil</search:facet-value>'
"</search:facet>"
)


@pytest.fixture(name="generate_search_response_xml")
def generate_search_response_xml_fixture() -> Callable:
"""
Expand All @@ -43,7 +55,7 @@ def generate_search_response_xml_fixture() -> Callable:
Callable: Function that generates search response XML.
"""

def _generate_search_response_xml(response_content: str) -> str:
def _generate_search_response_xml(response_content: str, facets="") -> str:
"""
Generate a search response XML string.
Expand All @@ -56,6 +68,7 @@ def _generate_search_response_xml(response_content: str) -> str:
return (
'<search:response xmlns:search="http://marklogic.com/appservices/search" total="2">' # noqa: E501
f"{response_content}"
f"{facets}"
"</search:response>"
)

Expand Down
29 changes: 29 additions & 0 deletions tests/responses/test_search_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,32 @@ def test_when_search_namespace_prefix_not_defined_on_response_xml_syntax_error_r
match="Namespace prefix search on response is not defined",
):
SearchResponse(etree.fromstring(xml_without_namespace), self.client)

def test_facets(
self,
valid_search_result_xml,
valid_facets_fixture_xml,
generate_search_response_xml,
):
"""
Given a SearchResponse instance with n facets
When calling 'facets' on it
Then it should return a list of n elements
And each element's node attribute should be as expected
"""
search_response = SearchResponse(
etree.fromstring(
generate_search_response_xml(
valid_search_result_xml, valid_facets_fixture_xml
)
),
self.client,
)

results = search_response.facets

assert len(results) == 4
assert results[""] == "14"
assert results[" UKUT-AAC"] == "1"
assert results["EAT"] == "649"
assert results["EWCA-Civil"] == "5768"

0 comments on commit a9d9739

Please sign in to comment.