Пример #1
0
def test_two_bools_are_combined():
    q1 = query.Bool(must=[query.MatchAll(),
                          query.Match(f=42)],
                    should=[query.Match(g="v")])
    q2 = query.Bool(must=[query.Match(x=42)],
                    should=[query.Match(g="v2")],
                    must_not=[query.Match(title='value')])

    q = q1 + q2
    assert isinstance(q, query.Bool)
    assert q.must == [query.MatchAll(), query.Match(f=42), query.Match(x=42)]
    assert q.should == [query.Match(g="v"), query.Match(g="v2")]
    assert q.must_not == [query.Match(title='value')]
Пример #2
0
 def build_first_query(self, args) -> query.Query:
     """
     Nếu khớp với 1 sku hoặc 1 phần của 1 sku nào đó, query sẽ trả về sản phẩm có sku đó
     Query khớp hoàn toàn với tên sản phẩm, không cho phép khoảng trống cũng như đổi thứ tự
     Query sẽ trả về match all nếu text truyền vào rỗng
     :param args:
     :return: product_es
     """
     text_source = args.get('q_source')
     search_text = args.get('search_text')
     search_text_no_tone = args.get('q')
     text_query_condition = query.Bool(
         must=[
             query.Bool(
                 should=[query.MatchAll()] if search_text is None else
                 [
                     *self.build_sku_match_conditions(text_source),
                     *self.build_extract_match_text_conditions(search_text, search_text_no_tone, args)
                 ]
             ),
             query.Exists(field="name"),
             query.Exists(field="seller")
         ],
         filter=self.get_filter_conditions(args)
     )
     products_es = self.build_product_es_from_text_query_condition(args, text_query_condition)
     # print(json.dumps(products_es.to_dict()))
     return products_es
Пример #3
0
    def filter_queryset(self, request, queryset, view):
        locale = request.GET.get('locale', None)
        if '*' == locale:
            return queryset

        sq = queryset.to_dict().pop('query', query.MatchAll().to_dict())

        if request.LANGUAGE_CODE == settings.LANGUAGE_CODE:
            locales = [request.LANGUAGE_CODE]
        else:
            locales = [request.LANGUAGE_CODE, settings.LANGUAGE_CODE]

        positive_sq = {
            'filtered': {
                'query': sq,
                'filter': {'terms': {'locale': locales}}
            }
        }
        negative_sq = {
            'bool': {
                'must_not': [
                    {'term': {'locale': request.LANGUAGE_CODE}}
                ]
            }
        }
        # Note: Here we are replacing the query rather than calling
        # `queryset.query` which would result in a boolean must query.
        queryset.query = query.Boosting(positive=positive_sq,
                                        negative=negative_sq,
                                        negative_boost=0.5)
        return queryset
Пример #4
0
def test_search_query_combines_query():
    s = search.Search()

    s2 = s.query('match', f=42)
    assert s2.query._proxied == query.Match(f=42)
    assert s.query._proxied == query.MatchAll()

    s3 = s2.query('match', f=43)
    assert s2.query._proxied == query.Match(f=42)
    assert s3.query._proxied == query.Bool(must=[query.Match(f=42), query.Match(f=43)])
Пример #5
0
 def find(self, q, facet_filters):
     logger.info('filters: %s', facet_filters)
     queries = self._filter(facet_filters)
     full_text = self._full_text(q) if q else query.MatchAll()
     if queries:
         return PublicationDocSearch(
             self.search.query(
                 query.Bool(should=queries,
                            must=[full_text],
                            minimum_should_match=1)))
     elif q:
         return PublicationDocSearch(self.search.query(full_text))
     else:
         return PublicationDocSearch(self.search.sort('-date_published'))
Пример #6
0
    def build_first_text_query(self, args):
        vn_text_query = args.get('search_text')
        na_text_query = args.get('q')

        if not vn_text_query:
            return query.MatchAll()

        text_query = query.DisMax(
            queries=[
                self.vietnames_prefix_query(vn_text_query),
                self.non_accented_prefix_query(na_text_query)
            ],
            tie_breaker=0
        )
        return text_query
Пример #7
0
    def build_first_text_query(self, args):
        fuzzinees = "AUTO"
        vn_text_query = args.get('search_text')
        na_text_query = args.get('q')

        if not vn_text_query:
            return query.MatchAll()

        text_query = query.DisMax(queries=[
            build_prefix_query("name_no_tone__raw", na_text_query, 1000),
            build_phrase_prefix_query("name", vn_text_query, 200),
            build_phrase_prefix_query("name_no_tone", na_text_query, 100),
            build_match_query("name", vn_text_query, fuzzinees, 5),
            build_match_query("name_no_tone", na_text_query, fuzzinees),
        ],
                                  tie_breaker=0.2)
        return text_query
