Skip to content
Snippets Groups Projects
Commit 3ffb4dab authored by Megan Henning's avatar Megan Henning
Browse files

Reorganize code pieces in de

parent ed8e33f1
No related branches found
No related tags found
No related merge requests found
...@@ -103,7 +103,7 @@ endpoints = [ ...@@ -103,7 +103,7 @@ endpoints = [
route('/dataexplorer/search', DataExplorerHandler, h='search', m=['POST']), route('/dataexplorer/search', DataExplorerHandler, h='search', m=['POST']),
route('/dataexplorer/facets', DataExplorerHandler, h='get_facets', m=['POST']), route('/dataexplorer/facets', DataExplorerHandler, h='get_facets', m=['POST']),
route('/dataexplorer/search/fields', DataExplorerHandler, h='search_fields', m=['POST']), route('/dataexplorer/search/fields', DataExplorerHandler, h='search_fields', m=['POST']),
route('/dataexplorer/search/fields/aggregate', DataExplorerHandler, h='custom_field_values', m=['POST']), route('/dataexplorer/search/fields/aggregate', DataExplorerHandler, h='aggregate_field_values', m=['POST']),
route('/dataexplorer/index/fields', DataExplorerHandler, h='index_field_names', m=['POST']), route('/dataexplorer/index/fields', DataExplorerHandler, h='index_field_names', m=['POST']),
# Users # Users
......
...@@ -9,6 +9,78 @@ from ..auth import require_login, require_superuser ...@@ -9,6 +9,78 @@ from ..auth import require_login, require_superuser
log = config.log log = config.log
"""
EXAMPLE_SESSION_QUERY = {
"size": 0,
"query": {
"match": {
"_all": "test'"
}
},
"aggs": {
"by_session": {
"terms": {
"field": "session._id",
"size": 100
},
"aggs": {
"by_top_hit": {
"top_hits": {
"size": 1
}
}
}
}
}
}
EXAMPLE_ACQUISITION_QUERY = {
"size": 0,
"query": {
"match": {
"_all": "megan'"
}
},
"aggs": {
"by_session": {
"terms": {
"field": "acquisition._id",
"size": 100
},
"aggs": {
"by_top_hit": {
"top_hits": {
"size": 1
}
}
}
}
}
}
EXAMPLE_FILE_QUERY = {
"size": 100,
"query": {
"bool": {
"must": {
"match": {
"_all": "brain"
}
},
"filter": {
"bool" : {
"must" : [
{ "term" : {"file.type" : "dicom"}},
{ "term" : {"container_type" : "file"}}
]
}
}
}
}
}
"""
ANALYSIS = { ANALYSIS = {
"analyzer": { "analyzer": {
"my_analyzer": { "my_analyzer": {
...@@ -150,75 +222,6 @@ FACET_QUERY = { ...@@ -150,75 +222,6 @@ FACET_QUERY = {
} }
} }
EXAMPLE_SESSION_QUERY = {
"size": 0,
"query": {
"match": {
"_all": "test'"
}
},
"aggs": {
"by_session": {
"terms": {
"field": "session._id",
"size": 100
},
"aggs": {
"by_top_hit": {
"top_hits": {
"size": 1
}
}
}
}
}
}
EXAMPLE_ACQUISITION_QUERY = {
"size": 0,
"query": {
"match": {
"_all": "megan'"
}
},
"aggs": {
"by_session": {
"terms": {
"field": "acquisition._id",
"size": 100
},
"aggs": {
"by_top_hit": {
"top_hits": {
"size": 1
}
}
}
}
}
}
EXAMPLE_FILE_QUERY = {
"size": 100,
"query": {
"bool": {
"must": {
"match": {
"_all": "brain"
}
},
"filter": {
"bool" : {
"must" : [
{ "term" : {"file.type" : "dicom"}},
{ "term" : {"container_type" : "file"}}
]
}
}
}
}
}
class DataExplorerHandler(base.RequestHandler): class DataExplorerHandler(base.RequestHandler):
# pylint: disable=broad-except # pylint: disable=broad-except
...@@ -230,7 +233,7 @@ class DataExplorerHandler(base.RequestHandler): ...@@ -230,7 +233,7 @@ class DataExplorerHandler(base.RequestHandler):
try: try:
request = self.request.json_body request = self.request.json_body
except KeyError: except (ValueError):
if request_type == 'search': if request_type == 'search':
self.abort(400, 'Must specify return type') self.abort(400, 'Must specify return type')
return None, None, None return None, None, None
...@@ -265,85 +268,74 @@ class DataExplorerHandler(base.RequestHandler): ...@@ -265,85 +268,74 @@ class DataExplorerHandler(base.RequestHandler):
return return_type, modified_filters, search_string return return_type, modified_filters, search_string
@require_login @require_login
def custom_field_values(self): def aggregate_field_values(self):
""" """
Return list of type ahead values for a key given a value Return list of type ahead values for a key given a value
that the user has already started to type in for the value of that the user has already started to type in for the value of
a custom string field or a set of statistics if the field type is a custom string field or a set of statistics if the field type is
a number. a number.
""" """
try:
custom_field = self.request.json_body['field_name'] field_name = self.request.json_body['field_name']
except (KeyError, ValueError):
self.abort(400, 'Field name is required')
filters = [{'term': {'permissions._id': self.uid}}] filters = [{'term': {'permissions._id': self.uid}}]
field = config.es.indices.get_field_mapping(custom_field, try:
index='data_explorer', field = config.es.get(index='data_explorer_fields', id=field_name, doc_type='flywheel_field')
doc_type='flywheel')['data_explorer']['mappings']['flywheel'][custom_field] except TransportError as e:
field_type = self._get_field_type(field['mapping'][custom_field.split('.')[-1]]['type']) log.warning(e)
self.abort(404, 'Could not find mapping for field {}.'.format(field_name))
field_type = field['_source']['type']
search_string = self.request.json_body.get('search_string', None)
# If the field type is a string, return a list of type-ahead values # If the field type is a string, return a list of type-ahead values
if field_type == 'string': body = {
user_value = self.request.json_body['value'] "size": 0,
body = { "query": {
"size": 0, "bool": {
"query": { "must" : {
"bool": { "match" : { field_name : search_string}
"must" : { },
"match" : { custom_field : user_value} "filter" : filters
},
"filter" : filters
}
},
"aggs" : {
"results" : {
"terms" : {
"field" : custom_field + ".raw",
"size" : 15
}
}
} }
} }
}
if not filters:
body['query']['bool'].pop('filter')
if search_string is None:
body['query']['bool']['must'] = MATCH_ALL
if not filters: if field_type in ['string', 'boolean']:
body['query']['bool'].pop('filter') body['aggs'] = {
"results" : {
aggs = config.es.search( "terms" : {
index='data_explorer', "field" : field_name + ".raw",
doc_type='flywheel', "size" : 15
body=body }
)['aggregations']['results']['buckets'] }
aggs = [bucket['key'] for bucket in aggs] }
return {'type_aheads': aggs}
# If it is a number (int, date, or some other type), return various statistics on the values of the field # If it is a number (int, date, or some other type), return various statistics on the values of the field
else: elif field_type in ['integer', 'float', 'date']:
body = { body['aggs'] = {
"size": 0, "results" : {
"query": { "stats" : {
"bool": { "field" : field_name
"must" : {
"match_all" : {}
},
"filter" : filters
}
},
"aggs" : {
"results" : {
"stats" : {
"field" : custom_field
}
} }
} }
} }
if not filters: else:
body['query']['bool'].pop('filter') self.abort(400, 'Aggregations are only allowed on string, integer, float, data and boolean fields.')
aggs = config.es.search( aggs = config.es.search(
index='data_explorer', index='data_explorer',
doc_type='flywheel', doc_type='flywheel',
body=body body=body
)['aggregations']['results'] )['aggregations']['results']
return aggs return aggs
@require_login @require_login
def get_facets(self): def get_facets(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment