Beispiel #1
0
class ProductDoc(Document):
    __doc_type__ = 'product'

    model = Field(Text)
    attrs = Field(List(Long))
    attrs_bool = Field(List(Long))
    attrs_range = Field(List(Long))
Beispiel #2
0
def qf():
    qf = QueryFilter()
    qf.add_filter(
        AttrBoolFacetFilter('attr_bool', Field('attr.bool'), alias='a'))
    qf.add_filter(AttrIntFacetFilter('attr_int', Field('attr.int'), alias='a'))
    qf.add_filter(
        AttrRangeSimpleFilter('attr_float', Field('attr.float'), alias='a'))
    yield qf
Beispiel #3
0
        class CarDocument(Document):
            __doc_type__ = 'car'

            vendor = Field(String)
            model = Field(String)
            date_manufactured = Field(Date)

            __mapping_options__ = {
                'date_detection': False,
            }
Beispiel #4
0
class MessageDocument(Document):
    __doc_type__ = 'message'

    __mapping_options__ = {
        'dynamic': True,
        'date_detection': False,
    }

    user_id = Field(Integer)
    user_name = Field(String)
    text = Field(String)
Beispiel #5
0
def test_attr_int_facet_filter__include_values(int_qf_with_values, compiler):
    sq = int_qf_with_values.apply(SearchQuery(), {'a18': '58084'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_int.filter':
        agg.Filter(
            Term('attr.int', 0x12_0000e2e4),
            aggs={'qf.attr_int': agg.Terms(Field('attr.int'), size=10_000)}),
        'qf.attr_int:18':
        agg.Terms(Field('attr.int'),
                  size=100,
                  include=[0x12_0000e2e4, 0x12_0000e7e5])
    }).post_filter(Term('attr.int', 0x12_0000e2e4)).to_dict(compiler=compiler))
Beispiel #6
0
class ListsDocument(Document):
    __doc_type__ = 'lists'

    boolean_0 = Field(List(Boolean))
    integer_0 = Field(List(Integer))
    float_0 = Field(List(Float))
    string_0 = Field(List(String))
    date_0 = Field(List(Date))

    boolean_1 = Field(List(Boolean))
    integer_1 = Field(List(Integer))
    float_1 = Field(List(Float))
    string_1 = Field(List(String))
    date_1 = Field(List(Date))
Beispiel #7
0
class SimpleDocument(Document):
    __doc_type__ = 'simple'

    boolean_0 = Field(Boolean)
    integer_0 = Field(Integer)
    float_0 = Field(Float)
    string_0 = Field(String)
    date_0 = Field(Date)

    boolean_1 = Field(Boolean)
    integer_1 = Field(Integer)
    float_1 = Field(Float)
    string_1 = Field(String)
    date_1 = Field(Date)
Beispiel #8
0
def test_attr_int_facet_filter__empty_params(int_qf, compiler):
    sq = int_qf.apply(SearchQuery(), {})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_int':
        agg.Terms(Field('attr.int'), size=10_000)
    }).to_dict(compiler=compiler))
    qf_res = int_qf.process_result(
        SearchResult(
            {
                'aggregations': {
                    'qf.attr_int': {
                        'buckets': [{
                            'key': 0x12_00000001,
                            'doc_count': 123,
                        }, {
                            'key': 0x144_0000dead,
                            'doc_count': 99
                        }, {
                            'key': 0x12_f0000000,
                            'doc_count': 1
                        }]
                    }
                }
            },
            aggregations=sq.get_context().aggregations))
