caselawclient.responses.search_response
1from typing import List 2 3from lxml import etree 4 5from caselawclient.Client import MarklogicApiClient 6from caselawclient.responses.search_result import SearchResult 7 8 9class SearchResponse: 10 """ 11 Represents a search response obtained from XML data. 12 """ 13 14 NAMESPACES = {"search": "http://marklogic.com/appservices/search"} 15 """ Namespaces used in XPath expressions.""" 16 17 def __init__(self, node: etree._Element, client: MarklogicApiClient) -> None: 18 """ 19 Initializes a SearchResponse instance from an xml node. 20 21 :param node: The XML data as an etree element 22 """ 23 self.node = node 24 self.client = client 25 26 @property 27 def total(self) -> int: 28 """ 29 The total number of search results. 30 31 :return: The total number of search results 32 """ 33 return int( 34 self.node.xpath("//search:response/@total", namespaces=self.NAMESPACES)[0], 35 ) 36 37 @property 38 def results(self) -> List[SearchResult]: 39 """ 40 Converts the SearchResponse to a list of SearchResult objects. 41 42 :return: The list of search results 43 """ 44 results = self.node.xpath( 45 "//search:response/search:result", 46 namespaces=self.NAMESPACES, 47 ) 48 return [SearchResult(result, self.client) for result in results] 49 50 @property 51 def facets(self) -> dict[str, str]: 52 """ 53 Returns search facets from the SearchResponse as a dictionary 54 55 :return: A flattened dictionary of search facet values 56 """ 57 # TODO: preserve the name of the facet (e.g. "court", "year") 58 results = self.node.xpath( 59 "//search:response/search:facet/search:facet-value", 60 namespaces={"search": "http://marklogic.com/appservices/search"}, 61 ) 62 facets_dictionary = {result.attrib["name"]: result.attrib["count"] for result in results} 63 return facets_dictionary
class
SearchResponse:
10class SearchResponse: 11 """ 12 Represents a search response obtained from XML data. 13 """ 14 15 NAMESPACES = {"search": "http://marklogic.com/appservices/search"} 16 """ Namespaces used in XPath expressions.""" 17 18 def __init__(self, node: etree._Element, client: MarklogicApiClient) -> None: 19 """ 20 Initializes a SearchResponse instance from an xml node. 21 22 :param node: The XML data as an etree element 23 """ 24 self.node = node 25 self.client = client 26 27 @property 28 def total(self) -> int: 29 """ 30 The total number of search results. 31 32 :return: The total number of search results 33 """ 34 return int( 35 self.node.xpath("//search:response/@total", namespaces=self.NAMESPACES)[0], 36 ) 37 38 @property 39 def results(self) -> List[SearchResult]: 40 """ 41 Converts the SearchResponse to a list of SearchResult objects. 42 43 :return: The list of search results 44 """ 45 results = self.node.xpath( 46 "//search:response/search:result", 47 namespaces=self.NAMESPACES, 48 ) 49 return [SearchResult(result, self.client) for result in results] 50 51 @property 52 def facets(self) -> dict[str, str]: 53 """ 54 Returns search facets from the SearchResponse as a dictionary 55 56 :return: A flattened dictionary of search facet values 57 """ 58 # TODO: preserve the name of the facet (e.g. "court", "year") 59 results = self.node.xpath( 60 "//search:response/search:facet/search:facet-value", 61 namespaces={"search": "http://marklogic.com/appservices/search"}, 62 ) 63 facets_dictionary = {result.attrib["name"]: result.attrib["count"] for result in results} 64 return facets_dictionary
Represents a search response obtained from XML data.
SearchResponse( node: lxml.etree._Element, client: caselawclient.Client.MarklogicApiClient)
18 def __init__(self, node: etree._Element, client: MarklogicApiClient) -> None: 19 """ 20 Initializes a SearchResponse instance from an xml node. 21 22 :param node: The XML data as an etree element 23 """ 24 self.node = node 25 self.client = client
Initializes a SearchResponse instance from an xml node.
Parameters
- node: The XML data as an etree element
NAMESPACES =
{'search': 'http://marklogic.com/appservices/search'}
Namespaces used in XPath expressions.
total: int
27 @property 28 def total(self) -> int: 29 """ 30 The total number of search results. 31 32 :return: The total number of search results 33 """ 34 return int( 35 self.node.xpath("//search:response/@total", namespaces=self.NAMESPACES)[0], 36 )
The total number of search results.
Returns
The total number of search results
results: List[caselawclient.responses.search_result.SearchResult]
38 @property 39 def results(self) -> List[SearchResult]: 40 """ 41 Converts the SearchResponse to a list of SearchResult objects. 42 43 :return: The list of search results 44 """ 45 results = self.node.xpath( 46 "//search:response/search:result", 47 namespaces=self.NAMESPACES, 48 ) 49 return [SearchResult(result, self.client) for result in results]
Converts the SearchResponse to a list of SearchResult objects.
Returns
The list of search results
facets: dict[str, str]
51 @property 52 def facets(self) -> dict[str, str]: 53 """ 54 Returns search facets from the SearchResponse as a dictionary 55 56 :return: A flattened dictionary of search facet values 57 """ 58 # TODO: preserve the name of the facet (e.g. "court", "year") 59 results = self.node.xpath( 60 "//search:response/search:facet/search:facet-value", 61 namespaces={"search": "http://marklogic.com/appservices/search"}, 62 ) 63 facets_dictionary = {result.attrib["name"]: result.attrib["count"] for result in results} 64 return facets_dictionary
Returns search facets from the SearchResponse as a dictionary
Returns
A flattened dictionary of search facet values