Simplified ElasticSearch connection
This commit is contained in:
parent
f6cf8d29f0
commit
dcd67b6114
@ -1,7 +1,9 @@
|
|||||||
import logging
|
|
||||||
|
|
||||||
from .routes import blueprint_search
|
from .routes import blueprint_search
|
||||||
|
from . import queries
|
||||||
|
|
||||||
def setup_app(app, url_prefix: str =None):
|
|
||||||
|
def setup_app(app, url_prefix: str = None):
|
||||||
app.register_api_blueprint(
|
app.register_api_blueprint(
|
||||||
blueprint_search, url_prefix=url_prefix)
|
blueprint_search, url_prefix=url_prefix)
|
||||||
|
|
||||||
|
queries.setup_app(app)
|
||||||
|
@ -4,30 +4,13 @@ import logging
|
|||||||
from elasticsearch import Elasticsearch
|
from elasticsearch import Elasticsearch
|
||||||
from elasticsearch_dsl import Search, Q
|
from elasticsearch_dsl import Search, Q
|
||||||
|
|
||||||
from pillar import current_app
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
node_agg_terms = ['node_type', 'media', 'tags', 'is_free']
|
node_agg_terms = ['node_type', 'media', 'tags', 'is_free']
|
||||||
user_agg_terms = ['roles', ]
|
user_agg_terms = ['roles', ]
|
||||||
|
|
||||||
|
# Will be set in setup_app()
|
||||||
class TheELKClient():
|
client: Elasticsearch = None
|
||||||
"""
|
|
||||||
Elastic-client singleton.
|
|
||||||
|
|
||||||
`current_app` is not available when on import
|
|
||||||
"""
|
|
||||||
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):
|
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)
|
must.append(bool_query)
|
||||||
bool_query = Q('bool', must=must)
|
bool_query = Q('bool', must=must)
|
||||||
|
|
||||||
search = Search(using=elk.get_client())
|
search = Search(using=client)
|
||||||
search.query = bool_query
|
search.query = bool_query
|
||||||
|
|
||||||
return search
|
return search
|
||||||
@ -147,7 +130,7 @@ def do_user_search_admin(query: str) -> dict:
|
|||||||
Q('match', full_name=query),
|
Q('match', full_name=query),
|
||||||
]
|
]
|
||||||
bool_query = Q('bool', should=should)
|
bool_query = Q('bool', should=should)
|
||||||
search = Search(using=elk.get_client())
|
search = Search(using=client)
|
||||||
search.query = bool_query
|
search.query = bool_query
|
||||||
|
|
||||||
if log.isEnabledFor(logging.DEBUG):
|
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))
|
log.debug(json.dumps(response.to_dict(), indent=4))
|
||||||
|
|
||||||
return response.to_dict()
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user