Beispiel #9
0
def test_attr_int_facet_filter__single_selected_value(int_qf, compiler):
    params = {'a18': '58084'}
    sq = int_qf.apply(SearchQuery(), params)
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_int.filter':
        agg.Filter(Term('attr.int', 0x12_0000e2e4),
                   aggs={
                       'qf.attr_int': agg.Terms(Field('attr.int'),
                                                size=10_000),
                   }),
        'qf.attr_int:18':
        agg.Terms(Field('attr.int'), size=100),
    }).post_filter(Term('attr.int', 0x12_0000e2e4)).to_dict(compiler=compiler))
    qf_res = int_qf.process_result(
        SearchResult(
            {
                'aggregations': {
                    'qf.attr_int.filter': {
                        'doc_count': 201,
                        'qf.attr_int': {
                            'buckets': [{
                                'key': 0x144_0000dead,
                                'doc_count': 123,
                            }, {
                                'key': 0x12_0000e2e4,
                                'doc_count': 119
                            }, {
                                'key': 0x144_0000beef,
                                'doc_count': 1
                            }]
                        }
                    },
                    'qf.attr_int:18': {
                        'buckets': [
                            {
                                'key': 0x12_0000e2e4,
                                'doc_count': 99
                            },
                            {
                                'key': 0x12_0000e7e5,
                                'doc_count': 88
                            },
                        ]
                    }
                }
            },
            aggregations=sq.get_context().aggregations))
Beispiel #10
0
def int_qf_with_values():
    qf = QueryFilter()
    qf.add_filter(
        AttrIntFacetFilter(
            'attr_int',
            Field('attr.int'),
            alias='a',
            attrs_values_getter=lambda _: {18: [0xe2e4, 0xe7e5]}))
    yield qf
Beispiel #11
0
def test_attr_int_facet_filter__existing_post_filter(int_qf, compiler):
    sq = int_qf.apply(SearchQuery().post_filter(Range('price', lt=100)), {})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_int.filter':
        agg.Filter(
            Range('price', lt=100),
            aggs={'qf.attr_int': agg.Terms(Field('attr.int'), size=10_000)})
    }).post_filter(Range('price', lt=100)).to_dict(compiler=compiler))

    sq = int_qf.apply(
        SearchQuery().post_filter(Range('price', lt=100), meta={'price':
                                                                True}), {})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_int.filter':
        agg.Filter(
            Range('price', lt=100),
            aggs={'qf.attr_int': agg.Terms(Field('attr.int'), size=10_000)})
    }).post_filter(Range('price', lt=100)).to_dict(compiler=compiler))
Beispiel #12
0
def test_attr_int_simple_filter_no_alias(compiler):
    qf = QueryFilter()
    qf.add_filter(AttrIntSimpleFilter('attr', Field('attr.int')))

    sq = qf.apply(SearchQuery(), {})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a18': '224'})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'attr18': '1234'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Term('attr.int', 0x12_000004d2)).to_dict(compiler=compiler))
Beispiel #13
0
def test_attr_range_facet_filter__existing_post_filter(range_qf, compiler):
    sq = range_qf.apply(SearchQuery().post_filter(Field('status').term(0)), {})
    assert_search_query(
        sq,
        SearchQuery().aggs({
            'qf.attr_range.filter':
            agg.Filter(Term('status', 0),
                       aggs={
                           'qf.attr_range':
                           agg.Terms(script=Script(
                               'doc[params.field].value >>> 32',
                               lang='painless',
                               params={
                                   'field': 'attr.float',
                               }),
                                     size=100),
                       }),
        }).post_filter(Term('status', 0)), compiler)
Beispiel #14
0
def test_attr_bool_facet_filter__empty_params(bool_qf, compiler):
    sq = bool_qf.apply(SearchQuery(), {})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_bool':
        agg.Terms(Field('attr.bool'), size=100)
    }).to_dict(compiler=compiler))
    qf_res = bool_qf.process_result(
        SearchResult(
            {
                'aggregations': {
                    'qf.attr_bool': {
                        'buckets': [{
                            'key': 0b11,
                            'doc_count': 123,
                        }, {
                            'key': 0b10,
                            'doc_count': 99
                        }, {
                            'key': 0b101,
                            'doc_count': 1
                        }]
                    }
                }
            },
            aggregations=sq.get_context().aggregations))
    assert len(qf_res.attr_bool.facets) == 2
    facet = qf_res.attr_bool.get_facet(1)
    assert len(facet.all_values) == 2
    assert facet.all_values[0].value is True
    assert facet.all_values[0].count == 123
    assert facet.all_values[0].count_text == '123'
    assert facet.all_values[0].selected is False
    assert facet.all_values[1].value is False
    assert facet.all_values[1].count == 99
    assert facet.all_values[1].count_text == '99'
    assert facet.all_values[1].selected is False
    facet = qf_res.attr_bool.get_facet(2)
    assert len(facet.all_values) == 1
    assert facet.all_values[0].value is True
    assert facet.all_values[0].count == 1
    assert facet.all_values[0].count_text == '1'
    assert facet.all_values[0].selected is False
Beispiel #15
0
def test_attr_bool_simple_filter(compiler):
    qf = QueryFilter()
    qf.add_filter(
        AttrBoolSimpleFilter('attr_bool', Field('attr.bool'), alias='a'))

    sq = qf.apply(SearchQuery(), {})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a1': 'true'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Term('attr.bool', 0x3)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a1': 'True'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Term('attr.bool', 0x3)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a1': [True]})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Term('attr.bool', 0x3)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a1': 'False'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Term('attr.bool', 0x2)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a1': ['true', 'false']})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Terms('attr.bool', [0x3, 0x2])).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a1': ['true', 'false'], 'a2': 'false'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Terms('attr.bool',
              [0x3, 0x2])).filter(Term('attr.bool',
                                       0x4)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a2147483648': '1'})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a1': 'TRUE'})
    assert sq.to_dict(compiler=compiler) == {}
Beispiel #16
0
def test_attr_int_simple_filter(compiler):
    qf = QueryFilter()
    qf.add_filter(AttrIntSimpleFilter('attr_int', Field('attr.int'),
                                      alias='a'))

    sq = qf.apply(SearchQuery(), {})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'b18': '224'})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a18': '1234'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Term('attr.int', 0x12_000004d2)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a18': ['1234', '5678']})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Terms('attr.int',
              [0x12_000004d2, 0x12_0000162e])).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a18': ['1234', '5678'], 'a324': '90'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Terms('attr.int', [0x12_000004d2, 0x12_0000162e])).filter(
            Term('attr.int', 0x144_0000005a)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a18': '0x1234'})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a18-19': '1234'})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a2147483648': '1'})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a1': '2147483648'})
    assert sq.to_dict(compiler=compiler) == {}
Beispiel #17
0
def test_attr_int_facet_filter__unknown_param(int_qf, compiler):
    sq = int_qf.apply(SearchQuery(), {'b18': '224'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_int':
        agg.Terms(Field('attr.int'), size=10_000)
    }).to_dict(compiler=compiler))
Beispiel #18
0
def test_attr_bool_facet_filter__multiple_selected_values(bool_qf, compiler):
    sq = bool_qf.apply(SearchQuery(), {'a1': ['true', 'false'], 'a2': 'true'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_bool.filter':
        agg.Filter(
            Bool.must(
                Terms('attr.bool', [0b11, 0b10]),
                Term('attr.bool', 0b101),
            ),
            aggs={'qf.attr_bool': agg.Terms(Field('attr.bool'), size=100)}),
        'qf.attr_bool.filter:1':
        agg.Filter(Term('attr.bool', 0b101),
                   aggs={
                       'qf.attr_bool:1':
                       agg.Terms(Field('attr.bool'),
                                 size=2,
                                 include=[0b10, 0b11])
                   }),
        'qf.attr_bool.filter:2':
        agg.Filter(Terms('attr.bool', [0b11, 0b10]),
                   aggs={
                       'qf.attr_bool:2':
                       agg.Terms(Field('attr.bool'),
                                 size=2,
                                 include=[0b100, 0b101])
                   }),
    }).post_filter(
        Bool.must(
            Terms('attr.bool', [0b11, 0b10]),
            Term('attr.bool', 0b101),
        )).to_dict(compiler=compiler))
    qf_res = bool_qf.process_result(
        SearchResult(
            {
                'aggregations': {
                    'qf.attr_bool.filter': {
                        'doc_count': 200,
                        'qf.attr_bool': {
                            'buckets': [
                                {
                                    'key': 0b11,
                                    'doc_count': 123,
                                },
                                {
                                    'key': 0b101,
                                    'doc_count': 1
                                },
                            ]
                        }
                    },
                    'qf.attr_bool.filter:1': {
                        'doc_count': 163,
                        'qf.attr_bool:1': {
                            'buckets': [
                                {
                                    'key': 0b11,
                                    'doc_count': 123,
                                },
                                {
                                    'key': 0b10,
                                    'doc_count': 99
                                },
                            ]
                        }
                    },
                    'qf.attr_bool.filter:2': {
                        'doc_count': 144,
                        'qf.attr_bool:2': {
                            'buckets': [
                                {
                                    'key': 0b101,
                                    'doc_count': 1
                                },
                            ]
                        }
                    },
                }
            },
            aggregations=sq.get_context().aggregations))
    assert len(qf_res.attr_bool.facets) == 2
    facet = qf_res.attr_bool.get_facet(1)
    assert len(facet.all_values) == 2
    assert len(facet.selected_values) == 2
    assert len(facet.values) == 0
    assert facet.all_values[0] is facet.selected_values[0]
    assert facet.all_values[1] is facet.selected_values[1]
    assert facet.all_values[0].value is True
    assert facet.all_values[0].count == 123
    assert facet.all_values[0].count_text == '123'
    assert facet.all_values[0].selected is True
    assert facet.all_values[1].value is False
    assert facet.all_values[1].count == 99
    assert facet.all_values[1].count_text == '99'
    assert facet.all_values[1].selected is True
    facet = qf_res.attr_bool.get_facet(2)
    assert len(facet.all_values) == 1
    assert len(facet.selected_values) == 1
    assert len(facet.values) == 0
    assert facet.all_values[0] is facet.selected_values[0]
    assert facet.all_values[0].value is True
    assert facet.all_values[0].count == 1
    assert facet.all_values[0].count_text == '1'
    assert facet.all_values[0].selected is True
Beispiel #19
0
class Car(Document):
    __doc_type__ = 'car'

    name = Field(Text())
Beispiel #20
0
def test_attr_range_simple_filter(compiler):
    qf = QueryFilter()
    qf.add_filter(
        AttrRangeSimpleFilter('attr_float', Field('attr.float'), alias='a'))

    sq = qf.apply(SearchQuery(), {})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a8__gte': '3.14'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Range('attr.float', gte=0x8_4048f5c3,
              lte=0x8_7f800000)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a8__gte': '-3.14'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Bool.should(Range('attr.float', gte=0x8_80000000, lte=0x8_c048f5c3),
                    Range('attr.float', gte=0x8_00000000,
                          lte=0x8_7f800000))).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a8__lte': '-2.71'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Range('attr.float', gte=0x8_c02d70a4,
              lte=0x8_ff800000)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a8__gte': ['1', '2.71'], 'a8__lte': '3.14'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Range('attr.float', gte=0x8_402d70a4,
              lte=0x8_4048f5c3)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a8__gte': '-3.14', 'a8__lte': '-2.71'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Range('attr.float', gte=0x8_c02d70a4,
              lte=0x8_c048f5c3)).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a8__gte': '-3.14', 'a8__lte': '3.14'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Bool.should(Range('attr.float', gte=0x8_80000000, lte=0x8_c048f5c3),
                    Range('attr.float', gte=0x8_00000000,
                          lte=0x8_4048f5c3))).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a8__gte': '3.14', 'a8__lte': '-3.14'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Bool.must(Range('attr.float', gte=0x8_4048f5c3, lte=0x8_7f800000),
                  Range('attr.float', gte=0x8_c048f5c3,
                        lte=0x8_ff800000))).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {
        'a8__gte': '2.71',
        'a8__lte': '3.14',
        'a99__lte': '99'
    })
    assert sq.to_dict(compiler=compiler) == (SearchQuery().filter(
        Range('attr.float', gte=0x8_402d70a4, lte=0x8_4048f5c3)).filter(
            Bool.should(
                Range('attr.float', gte=0x63_00000000, lte=0x63_42c60000),
                Range('attr.float', gte=0x63_80000000,
                      lte=0x63_ff800000))).to_dict(compiler=compiler))

    sq = qf.apply(SearchQuery(), {'a99.9__gte': '99.9'})
    assert sq.to_dict(compiler=compiler) == {}

    sq = qf.apply(SearchQuery(), {'a99__gte': '100ee2'})
    assert sq.to_dict(compiler=compiler) == {}
