Exemplo n.º 1
0
def test_filter_can_be_instantiated_using_positional_args():
    a = aggs.Filter(query.Q('term', f=42))

    assert {'filter': {'term': {'f': 42}}} == a.to_dict()

    assert a == aggs.A('filter', query.Q('term', f=42))
Exemplo n.º 2
0
def test_empty_Q_is_match_all():
    q = query.Q()

    assert isinstance(q, query.MatchAll)
    assert query.MatchAll() == q
Exemplo n.º 3
0
def test_bool_from_dict_issue_318():
    d = {"bool": {"must_not": {"match": {"field": "value"}}}}
    q = query.Q(d)

    assert q == ~query.Match(field='value')
Exemplo n.º 4
0
def test_Q_raises_error_when_passed_in_query_and_params():
    q = query.Match(f='value1')

    with raises(Exception):
        query.Q(q, f='value')
Exemplo n.º 5
0
def test_Q_raises_error_on_unknown_query():
    with raises(Exception):
        query.Q('not a query', f='value')
Exemplo n.º 6
0
def test_Q_constructs_simple_query_from_dict():
    q = query.Q({'match': {'f': 'value'}})

    assert isinstance(q, query.Match)
    assert {'f': 'value'} == q._params
Exemplo n.º 7
0
def test_Q_raises_error_when_passed_in_dict_and_params():
    with raises(Exception):
        query.Q({"match": {'f': 'value'}}, f='value')
Exemplo n.º 8
0
def test_Q_translates_double_underscore_to_dots_in_param_names():
    q = query.Q('match', comment__author='honza')

    assert {'comment.author': 'honza'} == q._params
Exemplo n.º 9
0
def test_Q_doesn_translate_double_underscore_to_dots_in_param_names():
    q = query.Q('match', comment__author='honza', _expand__to_dot=False)

    assert {'comment__author': 'honza'} == q._params
Exemplo n.º 10
0
def test_Q_passes_query_through():
    q = query.Match(f='value1')

    assert query.Q(q) is q
Exemplo n.º 11
0
def test_Q_constructs_query_by_name():
    q = query.Q('match', f='value')

    assert isinstance(q, query.Match)
    assert {'f': 'value'} == q._params
Exemplo n.º 12
0
def test_inverted_query_with_must_and_must_not():
    q = query.Q('bool',
                must=[query.Q('match', f=3),
                      query.Q('match', f=4)],
                must_not=[query.Q('match', f=1),
                          query.Q('match', f=2)])
    print((~q).to_dict())
    assert ~q == query.Q(
        'bool',
        should=[
            # negation of must
            query.Q('bool', must_not=[query.Q('match', f=3)]),
            query.Q('bool', must_not=[query.Q('match', f=4)]),

            # negation of must_not
            query.Q('match', f=1),
            query.Q('match', f=2),
        ])
Exemplo n.º 13
0
def test_filter_can_be_instantiated_using_positional_args():
    a = aggs.Filter(query.Q("term", f=42))

    assert {"filter": {"term": {"f": 42}}} == a.to_dict()

    assert a == aggs.A("filter", query.Q("term", f=42))
Exemplo n.º 14
0
def test_bool_with_different_minimum_should_match_should_not_be_combined():
    q1 = query.Q('bool',
                 minimum_should_match=2,
                 should=[
                     query.Q('term', field='aa1'),
                     query.Q('term', field='aa2'),
                     query.Q('term', field='aa3'),
                     query.Q('term', field='aa4')
                 ])
    q2 = query.Q('bool',
                 minimum_should_match=3,
                 should=[
                     query.Q('term', field='bb1'),
                     query.Q('term', field='bb2'),
                     query.Q('term', field='bb3'),
                     query.Q('term', field='bb4')
                 ])
    q3 = query.Q('bool',
                 minimum_should_match=4,
                 should=[
                     query.Q('term', field='cc1'),
                     query.Q('term', field='cc2'),
                     query.Q('term', field='cc3'),
                     query.Q('term', field='cc4')
                 ])

    q4 = q1 | q2
    assert q4 == query.Bool(should=[q1, q2])

    q5 = q1 | q2 | q3
    assert q5 == query.Bool(should=[q1, q2, q3])
Exemplo n.º 15
0
def test_function_score_with_no_function_is_boost_factor():
    q = query.Q('function_score', functions=[query.SF({'weight': 20, 'filter': query.Q('term', f=42)})])

    assert {'function_score': {'functions': [{'filter': {'term': {'f': 42}}, 'weight': 20}]}} == q.to_dict()
Exemplo n.º 16
0
def test_function_score_with_functions():
    q = query.Q('function_score', functions=[query.SF('script_score', script="doc['comment_count'] * _score")])

    assert {'function_score': {'functions': [{'script_score': {'script': "doc['comment_count'] * _score"}}]}} == q.to_dict()