コード例 #1
0
def parse_hit(hit: AttrDict, highlighting: bool = True) -> Dict[str, Any]:
    parsed = hit.to_dict()  # Adds name and reference_number if available
    parsed["type"] = hit.meta.index.split("-")[-1]
    parsed["type_translated"] = DOCUMENT_TYPE_NAMES[parsed["type"]]
    parsed["url"] = reverse(parsed["type"], args=[hit.id])
    parsed["score"] = hit.meta.score

    if highlighting:
        highlights = get_highlights(hit, parsed)
        if len(highlights) > 0:
            parsed["highlight"] = html_escape_highlight(highlights[0])
            parsed["highlight_extracted"] = (
                highlights[0].split("<mark>")[1].split("</mark>")[0])
        else:
            parsed["highlight"] = None
            parsed["highlight_extracted"] = None

        if hit.type == "file" and hit.highlight_extracted:
            parsed["url"] += "?pdfjs_search=" + quote(
                parsed["highlight_extracted"])

    parsed["name_escaped"] = html_escape_highlight(parsed["name"])
    parsed["reference_number_escaped"] = html_escape_highlight(
        parsed.get("reference_number"))

    return parsed
コード例 #2
0
ファイル: indicies.py プロジェクト: voleg/paea
    def query(self, func_name=None, created_at=None):
        must_q = []
        if func_name:
            must_q.append(
                Q('match', tissue={
                    'query': func_name,
                    'operator': 'and'
                }))

        if len(must_q):
            q = Q('bool', must=must_q)
        else:
            q = Q('match_all')

        qs = self.search.sort("-created_at").query(q)

        # research and FIXME
        total = 0
        try:
            total = qs.count()
        except KeyError as e:
            log.warning('Count request could not be completed')

        return AttrDict({
            'total': total,
            'scroll': qs.scan(),  # FIXME: does not respects sorting ...
            'search': qs,
        })
コード例 #3
0
 def read_text_tokens_date_histogram_terms(
         self,
         response: Response) -> Iterator[Tuple[Bucket, Sequence[Bucket]]]:
     for bucket in getattr(response.aggs,
                           self._date_field.replace(".", "__")).buckets:
         yield bucket, getattr(
             bucket,
             self._text_tokens_field.replace(".", "__"),
             AttrDict({"buckets": []}),
         ).buckets
コード例 #4
0
ファイル: search.py プロジェクト: 3lnc/elasticsearch-adsl
    async def delete(self):
        """delete() executes the query by delegating to delete_by_query()"""
        aes = get_connection(self._using)

        response = await aes.delete_by_query(
            index=self._index,
            body=self.to_dict(),
            **self._params,
        )

        return AttrDict(response)
コード例 #5
0
def model_has_field_path(es_model, path):
    """Checks whether a field path (e.g. company.id) exists in a model."""
    path_components = path.split('.')
    fields = get_model_fields(es_model)

    for sub_field_name in path_components:
        if sub_field_name not in fields:
            return False

        sub_field = fields.get(sub_field_name)
        fields = getattr(sub_field, 'properties', AttrDict({})).to_dict()
        if not fields:
            fields = getattr(sub_field, 'fields', {})

    return True
コード例 #6
0
    def facets(self):
        """Unlabelled facets."""
        if not hasattr(self, "_facets"):
            super().__setattr__("_facets", AttrDict({}))

            try:
                for name, facet, data, selection in self._iter_facets():
                    self._facets[name] = facet.get_values(data, selection)
            except AttributeError:
                # Attribute errors are masked by AttrDict, so we reraise as a
                # different exception.
                raise RuntimeError(
                    'Failed to created facets due to attribute error.')

        return self._facets
