Skip to content
Snippets Groups Projects
Commit 4e15941f6ce1 authored by Arthur Lutz's avatar Arthur Lutz
Browse files

[hooks] initial version of hook to index content when created or changed

parent 680de326f545
No related branches found
No related tags found
No related merge requests found
......@@ -16,3 +16,43 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""cubicweb-elasticsearch specific hooks and operations"""
import logging
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import ConnectionError
from urllib3.exceptions import ProtocolError
from cubicweb.server import hook
from cubicweb.predicates import score_entity
from cubes.elasticsearch.es import indexable_types
log = logging.getLogger(__name__)
def entity_indexable(entity):
return entity.cw_etype in indexable_types(entity._cw.vreg.schema)
class ContentUpdateIndexES(hook.Hook):
"""detect content change and updates ES indexing"""
__regid__ = 'elasticsearch.contentupdatetoes'
__select__ = hook.Hook.__select__ & score_entity(entity_indexable)
events = ('after_update_entity', 'after_add_entity')
category = 'es'
def __call__(self):
locations = self._cw.vreg.config['elasticsearch-locations']
index_name = self._cw.vreg.config['index-name']
serializer = self.entity.cw_adapt_to('ISerializable')
json = serializer.serialize()
es = Elasticsearch(locations and locations.split(',') or None)
try:
# TODO option pour coté async ?
es.index(index=index_name,
doc_type=self.entity.cw_etype,
body=json)
except (ConnectionError, ProtocolError):
log.debug('Failed to index in hook, could not connect to ES')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment