Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions pele/lib/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def get_page_size_and_offset(r):
class QueryES:
"""Class for querying ES backend."""

# Parameters for handling closed indices
CLOSED_INDEX_PARAMS = {
"ignore_unavailable": True,
"allow_no_indices": True,
"expand_wildcards": "open"
}

def __init__(self, es_client, Search, Q, A): # noqa
"""
:param es_client: the object returned from Elasticsearch(...)
Expand All @@ -61,6 +68,10 @@ def __init__(self, es_client, Search, Q, A): # noqa
self.Q = Q
self.A = A

def _create_search(self, index):
"""Create a Search object with closed index handling parameters."""
return self.Search(using=self.client, index=index).params(**self.CLOSED_INDEX_PARAMS)

def query_types(self, index, offset, page_size):
"""Return list of dataset types:
{
Expand All @@ -79,7 +90,7 @@ def query_types(self, index, offset, page_size):
}
"""

s = self.Search(using=self.client, index=index).extra(size=0)
s = self._create_search(index).extra(size=0)
a = self.A('terms', field='dataset_type.keyword', size=MAX_SIZE)
s.aggs.bucket('types', a)

Expand All @@ -106,7 +117,7 @@ def query_datasets(self, index, offset, page_size):
}
"""

s = self.Search(using=self.client, index=index).extra(size=0)
s = self._create_search(index).extra(size=0)
a = self.A('terms', field='dataset.keyword', size=MAX_SIZE)
s.aggs.bucket('datasets', a)

Expand Down Expand Up @@ -135,7 +146,7 @@ def query_datasets_by_type(self, index, dataset_type, offset, page_size):
}
"""

s = self.Search(using=self.client, index=index).extra(size=0)
s = self._create_search(index).extra(size=0)
q = self.Q('term', dataset_type__keyword=dataset_type)
a = self.A('terms', field='dataset.keyword', size=MAX_SIZE)
s = s.query(q)
Expand Down Expand Up @@ -166,7 +177,7 @@ def query_types_by_dataset(self, index, dataset, offset, page_size):
}
"""

s = self.Search(using=self.client, index=index).extra(size=0)
s = self._create_search(index).extra(size=0)
q = self.Q('term', dataset__keyword=dataset)
a = self.A('terms', field='dataset_type.keyword', size=MAX_SIZE)
s = s.query(q)
Expand Down Expand Up @@ -200,7 +211,7 @@ def query_ids_by_dataset(self, index, dataset, offset, page_size, start_time=Non
:return: Elasticsearch document
"""

s = self.Search(using=self.client, index=index).query(self.Q('term', dataset__keyword=dataset))
s = self._create_search(index).query(self.Q('term', dataset__keyword=dataset))
if start_time is not None:
s = s.query('range', **{'starttime': {'gte': start_time}})
if end_time is not None:
Expand Down Expand Up @@ -242,7 +253,7 @@ def query_ids_by_type(self, index, dataset_type, offset, page_size, start_time=N
:return: Elasticsearch document
"""

s = self.Search(using=self.client, index=index).query(self.Q('term', dataset_type__keyword=dataset_type))
s = self._create_search(index).query(self.Q('term', dataset_type__keyword=dataset_type))
if start_time is not None:
s = s.query('range', **{'starttime': {'gte': start_time}})
if end_time is not None:
Expand Down Expand Up @@ -275,7 +286,7 @@ def query_id(self, index, _id):
]
}
"""
s = self.Search(using=self.client, index=index).query(self.Q('term', _id=_id))
s = self._create_search(index).query(self.Q('term', _id=_id))
current_app.logger.debug(s.to_dict())
resp = s.execute()
return resp[0].to_dict() if s.count() > 0 else None
Expand Down Expand Up @@ -324,7 +335,7 @@ def query_fields(self, index, terms, fields, offset, page_size, start_time=None,
else:
q += self.Q('term', **{f: val})

s = self.Search(using=self.client, index=index).query(q)
s = self._create_search(index).query(q)
if start_time is not None:
s = s.query('range', **{'starttime': {'gte': start_time}})
if end_time is not None:
Expand Down Expand Up @@ -452,7 +463,7 @@ def overlaps(self, index, _id, terms, fields, offset, page_size):
f = self.Q('geo_shape', **{'location': {'shape': location}})

# search
s = self.Search(using=self.client, index=index)
s = self._create_search(index)
if t is not None:
s = s.query(t)
if q != self.Q():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pele',
version='1.3.1',
version='1.4.0',
long_description='REST API for HySDS Datasets',
packages=find_packages(),
include_package_data=True,
Expand Down