Skip to content

Commit

Permalink
short circuit exists query on select objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Bek committed Nov 11, 2020
1 parent b3813f3 commit 0c22a37
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/snovault/elasticsearch/searches/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@
X = 'x'
Y = 'y'
YES = 'yes'
SHORT_CIRCUITS = [
'embedded.files',
'embedded.files.quality_metrics',
'embedded.replicate']
5 changes: 5 additions & 0 deletions src/snovault/elasticsearch/searches/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
from .interfaces import X
from .interfaces import Y
from .interfaces import YES
from .interfaces import SHORT_CIRCUITS


class AbstractQueryFactory:
Expand Down Expand Up @@ -535,6 +536,8 @@ def _make_must_equal_terms_queries_from_params(self, params):
)

def _make_field_must_exist_query(self, field, **kwargs):
if field in SHORT_CIRCUITS:
field = field + '.@id'
return Q(
EXISTS,
field=field
Expand Down Expand Up @@ -588,6 +591,8 @@ def _make_terms_aggregation(self, field, **kwargs):
)

def _make_exists_aggregation(self, field, **kwargs):
if field in SHORT_CIRCUITS:
field = field + '.@id'
return A(
FILTERS,
filters={
Expand Down
35 changes: 35 additions & 0 deletions src/snovault/tests/test_searches_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,18 @@ def test_searches_queries_abstract_query_factory_make_field_must_exist_query(par
}
}

def test_searches_queries_abstract_query_factory_make_field_must_exist_query_with_wildcard(params_parser):
from snovault.elasticsearch.searches.queries import AbstractQueryFactory
aq = AbstractQueryFactory(params_parser)
meq = aq._make_field_must_exist_query(
field='embedded.files'
)
assert meq.to_dict() == {
'exists': {
'field': 'embedded.files.@id'
}
}


def test_searches_queries_abstract_query_factory_make_field_must_exist_queries_from_params(params_parser):
from snovault.elasticsearch.searches.queries import AbstractQueryFactory
Expand Down Expand Up @@ -1798,6 +1810,29 @@ def test_searches_queries_abstract_query_factory_make_exists_aggregation(params_
}
}

def test_searches_queries_abstract_query_factory_make_exists_aggregation_with_wildcard(params_parser):
from snovault.elasticsearch.searches.queries import AbstractQueryFactory
aq = AbstractQueryFactory(params_parser)
eq = aq._make_exists_aggregation(
field='embedded.files'
)
assert eq.to_dict() == {
'filters': {
'filters': {
'no': {
'bool': {
'must_not': [
{'exists': {'field': 'embedded.files.@id'}}
]
}
},
'yes': {
'exists': {'field': 'embedded.files.@id'}
}
}
}
}


def test_searches_queries_abstract_query_factory_map_param_to_elasticsearch_field():
from snovault.elasticsearch.searches.queries import AbstractQueryFactory
Expand Down

0 comments on commit 0c22a37

Please sign in to comment.