Пример #8
0
    def get_filter_conditions(self, args):
        conditions = []
        conditions.append(query.MatchAll())

        seller_id = args.get('seller_id')
        if seller_id:
            conditions.append(query.Term(seller__id=seller_id))

        category_codes = args.get('category_codes')
        if category_codes:
            conditions.append(query.Nested(
                path='categories',
                query=query.Terms(categories__code=category_codes)
            ))

        brand_codes = args.get('brand_codes')
        if brand_codes:
            conditions.append(query.Terms(brand__code=brand_codes))

        return conditions
Пример #9
0
    def filter_queryset(self, request, queryset, view):
        locale = request.GET.get("locale", None)
        if "*" == locale:
            return queryset

        sq = queryset.to_dict().pop("query", query.MatchAll().to_dict())

        if request.LANGUAGE_CODE == settings.LANGUAGE_CODE:
            locales = [request.LANGUAGE_CODE]
        else:
            locales = [request.LANGUAGE_CODE, settings.LANGUAGE_CODE]

        positive_sq = {
            "bool": {
                "must": sq,
                "filter": {
                    "terms": {
                        "locale": locales
                    }
                }
            }
        }
        negative_sq = {
            "bool": {
                "must_not": [{
                    "term": {
                        "locale": request.LANGUAGE_CODE
                    }
                }]
            }
        }
        # Note: Here we are replacing the query rather than calling
        # `queryset.query` which would result in a boolean must query.
        queryset.query = query.Boosting(positive=positive_sq,
                                        negative=negative_sq,
                                        negative_boost=0.5)
        return queryset
Пример #10
0
def test_match_all_and_anything_is_anything():
    q = query.MatchAll()

    s = query.Match(f=42)
    assert q & s == s
    assert s & q == s
Пример #11
0
def test_match_all_or_something_is_match_all():
    q1 = query.MatchAll()
    q2 = query.Match(f=42)

    assert (q1 | q2) == query.MatchAll()
    assert (q2 | q1) == query.MatchAll()
Пример #12
0
def test_match_all_and_query_equals_other():
    q1 = query.Match(f=42)
    q2 = query.MatchAll()

    q = q1 & q2
    assert q1 == q
Пример #13
0
def test_match_all_plus_anything_is_anything():
    q = query.MatchAll()

    s = query.Match(f=42)
    assert q + s == s
    assert s + q == s
Пример #14
0
def product_query_condition():
    condition = query.Bool(must=[query.MatchAll()])
    # print(json.dumps(condition.to_dict()))
    return condition
Пример #15
0
def test_empty_Q_is_match_all():
    q = query.Q()

    assert isinstance(q, query.MatchAll)
    assert query.MatchAll() == q
Пример #16
0
 def get_recent_feed_elements(self, sq):
     """Matches all sorted by recent."""
     return sq.sort('-created').query(query.MatchAll())
Пример #17
0
def test_not_match_none_is_match_all():
    q = query.MatchNone()

    assert ~q == query.MatchAll()
Пример #18
0
def test_search_starts_with_empty_query():
    s = search.Search()

    assert s.query._proxied == query.MatchAll()
Пример #19
0
 def get_must_conditions(self, args):
     conditions = []
     file_id = args.get('file_id')
     if file_id:
         if isinstance(file_id, list):
             conditions.append(query.Terms(file_id=file_id))
         else:
             conditions.append(query.Term(file_id=file_id))
     search_text = args.get('q')
     if file_id and search_text:
         raise BadRequestException("Not support both q and file_id param")
     if search_text:
         conditions.append(
             query.DisMax(queries=[
                 query.MatchPhrasePrefix(file_title={
                     'query': search_text,
                     'boost': 10
                 }),
                 query.MatchPhrasePrefix(file_title__no_tone={
                     'query': search_text,
                     'boost': 10
                 }),
                 query.Match(
                     file_title={
                         'query': search_text,
                         'boost': 4,
                         'operator': 'or',
                         'minimum_should_match': "1<75%"
                     }),
                 query.Match(
                     file_title__no_tone={
                         'query': search_text,
                         'boost': 4,
                         'operator': 'or',
                         'minimum_should_match': "1<75%"
                     }),
                 query.Match(
                     file_tag__text={
                         'query': search_text,
                         'boost': 2,
                         'operator': 'or',
                         'minimum_should_match': "1<75%"
                     }),
                 query.MatchPhrasePrefix(file_tag__text={
                     'query': search_text,
                     'boost': 2
                 }),
                 query.Match(
                     description={
                         'query': search_text,
                         'boost': 1,
                         'operator': 'or',
                         'minimum_should_match': "1<75%"
                     }),
                 query.Match(
                     description__no_tone={
                         'query': search_text,
                         'boost': 1,
                         'operator': 'or',
                         'minimum_should_match': "1<75%"
                     })
             ]))
     if not conditions:
         conditions.append(query.MatchAll())
     return conditions