(wip) udpate elasticsearch indexes after modifying parent relation (related #13967211)

authorKatia Saurfelt <katia.saurfelt@logilab.fr>
changeset21a0272363b4
branchdefault
phasedraft
hiddenyes
parent revision#9fca73587475 [pkg] use beautifulsoup4 instead of bs4
child revision<not specified>
files modified by this revision
hooks.py
test/test_parents.py
# HG changeset patch
# User Katia Saurfelt <katia.saurfelt@logilab.fr>
# Date 1485359871 -3600
# Wed Jan 25 16:57:51 2017 +0100
# Node ID 21a0272363b41f21f4ac62a7f9c79bc87b7e458e
# Parent 9fca73587475373e91dc7f5ecd8142c3b3246573
(wip) udpate elasticsearch indexes after modifying parent relation (related #13967211)

diff --git a/hooks.py b/hooks.py
@@ -47,17 +47,37 @@
1          if self.entity.cw_etype == 'File':
2              return  # FIXME hack!
3          IndexEsOperation.get_instance(self._cw).add_data(self.entity)
4 
5 
6 +class RelationsUpdateIndexES(hook.Hook):
7 +
8 +    """detect relations changes and updates ES indexing"""
9 +
10 +    __regid__ = 'elasticsearch.relationsupdatetoes'
11 +    events = ('after_add_relation', 'before_delete_relation')
12 +    category = 'es'
13 +
14 +    def __call__(self):
15 +        # XXX add a selector for object and subject
16 +        for entity in (self._cw.entity_from_eid(self.eidfrom),
17 +                       self._cw.entity_from_eid(self.eidto)):
18 +            cw_etype = entity.cw_etype
19 +            if cw_etype == 'File':
20 +                return  # FIXME hack!
21 +            if (cw_etype in indexable_types(entity._cw.vreg.schema) or
22 +                    cw_etype in CUSTOM_ATTRIBUTES):
23 +                IndexEsOperation.get_instance(self._cw).add_data(entity)
24 +
25 +
26  class IndexEsOperation(hook.DataOperationMixIn, hook.Operation):
27 
28      def precommit_event(self):
29          indexer = self.cnx.vreg['es'].select('indexer', self.cnx)
30          es = indexer.get_connection()
31          if es is None:
32 -            log.info('no connection to ES (not configured) skip ES indexing')
33 +            log.error('no connection to ES (not configured) skip ES indexing')
34              return
35          for entity in self.get_data():
36              rql = fulltext_indexable_rql(entity.cw_etype,
37                                           entity._cw.vreg.schema,
38                                           eid=entity.eid)
diff --git a/test/test_parents.py b/test/test_parents.py
@@ -87,10 +87,36 @@
39                                          fuzzy=True)
40                  self.assertEquals(len(search.execute()), number_of_results)
41                  self.assertEquals(search.execute().to_dict()['hits']['hits'][0]['_source']['title'],
42                                    first_result)
43 
44 +    def test_es_hooks_modify_relation(self):
45 +        with self.admin_access.cnx() as cnx:
46 +            with self.temporary_appobjects(BlogFTIAdapter):
47 +                indexer = cnx.vreg['es'].select('indexer', cnx)
48 +                indexer.get_connection()
49 +                indexer.create_index(custom_settings={
50 +                    'mappings': {
51 +                        'BlogEntry': {'_parent': {"type": "Blog"}},
52 +                    }
53 +                })
54 +                blog1 = cnx.create_entity('Blog', title=u'Blog')
55 +                entity = cnx.create_entity('BlogEntry',
56 +                                           title=u'Article about stuff',
57 +                                           content=u'content herer',
58 +                                           entry_of=blog1)
59 +                blog2 = cnx.create_entity('Blog', title=u'Blog')
60 +                cnx.commit()
61 +                time.sleep(2)  # TODO find a way to have synchronous operations in unittests
62 +
63 +                blog2.cw_set(reverse_entry_of=entity)
64 +                cnx.commit()
65 +                search = Search(index=self.config['index-name'],
66 +                                doc_type='Blog').query(
67 +                                    "match", eid=entity.eid)
68 +                print(search.execute().to_dict()['hits']['hits'])
69 +
70      def tearDown(self):
71          with self.admin_access.cnx() as cnx:
72              indexer = cnx.vreg['es'].select('indexer', cnx)
73              es = indexer.get_connection()
74              es.indices.delete(self.config['index-name'])