def test_index_correctly_presents_bulk_actions(self, db_session, indexer, pyramid_request, streaming_bulk): annotation = self.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] == ({ 'index': { '_type': indexer.es_client.t.annotation, '_index': indexer.es_client.index, '_id': annotation.id } }, rendered)
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 index(es, annotation, request): """ 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: memex.search.Client :param annotation: the annotation to index :type annotation: memex.models.Annotation """ presenter = presenters.AnnotationSearchIndexPresenter(annotation) annotation_dict = presenter.asdict() event = AnnotationTransformEvent(request, annotation_dict) request.registry.notify(event) es.conn.index( index=es.index, doc_type=es.t.annotation, body=annotation_dict, id=annotation_dict["id"], )
def _prepare(self, annotation): action = {'index': {'_index': self.es_client.index, '_type': self.es_client.t.annotation, '_id': annotation.id}} data = presenters.AnnotationSearchIndexPresenter(annotation).asdict() event = AnnotationTransformEvent(self.request, data) self.request.registry.notify(event) return (action, data)
def test_deleted_annotation_ids_no_changes(self, annotation, db_session, es_scan, pyramid_request): deleter = self.deleter(session=db_session) es_scan.return_value = [{ '_id': annotation.id, '_source': presenters.AnnotationSearchIndexPresenter(annotation) }] deleted_ids = deleter.deleted_annotation_ids() assert len(deleted_ids) == 0
def test_index_emits_AnnotationTransformEvent_when_presenting_bulk_actions( self, db_session, indexer, pyramid_request, streaming_bulk, pyramid_config): annotation = self.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 def transform(event): data = event.annotation_dict data['transformed'] = True pyramid_config.add_subscriber(transform, 'memex.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': indexer.es_client.index, '_id': annotation.id } }, rendered)