Beispiel #1
0
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"],
    )
Beispiel #2
0
Datei: index.py Projekt: ziqizh/h
    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)
Beispiel #3
0
    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)
Beispiel #4
0
def test_annotation_transform_event():
    evt = AnnotationTransformEvent(s.request, s.annotation, s.annotation_dict)

    assert evt.request == s.request
    assert evt.annotation == s.annotation
    assert evt.annotation_dict == s.annotation_dict