Пример #1
0
    def default_string_query(self, q, options):
        search = AsyncSearch()

        if q == '__all__':
            search = search.query()

        elif q == '__any__' and self.allow_random_query:
            search = search.query('function_score', random_score={})

        else:  # elasticsearch default
            query = {
                "query": {
                    "function_score": {
                        "query": {"dis_max": {"queries": [
                            {"term": {"_id": {"value": q, "boost": 15.0}}},
                            {"term": {"label.raw": {"value": q, "boost": 10.0}}},
                            {"term": {"_meta.username": {"value": q}}},  # for dataset
                            {"term": {"name": {"value": q}}},
                            {"match": {"parent_classes": {"query": q}}},
                            {"prefix": {"label": {"value": q}}},
                            {"query_string": {"query": q}}
                        ]}},
                        "functions": [
                            {"filter": {"term": {"namespace": "schema"}}, "weight": 0.5},
                            {"filter": {"term": {"prefix.raw": "schema"}}, "weight": 0.5},
                            {"filter": {"match": {"parent_classes": "bts:BiologicalEntity"}}, "weight": 1.5}
                        ]
                    }
                }
            }
            search = AsyncSearch()
            search = search.update_from_dict(query)

        search = search.params(rest_total_hits_as_int=True)
        return search
Пример #2
0
    def default_string_query(self, q, options):

        query = {
            "query": {
                "query_string": {
                    "query":
                    q,
                    "fields":
                    ["name^4", "interventions.name^3", "description", "all"]
                }
            }
        }
        search = AsyncSearch()
        search = search.update_from_dict(query)
        return search
Пример #3
0
    def default_string_query(self, q, options):

        search = AsyncSearch()
        q = q.strip()

        if q == '__all__':
            search = search.query()

        elif q == '__any__' and self.allow_random_query:
            search = search.query('function_score', random_score={})

        # elasticsearch query string syntax
        elif ":" in q or " AND " in q or " OR " in q:
            search = search.query('query_string', query=q)

        # term search
        elif q.startswith('"') \
                and q.endswith('"'):
            query = {
                "query": {
                    "dis_max": {
                        "queries": [
                            {
                                "term": {
                                    "_id": {
                                        "value": q.strip('"'),
                                        "boost": 5
                                    }
                                }
                            },
                            {
                                "term": {
                                    "_meta.slug": {
                                        "value": q.strip('"'),
                                        "boost": 5
                                    }
                                }
                            },
                            {
                                "match": {
                                    "info.title": {
                                        "query": q,
                                        "boost": 1.5,
                                        "operator": "AND"
                                    }
                                }
                            },
                            {
                                "query_string": {
                                    "query": q,
                                    "default_operator": "AND"
                                }
                            }  # base score
                        ]
                    }
                }
            }
            search = AsyncSearch()
            search = search.update_from_dict(query)

        else:  # simple text search
            query = {
                "query": {
                    "dis_max": {
                        "queries": [
                            {
                                "term": {
                                    "_id": {
                                        "value": q,
                                        "boost": 5
                                    }
                                }
                            },
                            {
                                "term": {
                                    "_meta.slug": {
                                        "value": q,
                                        "boost": 5
                                    }
                                }
                            },
                            {
                                "match": {
                                    "info.title": {
                                        "query": q,
                                        "boost": 1.5
                                    }
                                }
                            },
                            {
                                "term": {
                                    "servers.url": {
                                        "value": q,
                                        "boost": 1.1
                                    }
                                }
                            },
                            # ---------------------------------------------
                            {
                                "query_string": {
                                    "query": q
                                }
                            },  # base score
                            # ---------------------------------------------
                            {
                                "wildcard": {
                                    "info.title": {
                                        "value": q + "*",
                                        "boost": 0.8
                                    }
                                }
                            },
                            {
                                "wildcard": {
                                    "info.description": {
                                        "value": q + "*",
                                        "boost": 0.5
                                    }
                                }
                            },
                        ]
                    }
                }
            }
            search = AsyncSearch()
            search = search.update_from_dict(query)

        search = search.params(rest_total_hits_as_int=True)
        search = search.source(exclude=['_raw'], include=options._source)

        if options.authors:  # '"Chunlei Wu"'
            search = search.filter('terms',
                                   info__contact__name__raw=options.authors)

        if options.tags:  # '"chemical", "drug"'
            search = search.filter('terms', tags__name__raw=options.tags)

        return search