Beispiel #21
0
def test_attr_bool_facet_filter__unknown_param(bool_qf, compiler):
    sq = bool_qf.apply(SearchQuery(), {'b18': 'true'})
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_bool':
        agg.Terms(Field('attr.bool'), size=100)
    }).to_dict(compiler=compiler))
Beispiel #22
0
class ProductDoc(Document):
    __doc_type__ = 'product'

    name = Field(Text)
    status = Field(Integer)
Beispiel #23
0
def test_attr_int_facet_filter__multiple_selected_values(int_qf, compiler):
    sq = int_qf.apply(SearchQuery(), {
        'a18': '58084',
        'a324': ['57005', '48879']
    })
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_int.filter':
        agg.Filter(Bool.must(
            Term('attr.int', 0x12_0000e2e4),
            Terms('attr.int', [0x144_0000dead, 0x144_0000beef]),
        ),
                   aggs={
                       'qf.attr_int': agg.Terms(Field('attr.int'),
                                                size=10_000),
                   }),
        'qf.attr_int.filter:18':
        agg.Filter(Terms('attr.int', [0x144_0000dead, 0x144_0000beef]),
                   aggs={
                       'qf.attr_int:18': agg.Terms(Field('attr.int'),
                                                   size=100),
                   }),
        'qf.attr_int.filter:324':
        agg.Filter(Term('attr.int', 0x12_0000e2e4),
                   aggs={
                       'qf.attr_int:324': agg.Terms(Field('attr.int'),
                                                    size=100),
                   })
    }).post_filter(Term('attr.int', 0x12_0000e2e4)).post_filter(
        Terms('attr.int',
              [0x144_0000dead, 0x1440000beef])).to_dict(compiler=compiler))
    qf_res = int_qf.process_result(
        SearchResult(
            {
                'aggregations': {
                    'qf.attr_int.filter': {
                        'doc_count': 404,
                        'qf.attr_int': {
                            'buckets': [{
                                'key': 0x144_0000dead,
                                'doc_count': 1
                            }, {
                                'key': 0x12_0000e2e4,
                                'doc_count': 1
                            }, {
                                'key': 0x144_0000beef,
                                'doc_count': 1
                            }]
                        }
                    },
                    'qf.attr_int.filter:18': {
                        'doc_count': 200,
                        'qf.attr_int:18': {
                            'buckets': [
                                {
                                    'key': 0x12_0000e2e4,
                                    'doc_count': 99
                                },
                                {
                                    'key': 0x12_0000e7e5,
                                    'doc_count': 88
                                },
                            ]
                        }
                    },
                    'qf.attr_int.filter:324': {
                        'doc_count': 200,
                        'qf.attr_int:324': {
                            'buckets': [
                                {
                                    'key': 0x144_0000dead,
                                    'doc_count': 123
                                },
                                {
                                    'key': 0x144_0000beef,
                                    'doc_count': 1
                                },
                            ]
                        }
                    },
                }
            },
            aggregations=sq.get_context().aggregations))
Beispiel #24
0
def int_qf():
    qf = QueryFilter()
    qf.add_filter(AttrIntFacetFilter('attr_int', Field('attr.int'), alias='a'))
    yield qf
Beispiel #25
0
        class CarDocument(Document):
            __doc_type__ = 'car'

            vendor = Field(String)
            model = Field(String)
