def index(es, annotation, request, target_index=None): """ Index an annotation into the search index. A new annotation document will be created in the search index or, if the index already contains an annotation document with the same ID as the given annotation then it will be updated. :param es: the Elasticsearch client object to use :type es: h.search.Client :param annotation: the annotation to index :type annotation: h.models.Annotation :param target_index: the index name, uses default index if not given :type target_index: unicode """ presenter = presenters.AnnotationSearchIndexPresenter(annotation, request) annotation_dict = presenter.asdict() event = AnnotationTransformEvent(request, annotation, annotation_dict) request.registry.notify(event) if target_index is None: target_index = es.index es.conn.index( index=target_index, doc_type=es.mapping_type, body=annotation_dict, id=annotation_dict["id"], )
def test_index_allows_to_set_op_type(self, db_session, es, pyramid_request, streaming_bulk, factories): indexer = index.BatchIndexer(db_session, es, pyramid_request, op_type='create') annotation = factories.Annotation() db_session.add(annotation) db_session.flush() results = [] def fake_streaming_bulk(*args, **kwargs): ann = list(args[1])[0] callback = kwargs.get('expand_action_callback') results.append(callback(ann)) return set() streaming_bulk.side_effect = fake_streaming_bulk indexer.index() rendered = presenters.AnnotationSearchIndexPresenter( annotation).asdict() rendered['target'][0]['scope'] = [annotation.target_uri_normalized] assert results[0] == ({ 'create': { '_type': indexer.es_client.t.annotation, '_index': 'hypothesis', '_id': annotation.id } }, rendered)
def _prepare(self, annotation): action = {self.op_type: {'_index': self._target_index, '_type': self.es_client.t.annotation, '_id': annotation.id}} data = presenters.AnnotationSearchIndexPresenter(annotation).asdict() event = AnnotationTransformEvent(self.request, annotation, data) self.request.registry.notify(event) return (action, data)
def _prepare(self, annotation): action = { self.op_type: { "_index": self._target_index, "_type": self.es_client.mapping_type, "_id": annotation.id, } } data = presenters.AnnotationSearchIndexPresenter( annotation, self.request).asdict() return action, data
def _prepare(self, annotation): action = { self.op_type: { "_index": self._target_index, "_type": self.es_client.mapping_type, "_id": annotation.id, } } data = presenters.AnnotationSearchIndexPresenter( annotation, self.request).asdict() event = AnnotationTransformEvent(self.request, annotation, data) self.request.registry.notify(event) return (action, data)
def test_index_emits_AnnotationTransformEvent_when_presenting_bulk_actions(self, db_session, indexer, pyramid_request, streaming_bulk, pyramid_config, factories): annotation = factories.Annotation() results = [] def fake_streaming_bulk(*args, **kwargs): ann = list(args[1])[0] callback = kwargs.get('expand_action_callback') results.append(callback(ann)) return set() streaming_bulk.side_effect = fake_streaming_bulk def transform(event): if event.annotation == annotation: data = event.annotation_dict data['transformed'] = True pyramid_config.add_subscriber(transform, 'h.events.AnnotationTransformEvent') indexer.index() rendered = presenters.AnnotationSearchIndexPresenter(annotation).asdict() rendered['transformed'] = True rendered['target'][0]['scope'] = [annotation.target_uri_normalized] assert results[0] == ( {'index': {'_type': indexer.es_client.t.annotation, '_index': 'hypothesis', '_id': annotation.id}}, rendered )