Simplified ElasticSearch connection

This commit is contained in:
Sybren A. Stüvel 2018-01-05 15:24:57 +01:00
parent f6cf8d29f0
commit dcd67b6114
2 changed files with 17 additions and 24 deletions

View File

@ -1,7 +1,9 @@
import logging
from .routes import blueprint_search
from . import queries
def setup_app(app, url_prefix: str = None):
app.register_api_blueprint(
blueprint_search, url_prefix=url_prefix)
queries.setup_app(app)

View File

@ -4,31 +4,14 @@ import logging
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
from pillar import current_app
log = logging.getLogger(__name__)
node_agg_terms = ['node_type', 'media', 'tags', 'is_free']
user_agg_terms = ['roles', ]
class TheELKClient():
"""
Elastic-client singleton.
`current_app` is not available when on import
"""
# Will be set in setup_app()
client: Elasticsearch = None
def get_client(self):
if not self.client:
self.client = Elasticsearch(
current_app.config['ELASTIC_SEARCH_HOSTS'])
else:
return self.client
elk = TheELKClient()
def add_aggs_to_search(search, agg_terms):
"""
@ -59,7 +42,7 @@ def nested_bool(must: list, should: list, terms: dict) -> Search:
must.append(bool_query)
bool_query = Q('bool', must=must)
search = Search(using=elk.get_client())
search = Search(using=client)
search.query = bool_query
return search
@ -147,7 +130,7 @@ def do_user_search_admin(query: str) -> dict:
Q('match', full_name=query),
]
bool_query = Q('bool', should=should)
search = Search(using=elk.get_client())
search = Search(using=client)
search.query = bool_query
if log.isEnabledFor(logging.DEBUG):
@ -159,3 +142,11 @@ def do_user_search_admin(query: str) -> dict:
log.debug(json.dumps(response.to_dict(), indent=4))
return response.to_dict()
def setup_app(app):
global client
hosts = app.config['ELASTIC_SEARCH_HOSTS']
log.getChild('setup_app').info('Creating ElasticSearch client for %s', hosts)
client = Elasticsearch(hosts)