コード例 #7
0
    def register_document(self, document):
        django_meta = getattr(document, 'Django')
        # Raise error if Django class can not be found
        if not django_meta:
            message = "You must declare the Django class inside {}".format(
                document.__name__)
            raise ImproperlyConfigured(message)

        # Keep all django related attribute in a django_attr AttrDict
        data = {'model': getattr(document.Django, 'model')}
        django_attr = AttrDict(data)

        if not django_attr.model:
            raise ImproperlyConfigured("You must specify the django model")

        # Add The model fields into elasticsearch mapping field
        model_field_names = getattr(document.Django, "fields", [])
        mapping_fields = document._doc_type.mapping.properties.properties.to_dict(
        ).keys()

        for field_name in model_field_names:
            if field_name in mapping_fields:
                raise RedeclaredFieldError(
                    "You cannot redeclare the field named '{}' on {}".format(
                        field_name, document.__name__))

            django_field = django_attr.model._meta.get_field(field_name)

            field_instance = document.to_field(field_name, django_field)
            document._doc_type.mapping.field(field_name, field_instance)

        django_attr.ignore_signals = getattr(django_meta, "ignore_signals",
                                             False)
        django_attr.auto_refresh = getattr(django_meta, "auto_refresh",
                                           DEDConfig.auto_refresh_enabled())
        django_attr.related_models = getattr(django_meta, "related_models", [])
        django_attr.queryset_pagination = getattr(django_meta,
                                                  "queryset_pagination", None)

        # Add django attribute in the document class with all the django attribute
        setattr(document, 'django', django_attr)

        # Set the fields of the mappings
        fields = document._doc_type.mapping.properties.properties.to_dict()
        setattr(document, '_fields', fields)

        # Update settings of the document index
        default_index_settings = deepcopy(DEDConfig.default_index_settings())
        document._index.settings(**default_index_settings)

        # Register the document and index class to our registry
        self.register(index=document._index, doc_class=document)

        return document
コード例 #8
0
    def execute(self):
        out = []
        for position in range(
                self._s.to_dict()["from"],
                self._s.to_dict()["from"] + self._s.to_dict()["size"]):
            result = template.copy()
            result["highlight"] = {
                "name": ["<mark>" + str(position) + "</mark>"]
            }
            result["fields"]["name"] = str(position)
            result["fields"]["name_escaped"] = str(position)
            result["fields"]["id"] = position
            out.append(Hit(result))
        hits = AttrList(out)
        hits.__setattr__("total", len(out) * 2)

        return AttrDict({"hits": hits, "facets": get_aggregations()})
コード例 #9
0
    def execute(self):
        out = []
        for position in range(
            self._s.to_dict()["from"],
            self._s.to_dict()["from"] + self._s.to_dict()["size"],
        ):
            result = template.copy()
            result["highlight"] = {"name": ["<mark>" + str(position) + "</mark>"]}
            result["fields"] = {
                "id": position,
                "name": str(position),
                "name_escaped": str(position),
                "type": "file",
                "type_translated": "File",
                "created": "2017-11-24T09:06:05.159381+00:00",
                "modified": "2017-12-01T10:56:37.297771+00:00",
            }
            out.append(Hit(result))
        hits = AttrList(out)
        hits.__setattr__("total", len(out) * 2)

        return AttrDict({"hits": hits, "facets": get_aggregations()})
コード例 #10
0
def mock_search_autocomplete(*args):
    return AttrDict({'hits': []})
コード例 #11
0
 def execute(self):
     hits = AttrList([Hit(template)])
     hits.__setattr__("total", 1)
     return AttrDict({"hits": hits, "facets": get_aggregations()})
コード例 #12
0
ファイル: serializers.py プロジェクト: SwordJack/pkdb
 def get_pdf(self, obj):
     user = self.context["request"].user
     if get_study_file_permission(user, AttrDict(obj.study)):
         return obj.to_dict().get("pdf")
     else:
         return None
コード例 #13
0
 def execute(self):
     hits = AttrList([Hit(template.copy())])
     hits.__setattr__("total", {"value": 1, "relation": "eq"})
     return AttrDict({"hits": hits, "facets": get_aggregations()})