Beispiel #26
0
    def test_field(self):
        self.assertEqual(Field().get_type().__class__, Type)
        self.assertIs(Field().get_name(), None)

        self.assertRaises(TypeError, Field, [])
        self.assertRaises(TypeError, Field, 'name', 1, 2)
        self.assert_expression(Field('name'), "name")
        
        self.assert_expression(
            Field('status') == 0,
            {
                "term": {"status": 0}
            }
        )
        self.assert_expression(
            Field('presence') == None,
            {
                "missing": {"field": "presence"}
            }
        )
        self.assert_expression(
            Field('status') != 1,
            {
                "bool": {
                    "must_not": [
                        {
                            "term": {"status": 1}
                        }
                    ]
                }
            }
        )
        self.assert_expression(
            Field('presence') != None,
            {
                "exists": {"field": "presence"}
            }
        )
        self.assert_expression(
            Field('name').span_first("iphone", 2),
            {
                "span_first": {
                    "match": {
                        "span_term": {"name": "iphone"}
                    },
                    "end": 2
                }
            }
        )
        self.assert_expression(
            ~(Field('status') == 0),
            {
                "bool": {
                    "must_not": [
                        {
                            "term": {"status": 0}
                        }
                    ]
                }
            }
        )
        self.assert_expression(
            Field('name').span_first("iphone", 2),
            {
                "span_first": {
                    "match": {
                        "span_term": {"name": "iphone"}
                    },
                    "end": 2
                }
            }
        )
        self.assert_expression(
            Field('status').in_([0, 2, 3]),
            {
                "terms": {"status": [0, 2, 3]}
            }
        )
        self.assert_expression(
            Field('status').in_(iter([0, 2, 3])),
            {
                "terms": {"status": [0, 2, 3]}
            }
        )
        self.assert_expression(
            Field('status').not_in([0, 5, 10]),
            {
                "bool": {
                    "must_not": [{
                        "terms": {"status": [0, 5, 10]}
                    }]
                }
            }
        )
        self.assert_expression(
            Field('status').not_in(iter([0, 5, 10])),
            {
                "bool": {
                    "must_not": [{
                        "terms": {"status": [0, 5, 10]}
                    }]
                }
            }
        )

        self.assert_expression(
            Field('price') > 99.9,
            {
                "range": {
                    "price": {"gt": 99.9}
                }
            }
        )
        self.assert_expression(
            Field('price') < 101,
            {
                "range": {
                    "price": {"lt": 101}
                }
            }
        )
        self.assert_expression(
            Field('price') <= 1000,
            {
                "range": {
                    "price": {"lte": 1000}
                }
            }
        )
        self.assert_expression(
            Field('price').range(gte=100, lt=200),
            {
                "range": {
                    "price": {"gte": 100, "lt": 200}
                }
            }
        )
        self.assert_expression(
            Field('name').match('Hello kitty', minimum_should_match=2),
            {
                "match": {
                    "name": {
                        "query": "Hello kitty",
                        "minimum_should_match": 2
                    }
                }
            }
        )
        self.assert_expression(
            Field('price').asc(mode='min'),
            {
                "price": {
                    "order": "asc",
                    "mode": "min"
                }
            }
        )
        self.assert_expression(
            Field('price').desc(),
            {
                "price": "desc"
            }
        )
        self.assert_expression(
            Field('description').boost(0.1),
            "description^0.1"
        )
