From 9cc2b3b4f7d68021b4ea245c34209d78f17eb1fb Mon Sep 17 00:00:00 2001 From: Renzo Frigato Date: Tue, 23 Feb 2016 13:08:27 -0800 Subject: [PATCH 1/2] create proxy route to elasticsearch closes #168 --- api/api.py | 3 +++ api/config.py | 3 +++ api/handlers/searchhandler.py | 31 +++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 38 insertions(+) create mode 100644 api/handlers/searchhandler.py diff --git a/api/api.py b/api/api.py index ac06191e5..760227b02 100644 --- a/api/api.py +++ b/api/api.py @@ -16,6 +16,7 @@ from handlers import grouphandler from handlers import containerhandler from handlers import collectionshandler +from handlers import searchhandler log = config.log @@ -130,6 +131,8 @@ def _format(route): webapp2.Route(_format(r'/api///'), containerhandler.ContainerHandler, name='cont_sublist_groups', handler_method='get_all', methods=['GET']), webapp2.Route(_format(r'/api///'), containerhandler.ContainerHandler, name='cont_sublist', handler_method='get_all', methods=['GET']), + webapp2.Route(_format(r'/api/search'), searchhandler.SearchHandler, name='es_proxy', methods=['GET']), + webapp2.Route(_format(r'/api/search/'), searchhandler.SearchHandler, name='es_proxy', methods=['GET']), ] diff --git a/api/config.py b/api/config.py index a3b41e7ed..1168e9b2a 100644 --- a/api/config.py +++ b/api/config.py @@ -47,6 +47,9 @@ 'db_server_selection_timeout': '3000', 'data_path': os.path.join(os.path.dirname(__file__), '../persistent/data'), }, + 'elasticsearch': { + 'es_endpoint': 'flywheel-elasticsearch:9200' + } } __config = copy.deepcopy(DEFAULT_CONFIG) diff --git a/api/handlers/searchhandler.py b/api/handlers/searchhandler.py new file mode 100644 index 000000000..723396046 --- /dev/null +++ b/api/handlers/searchhandler.py @@ -0,0 +1,31 @@ +import datetime +import elasticsearch +from requests import ConnectionError + +from .. import base +from .. import config + +log = config.log + +es_client = elasticsearch.Elasticsearch([config.get_item('elasticsearch', 'es_endpoint')]) + + +class SearchHandler(base.RequestHandler): + + def __init__(self, request=None, response=None): + super(SearchHandler, self).__init__(request, response) + + def get(self, cont_name=None, **kwargs): + if self.public_request: + self.abort(403, 'search is available only for authenticated users') + size = self.get_param('size') + body = self.request.json_body if self.request.body else { + 'query': { + 'match_all': {} + } + } + try: + results = es_client.search(index='scitran', doc_type=cont_name, body=body, _source=['_id'], size=size or 10) + except elasticsearch.exceptions.ConnectionError as e: + self.abort(503, 'elasticsearch is not available') + return results['hits']['hits'] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 118cbf008..45af167cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ requests==2.9.1 rfc3987==1.3.4 webapp2==2.5.2 WebOb==1.5.1 +elasticsearch>=1.0.0,<2.0.0 From d8bc8311045fca77e8cb9018b11068968c159e69 Mon Sep 17 00:00:00 2001 From: Renzo Frigato Date: Wed, 24 Feb 2016 17:35:13 -0800 Subject: [PATCH 2/2] address pull request comments PR #170 --- api/api.py | 2 +- api/config.py | 5 +---- api/handlers/searchhandler.py | 10 +++------- requirements.txt | 2 +- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/api/api.py b/api/api.py index 760227b02..0f092b145 100644 --- a/api/api.py +++ b/api/api.py @@ -131,7 +131,7 @@ def _format(route): webapp2.Route(_format(r'/api///'), containerhandler.ContainerHandler, name='cont_sublist_groups', handler_method='get_all', methods=['GET']), webapp2.Route(_format(r'/api///'), containerhandler.ContainerHandler, name='cont_sublist', handler_method='get_all', methods=['GET']), - webapp2.Route(_format(r'/api/search'), searchhandler.SearchHandler, name='es_proxy', methods=['GET']), + webapp2.Route(_format(r'/api/search'), searchhandler.SearchHandler, name='es_proxy', methods=['GET']), webapp2.Route(_format(r'/api/search/'), searchhandler.SearchHandler, name='es_proxy', methods=['GET']), ] diff --git a/api/config.py b/api/config.py index 1168e9b2a..1d7c6cca8 100644 --- a/api/config.py +++ b/api/config.py @@ -39,16 +39,13 @@ 'id_endpoint': 'https://www.googleapis.com/plus/v1/people/me/openIdConnect', 'auth_endpoint': 'https://accounts.google.com/o/oauth2/auth', 'verify_endpoint': 'https://www.googleapis.com/oauth2/v1/tokeninfo', - }, 'persistent': { 'db_uri': 'mongodb://localhost:9001/scitran', 'db_connect_timeout': '2000', 'db_server_selection_timeout': '3000', 'data_path': os.path.join(os.path.dirname(__file__), '../persistent/data'), - }, - 'elasticsearch': { - 'es_endpoint': 'flywheel-elasticsearch:9200' + 'elasticsearch_host': 'localhost:9200', } } diff --git a/api/handlers/searchhandler.py b/api/handlers/searchhandler.py index 723396046..d3923cb80 100644 --- a/api/handlers/searchhandler.py +++ b/api/handlers/searchhandler.py @@ -7,7 +7,7 @@ log = config.log -es_client = elasticsearch.Elasticsearch([config.get_item('elasticsearch', 'es_endpoint')]) +es_client = elasticsearch.Elasticsearch([config.get_item('persistent', 'elasticsearch_host')]) class SearchHandler(base.RequestHandler): @@ -19,13 +19,9 @@ def get(self, cont_name=None, **kwargs): if self.public_request: self.abort(403, 'search is available only for authenticated users') size = self.get_param('size') - body = self.request.json_body if self.request.body else { - 'query': { - 'match_all': {} - } - } + body = self.request.json_body try: results = es_client.search(index='scitran', doc_type=cont_name, body=body, _source=['_id'], size=size or 10) except elasticsearch.exceptions.ConnectionError as e: self.abort(503, 'elasticsearch is not available') - return results['hits']['hits'] \ No newline at end of file + return results['hits']['hits'] diff --git a/requirements.txt b/requirements.txt index 45af167cd..f9e4d2332 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,4 @@ requests==2.9.1 rfc3987==1.3.4 webapp2==2.5.2 WebOb==1.5.1 -elasticsearch>=1.0.0,<2.0.0 +elasticsearch==1.9.0