improve elastic server settings

This commit is contained in:
2018-01-05 10:56:41 +01:00
parent 85d6f76000
commit de8c6a8b63
2 changed files with 42 additions and 6 deletions

View File

@@ -4,15 +4,28 @@ from elasticsearch_dsl import Search, Q
import logging
from pillar import current_app
client = Elasticsearch()
log = logging.getLogger(__name__)
node_agg_terms = ['node_type', 'media', 'tags', 'is_free']
user_agg_terms = ['roles', ]
class TheELKClient():
"""
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):
"""
Add facets / aggregations to the search result
@@ -42,7 +55,7 @@ def nested_bool(must: list, should: list, terms: dict) -> Search:
must.append(bool_query)
bool_query = Q('bool', must=must)
search = Search(using=client)
search = Search(using=elk.get_client())
search.query = bool_query
return search
@@ -129,7 +142,7 @@ def do_user_search_admin(query: str) -> dict:
Q('match', full_name=query),
]
bool_query = Q('bool', should=should)
search = Search(using=client)
search = Search(using=elk.get_client())
search.query = bool_query
if log.isEnabledFor(logging.DEBUG):

View File

@@ -80,7 +80,7 @@ ALGOLIA_INDEX_NODES = 'dev_Nodes'
SEARCH_BACKENDS = ('algolia', 'elastic') # search backend we use
ELASTIC_SEARCH_HOSTS = ['elasticsearch'] # elasticsearch hosts
ELASTIC_INDICES = {
# elasticsearch indexes
'NODE': 'nodes',
@@ -88,6 +88,29 @@ ELASTIC_INDICES = {
}
def get_docker_host():
"""
Looks for the DOCKER_HOST environment variable to find the VM
running docker-machine.
If the environment variable is not found, it is assumed that
you're running docker on localhost.
"""
d_host = os.getenv('DOCKER_HOST', None)
if d_host:
if re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', d_host):
return d_host
return re.match(r'tcp://(.*?):\d+', d_host).group(1)
return 'localhost'
# Elasticsearch hosts
ELASTIC_SEARCH_HOSTS = ["{}:{}".format(
os.getenv('ELASTICSEARCH_PORT_9200_TCP_ADDR', get_docker_host()),
os.getenv('ELASTICSEARCH_PORT_9200_TCP_PORT', 9200))]
ZENCODER_API_KEY = '-SECRET-'
ZENCODER_NOTIFICATIONS_SECRET = '-SECRET-'
ZENCODER_NOTIFICATIONS_URL = 'http://zencoderfetcher/'