Files
pillar/pillar/api/search/elastic_indexing.py

66 lines
1.6 KiB
Python
Raw Normal View History

import logging
2017-11-17 18:04:29 +01:00
from elasticsearch_dsl.connections import connections
2017-12-08 14:47:04 +01:00
from elasticsearch.exceptions import NotFoundError
2017-11-17 18:04:29 +01:00
from pillar import current_app
from . import documents
2017-12-15 17:33:06 +01:00
log = logging.getLogger(__name__)
elk_hosts = current_app.config['ELASTIC_SEARCH_HOSTS']
connections.create_connection(
hosts=elk_hosts,
2017-12-15 17:57:47 +01:00
sniff_on_start=False,
timeout=20)
def push_updated_user(user_to_index: dict):
"""
2017-12-15 17:33:06 +01:00
Push an update to the Elastic index when a user item is updated.
"""
2017-12-08 14:47:04 +01:00
if not user_to_index:
return
doc = documents.create_doc_from_user_data(user_to_index)
2017-12-08 14:47:04 +01:00
if not doc:
return
index = current_app.config['ELASTIC_INDICES']['USER']
log.debug('Index %r update user doc %s in ElasticSearch.', index, doc._id)
doc.save(index=index)
def index_node_save(node_to_index: dict):
2017-12-15 17:33:06 +01:00
"""
Push an update to the Elastic index when a node item is saved.
"""
2017-12-08 13:12:39 +01:00
if not node_to_index:
return
doc = documents.create_doc_from_node_data(node_to_index)
2017-12-08 14:47:04 +01:00
if not doc:
return
index = current_app.config['ELASTIC_INDICES']['NODE']
log.debug('Index %r update node doc %s in ElasticSearch.', index, doc._id)
doc.save(index=index)
def index_node_delete(delete_id: str):
2017-12-15 17:33:06 +01:00
"""
Delete node document from Elastic index useing a node id
"""
index = current_app.config['ELASTIC_INDICES']['NODE']
log.debug('Index %r node doc delete %s', index, delete_id)
2017-12-08 14:47:04 +01:00
try:
doc: documents.Node = documents.Node.get(id=delete_id)
doc.delete(index=index)
2017-12-08 14:47:04 +01:00
except NotFoundError:
# seems to be gone already..
2017-12-08 14:47:04 +01:00
pass