Beispiel #27
0
    def test_field_mapping(self):
        f = Field('name', String)
        self.assertEqual(
            f.to_mapping(),
            {
                "name": {
                    "type": "string"
                }
            }
        )

        f = Field('name', String, fields={'sort': Field(String)})
        self.assertEqual(
            f.to_mapping(),
            {
                "name": {
                    "type": "string",
                    "fields": {
                        "sort": {
                            "type": "string",
                        }
                    }
                }
            }
        )

        f = Field('name', String, fields={'sort': Field('ordering', String)})
        self.assertEqual(
            f.to_mapping(),
            {
                "name": {
                    "type": "string",
                    "fields": {
                        "ordering": {
                            "type": "string",
                        }
                    }
                }
            }
        )
        
        f = Field(
            'name', String,
            fields=[Field('raw', String, index='not_analyzed')]
        )
        self.assertEqual(
            f.to_mapping(),
            {
                "name": {
                    "type": "string",
                    "fields": {
                        "raw": {
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        )

        f = Field('status', Integer)
        self.assertEqual(
            f.to_mapping(),
            {
                "status": {
                    "type": "integer"
                }
            }
        )

        f = Field('tag', List(Integer))
        self.assertEqual(
            f.to_mapping(),
            {
                "tag": {
                    "type": "integer"
                }
            }
        )

        f = Field('pin', GeoPoint())
        self.assertEqual(
            f.to_mapping(),
            {
                "pin": {
                    "type": "geo_point"
                }
            }
        )
        f = Field('pin', GeoPoint(), lat_lon=True)
        self.assertEqual(
            f.to_mapping(),
            {
                "pin": {
                    "type": "geo_point",
                    "lat_lon": True,
                }
            }
        )

        f = Field('suggest', Completion())
        self.assertEqual(
            f.to_mapping(),
            {
                'suggest': {
                    'type': 'completion',
                }
            }
        )
        f = Field('suggest', Completion(), payloads=True)
        self.assertEqual(
            f.to_mapping(),
            {
                'suggest': {
                    'type': 'completion',
                    'payloads': True,
                }
            }
        )
Beispiel #28
0
def bool_qf():
    qf = QueryFilter()
    qf.add_filter(
        AttrBoolFacetFilter('attr_bool', Field('attr.bool'), alias='a'))
    yield qf
Beispiel #29
0
def range_qf():
    qf = QueryFilter()
    qf.add_filter(
        AttrRangeFacetFilter('attr_range', Field('attr.float'), alias='a'))
    yield qf
Beispiel #30
0
def test_combined_facet_filters(qf, compiler):
    sq = qf.apply(SearchQuery(), {
        'a1': 'true',
        'a18': '58084',
        'a324': '57005',
        'a8__gte': '2.71',
    })
    assert sq.to_dict(compiler=compiler) == (SearchQuery().aggs({
        'qf.attr_bool.filter':
        agg.Filter(Bool.must(
            Term('attr.bool', 0b11),
            Term('attr.int', 0x12_0000e2e4),
            Term('attr.int', 0x144_0000dead),
        ),
                   aggs={
                       'qf.attr_bool': agg.Terms(Field('attr.bool'), size=100),
                   }),
        'qf.attr_bool.filter:1':
        agg.Filter(Bool.must(
            Term('attr.int', 0x12_0000e2e4),
            Term('attr.int', 0x144_0000dead),
        ),
                   aggs={
                       'qf.attr_bool:1':
                       agg.Terms(
                           Field('attr.bool'),
                           size=2,
                           include=[0b10, 0b11],
                       ),
                   }),
        'qf.attr_int.filter':
        agg.Filter(Bool.must(
            Term('attr.bool', 0b11),
            Term('attr.int', 0x12_0000e2e4),
            Term('attr.int', 0x144_0000dead),
        ),
                   aggs={
                       'qf.attr_int': agg.Terms(Field('attr.int'),
                                                size=10_000),
                   }),
        'qf.attr_int.filter:18':
        agg.Filter(Bool.must(
            Term('attr.bool', 0b11),
            Term('attr.int', 0x144_0000dead),
        ),
                   aggs={
                       'qf.attr_int:18': agg.Terms(Field('attr.int'),
                                                   size=100),
                   }),
        'qf.attr_int.filter:324':
        agg.Filter(Bool.must(
            Term('attr.bool', 0b11),
            Term('attr.int', 0x12_0000e2e4),
        ),
                   aggs={
                       'qf.attr_int:324': agg.Terms(Field('attr.int'),
                                                    size=100),
                   })
    }).post_filter(Term('attr.bool', 0b11)).post_filter(
        Term('attr.int', 0x12_0000e2e4)).post_filter(
            Term('attr.int', 0x144_0000dead)).filter(
                Range('attr.float', gte=0x8_402d70a4,
                      lte=0x8_7f800000)).to_dict(compiler=compiler))