Skip to content

Commit

Permalink
feature: valueSearchFilter query param
Browse files Browse the repository at this point in the history
allow filtering `index-value-search` by value iri and type

e.g.
```
GET /api/v3/index-value-search
?valueSearchPropertyPath=rights
&valueSearchFilter[sameAs]=https%3A//creativecommons.org/publicdomain/zero/1.0/legalcode
```
```
GET /api/v3/index-value-search
?valueSearchPropertyPath=creator
&valueSearchFilter[resourceType]=Person
```
  • Loading branch information
aaxelb committed Sep 8, 2023
1 parent aef9b4d commit f6701b7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
27 changes: 19 additions & 8 deletions share/search/index_strategy/trove_indexcard_flats.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ def pls_handle_cardsearch(self, cardsearch_params: CardsearchParams) -> Cardsear

def pls_handle_valuesearch(self, valuesearch_params: ValuesearchParams) -> ValuesearchResponse:
_cursor = _SimpleCursor.from_page_param(valuesearch_params.page)
_on_date_property = is_date_property(valuesearch_params.valuesearch_property_path[-1])
try:
_es8_response = self.index_strategy.es8_client.search(
index=self.indexname,
Expand All @@ -357,7 +356,7 @@ def pls_handle_valuesearch(self, valuesearch_params: ValuesearchParams) -> Value
size=0, # ignore cardsearch hits; just want the aggs
aggs=(
self._valuesearch_date_aggs(valuesearch_params)
if _on_date_property
if is_date_property(valuesearch_params.valuesearch_property_path[-1])
else self._valuesearch_iri_aggs(valuesearch_params, _cursor)
),
)
Expand Down Expand Up @@ -495,7 +494,6 @@ def _cardsearch_aggs(self, cardsearch_params):
return _aggs

def _valuesearch_iri_aggs(self, valuesearch_params: ValuesearchParams, cursor: '_SimpleCursor'):
# TODO: valuesearch_filter_set (just rdf:type => nested_iri.value_type_iri)
_nested_iri_bool = {
'filter': [{'term': {'nested_iri.suffuniq_path_from_focus': iri_path_as_keyword(
valuesearch_params.valuesearch_property_path,
Expand All @@ -505,6 +503,23 @@ def _valuesearch_iri_aggs(self, valuesearch_params: ValuesearchParams, cursor: '
'must_not': [],
'should': [],
}
_nested_terms_agg = {
'field': 'nested_iri.iri_value',
# WARNING: terribly inefficient pagination (part one)
'size': cursor.start_index + cursor.page_size + 1,
}
_iris = list(valuesearch_params.valuesearch_iris())
if _iris:
_nested_iri_bool['filter'].append({'terms': {
'nested_iri.iri_value': _iris,
}})
_nested_terms_agg['size'] = len(_iris)
_nested_terms_agg['include'] = _iris
_type_iris = list(valuesearch_params.valuesearch_type_iris())
if _type_iris:
_nested_iri_bool['filter'].append({'terms': {
'nested_iri.value_type_iri': _type_iris,
}})
_textq_builder = self._TextQueryBuilder('nested_iri.value_namelike_text')
for _boolkey, _textquery in _textq_builder.textsegment_queries(valuesearch_params.valuesearch_textsegment_set):
_nested_iri_bool[_boolkey].append(_textquery)
Expand All @@ -516,11 +531,7 @@ def _valuesearch_iri_aggs(self, valuesearch_params: ValuesearchParams, cursor: '
'filter': {'bool': _nested_iri_bool},
'aggs': {
'iri_values': {
'terms': {
'field': 'nested_iri.iri_value',
# WARNING: terribly inefficient pagination (part one)
'size': cursor.start_index + cursor.page_size + 1,
},
'terms': _nested_terms_agg,
'aggs': {
'type_iri': {'terms': {
'field': 'nested_iri.value_type_iri',
Expand Down
14 changes: 12 additions & 2 deletions share/search/search_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
OSFMAP_VOCAB,
)
from trove.vocab.trove import trove_labeler
from trove.vocab.namespaces import RDF, TROVE
from trove.vocab.namespaces import RDF, TROVE, OWL


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -361,7 +361,7 @@ def from_queryparams(queryparams: QueryparamDict) -> 'ValuesearchParams':
return ValuesearchParams(
**CardsearchParams.parse_cardsearch_queryparams(queryparams),
valuesearch_property_path=tuple(
osfmap_labeler.iri_for_label(_pathstep)
osfmap_labeler.iri_for_label(_pathstep, default=_pathstep)
for _pathstep in split_queryparam_value(_raw_property_path)
),
valuesearch_text=_valuesearch_text,
Expand All @@ -379,6 +379,16 @@ def to_querydict(self):
_querydict.appendlist(_filter.original_param_name, _filter.original_param_value),
return _querydict

def valuesearch_iris(self):
for _filter in self.valuesearch_filter_set:
if _filter.property_path == (OWL.sameAs,) and _filter.operator == SearchFilter.FilterOperator.ANY_OF:
yield from _filter.value_set

def valuesearch_type_iris(self):
for _filter in self.valuesearch_filter_set:
if _filter.property_path == (RDF.type,) and _filter.operator == SearchFilter.FilterOperator.ANY_OF:
yield from _filter.value_set


###
# local helpers
Expand Down

0 comments on commit f6701b7

Please sign in to comment.