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)
def _get_filter_expression(self, attr_id: int, values) -> t.Optional[Expression]: w = [ merge_attr_value_int(attr_id, v) for v in self._parse_values(values, 'exact') ] if not w: return None if len(w) == 1: return Term(self.field, w[0]) return Terms(self.field, w)
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) == {}
def test_search_query_compile(self): f = DynamicDocument.fields sq = SearchQuery() self.assert_expression(sq, {}) self.assertEqual(collect_doc_classes(sq), set()) sq = SearchQuery(Term(f.user, 'kimchy')).limit(10).offset(0) self.assert_expression( sq, { "from": 0, "size": 10, "query": { "term": {"user": "******"} } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = SearchQuery(Term(f.user, 'kimchy')).filter(f.age >= 16) self.assert_expression( sq, { "query": { "filtered": { "query": { "term": {"user": "******"} }, "filter": { "range": { "age": {"gte": 16} } } } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = SearchQuery(Term(f.user, 'kimchy'), _compiler=QueryCompiled20).filter(f.age >= 16) self.assert_expression( sq, { "query": { "bool": { "must": { "term": {"user": "******"} }, "filter": { "range": { "age": {"gte": 16} } } } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery(Term(f.user, 'kimchy')) .query(f.user != 'kimchy') ) self.assert_expression( sq, { "query": { "bool": { "must_not": [ { "term": {"user": "******"} } ] } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery(Term(f.user, 'kimchy')) .query(None) ) self.assert_expression(sq, {}) self.assertEqual(collect_doc_classes(sq), set()) sq = ( SearchQuery(Term(f.user, 'kimchy')) .filter(f.age >= 16) .filter(f.lang == 'English') ) self.assert_expression( sq, { "query": { "filtered": { "query": { "term": {"user": "******"} }, "filter": { "bool": { "must": [ { "range": { "age": {"gte": 16} } }, { "term": { "lang": "English" } } ] } } } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .order_by( f.opinion_rating.desc(missing='_last'), f.opinion_count.desc(), f.id ) ) self.assert_expression( sq, { "sort": [ { "opinion_rating": { "order": "desc", "missing": "_last" } }, { "opinion_count": "desc" }, "id" ] } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .order_by( f.opinion_rating.desc(missing='_last'), f.opinion_count.desc(), f.id ) .order_by(None) .order_by(None) ) self.assert_expression(sq, {}) self.assertEqual(collect_doc_classes(sq), set()) sq = SearchQuery().source(f.name, f.company) self.assert_expression( sq, { "_source": ["name", "company"] } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = SearchQuery().source(exclude=[f.name, f.company]) self.assert_expression( sq, { "_source": { "exclude": ["name", "company"] } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .source( include=[f.obj1.wildcard('*'), f.obj2.wildcard('*')], # FIXME: f.wildcard('*') exclude=DynamicDocument.wildcard('*').description ) ) self.assert_expression( sq, { "_source": { "include": ["obj1.*", "obj2.*"], "exclude": "*.description" } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .source(None) .source(f.name, f.company) .source(None) ) self.assert_expression(sq, {}) self.assertEqual(collect_doc_classes(sq), set()) sq = ( SearchQuery() .source(f.name, f.company) .source(False) ) self.assert_expression( sq, { "_source": False } ) self.assertEqual(collect_doc_classes(sq), set()) sq = ( SearchQuery() .source(True) ) self.assert_expression( sq, { "_source": True } ) self.assertEqual(collect_doc_classes(sq), set()) sq = SearchQuery().fields(f.name, f.company) self.assert_expression( sq, { "fields": ["name", "company"] } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .fields(True) ) self.assert_expression( sq, { "fields": '*' } ) self.assertEqual(collect_doc_classes(sq), set()) sq = ( SearchQuery() .fields(None) .fields(f.name, f.company) .fields(None) ) self.assert_expression(sq, {}) self.assertEqual(collect_doc_classes(sq), set()) sq = ( SearchQuery() .fields(f.name, f.company) .fields(False) ) self.assert_expression( sq, { "fields": [] } ) self.assertEqual(collect_doc_classes(sq), set()) self.assert_expression( SearchQuery() .function_score({'random_score': {"seed": 1234}}), { "query": { "function_score": { "functions": [ { "random_score": {"seed": 1234} } ], } } } ) sq = ( SearchQuery(MultiMatch('Iphone 6', fields=[f.name, f.description])) .filter(f.status == 0) .function_score(None) .function_score({'_score': {"seed": 1234}}) .function_score(None) .function_score({'field_value_factor': {'field': f.popularity, 'factor': 1.2, 'modifier': 'sqrt'}}, boost_mode='sum') .function_score({'boost_factor': 3, 'filter': f.region == 12}) ) self.assert_expression( sq, { "query": { "filtered": { "query": { "function_score": { "query": { "multi_match": { "query": "Iphone 6", "fields": ["name", "description"] } }, "functions": [ { "field_value_factor": { "field": "popularity", "factor": 1.2, "modifier": "sqrt" } }, { "filter": { "term": {"region": 12} }, "boost_factor": 3 } ], "boost_mode": "sum" } }, "filter": { "term": {"status": 0} } } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .filter(f.status == 0) .boost_score( {'filter': f.discount_percent >= 10, 'weight': 1000}, {'filter': f.discount_percent >= 50, 'weight': 2000}, {'filter': f.presence == 'available', 'weight': 10000}, ) ) self.assert_expression( sq, { "query": { "filtered": { "query": { "function_score": { "functions": [ { "filter": {"range": {"discount_percent": {"gte": 10}}}, "weight": 1000 }, { "filter": {"range": {"discount_percent": {"gte": 50}}}, "weight": 2000 }, { "filter": {"term": {"presence": "available"}}, "weight": 10000 }, ], "score_mode": "sum", "boost_mode": "sum" } }, "filter": { "term": {"status": 0} } } } } ) sq = ( SearchQuery(f.name.match('test')) .filter(f.status == 0) .function_score( {'field_value_factor': {'field': f.popularity}}, ) .boost_score( {'filter': f.discount_percent >= 10, 'weight': 100}, ) .boost_score(None) .boost_score( {'filter': f.discount_percent >= 10, 'weight': 1000}, {'filter': f.discount_percent >= 50, 'weight': 2000}, score_mode='max', ) ) self.assert_expression( sq, { "query": { "filtered": { "query": { "function_score": { "query": { "function_score": { "query": { "match": { "name": "test" } }, "functions": [ { "field_value_factor": { "field": "popularity" } } ] } }, "functions": [ { "filter": {"range": {"discount_percent": {"gte": 10}}}, "weight": 1000 }, { "filter": {"range": {"discount_percent": {"gte": 50}}}, "weight": 2000 }, ], "score_mode": "max", "boost_mode": "sum" } }, "filter": { "term": {"status": 0} } } } } ) sq = ( SearchQuery() .rescore( QueryRescorer( self.index.t.field1.match('the quick brown', type='phrase', slop=2) ) ) .rescore(None) .rescore( QueryRescorer( self.index.t.field1.match('the quick brown fox', type='phrase', slop=2), query_weight=0.7, rescore_query_weight=1.2 ), window_size=100, ) .rescore( QueryRescorer( FunctionScore(script_score={'script': "log10(doc['numeric'].value + 2)"}), score_mode='multiply' ), window_size=10, ) ) self.assert_expression( sq, { "rescore": [ { "window_size": 100, "query": { "rescore_query": { "match": { "field1": { "query": "the quick brown fox", "type": "phrase", "slop": 2 } } }, "query_weight": 0.7, "rescore_query_weight": 1.2 } }, { "window_size": 10, "query": { "score_mode": "multiply", "rescore_query": { "function_score": { "script_score": { "script": "log10(doc['numeric'].value + 2)" } } } } } ] } ) self.assertEqual(collect_doc_classes(sq), {self.index.t}) sq = SearchQuery().post_filter(self.index.shirt.color == 'red') self.assert_expression( sq, { "post_filter": { "term": {"color": "red"} } } ) self.assertEqual(collect_doc_classes(sq), {self.index.shirt}) sq = ( SearchQuery() .filter(self.index.shirt.brand == 'gucci') .post_filter(self.index.shirt.color == 'red') .post_filter(self.index.shirt.model == 't-shirt') ) self.assert_expression( sq, { "query": { "filtered": { "filter": { "term": {"brand": "gucci"} } } }, "post_filter": { "bool": { "must": [ {"term": {"color": "red"}}, {"term": {"model": "t-shirt"}} ] } } } ) self.assertEqual(collect_doc_classes(sq), {self.index.shirt})
def test_aggs(self): f = DynamicDocument.fields a = agg.AggExpression() self.assertRaises(NotImplementedError, a.build_agg_result, {}) a = agg.Avg(f.price) self.assert_expression( a, { "avg": {"field": "price"} } ) res = a.build_agg_result({ 'value': 75.3 }) self.assertAlmostEqual(res.value, 75.3) res = a.build_agg_result({ 'value': None }) self.assertIs(res.value, None) aa = a.clone() self.assertIsNot(a, aa) self.assertEqual(a.__visit_name__, aa.__visit_name__) self.assertEqual(a.params, aa.params) a = agg.Min(f.price) self.assert_expression( a, { "min": {"field": "price"} } ) res = a.build_agg_result({ 'value': 38 }) self.assertAlmostEqual(res.value, 38) res = a.build_agg_result({ 'value': 1297167619690, 'value_as_string': '2011-02-08T12:20:19.690Z' }) self.assertAlmostEqual(res.value, 1297167619690) a = agg.Max(f.price) self.assert_expression( a, { "max": {"field": "price"} } ) res = a.build_agg_result({ 'value': 45693.5 }) self.assertAlmostEqual(res.value, 45693.5) a = agg.Stats(f.grade) self.assert_expression( a, { "stats": {"field": "grade"} } ) a = a.build_agg_result( { "count": 6, "min": 60, "max": 98, "avg": 78.5, "sum": 471 } ) self.assertEqual(a.count, 6) self.assertEqual(a.min, 60) self.assertEqual(a.max, 98) self.assertAlmostEqual(a.avg, 78.5) self.assertEqual(a.sum, 471) a = agg.ExtendedStats(f.grade) self.assert_expression( a, { "extended_stats": {"field": "grade"} } ) a = a.build_agg_result( { "count": 6, "min": 72, "max": 117.6, "avg": 94.2, "sum": 565.2, "sum_of_squares": 54551.51999999999, "variance": 218.2799999999976, "std_deviation": 14.774302013969987 } ) self.assertEqual(a.count, 6) self.assertEqual(a.min, 72) self.assertAlmostEqual(a.max, 117.6) self.assertAlmostEqual(a.avg, 94.2) self.assertAlmostEqual(a.sum, 565.2) self.assertAlmostEqual(a.sum_of_squares, 54551.51999999999) self.assertAlmostEqual(a.variance, 218.2799999999976) self.assertAlmostEqual(a.std_deviation, 14.774302013969987) percentiles_agg = agg.Percentiles(f.load_time, percents=[95, 99, 99.9]) self.assert_expression( percentiles_agg, { "percentiles": { "field": "load_time", "percents": [95, 99, 99.9] } } ) a = percentiles_agg.build_agg_result( { "values": { "95.0": 60, "99.0": 150, "99.9": 153, } } ) self.assertEqual( a.values, [(95.0, 60), (99.0, 150), (99.9, 153)], ) self.assertEqual(a.get_value(95), 60) self.assertEqual(a.get_value(95.0), 60) self.assertEqual(a.get_value(99), 150) self.assertEqual(a.get_value(99.0), 150) self.assertEqual(a.get_value(99.9), 153) a = percentiles_agg.build_agg_result( { "values": { "95.0": 60, "95.0_as_string": "60", "99.0": 150, "99.0_as_string": "150", "99.9": 153, "99.9_as_string": "153", } } ) self.assertEqual( a.values, [(95.0, 60), (99.0, 150), (99.9, 153)], ) self.assertEqual(a.get_value(95), 60) self.assertEqual(a.get_value(95.0), 60) self.assertEqual(a.get_value(99), 150) self.assertEqual(a.get_value(99.0), 150) self.assertEqual(a.get_value(99.9), 153) percentiles_agg = agg.Percentiles(f.load_time, percents=[50]) self.assert_expression( percentiles_agg, { "percentiles": { "field": "load_time", "percents": [50] } } ) a = percentiles_agg.build_agg_result( { "values": { "50.0": "NaN", } } ) self.assertEqual( len(a.values), 1 ) self.assertAlmostEqual( a.values[0][0], 50.0 ) self.assertTrue( math.isnan(a.values[0][1]) ) self.assertTrue(math.isnan(a.get_value(50))) self.assertTrue(math.isnan(a.get_value(50.0))) ranks_agg = agg.PercentileRanks(f.load_time, values=[14.8, 30]) self.assert_expression( ranks_agg, { "percentile_ranks": { "field": "load_time", "values": [14.8, 30.0] } } ) a = ranks_agg.build_agg_result( { "values": { "14.8": 12.32, "30": 100, } } ) self.assertEqual( a.values, [(14.8, 12.32), (30.0, 100)], ) self.assertEqual( a.values, [(14.8, 12.32), (30.0, 100)], ) self.assertAlmostEqual(a.get_percent(14.8), 12.32) self.assertAlmostEqual(a.get_percent(13.7 + 1.1), 12.32) self.assertAlmostEqual(a.get_percent(30), 100.0) self.assertAlmostEqual(a.get_percent(30.0), 100.0) a = ranks_agg.build_agg_result( { "values": { "14.8": 12.32, "14.8_as_string": "12.32", "30": 100, "30_as_string": "100", } } ) self.assertEqual( a.values, [(14.8, 12.32), (30.0, 100)], ) self.assertEqual( a.values, [(14.8, 12.32), (30.0, 100)], ) self.assertAlmostEqual(a.get_percent(14.8), 12.32) self.assertAlmostEqual(a.get_percent(13.7 + 1.1), 12.32) self.assertAlmostEqual(a.get_percent(30), 100.0) self.assertAlmostEqual(a.get_percent(30.0), 100.0) a = agg.Cardinality(f.author, precision_threshold=100) self.assert_expression( a, { "cardinality": { "field": "author", "precision_threshold": 100 } } ) a = a.build_agg_result( { "value": 184 } ) self.assertEqual(a.value, 184) a = agg.Global() self.assert_expression(a, {"global": {}}) a = a.build_agg_result( {"doc_count": 185} ) self.assertEqual(a.doc_count, 185) a = agg.Filter(f.company == 1) self.assert_expression(a, {"filter": {"term": {"company": 1}}}) a2 = a.clone() self.assertIsNot(a, a2) self.assert_expression(a2, {"filter": {"term": {"company": 1}}}) a = a.build_agg_result( {"doc_count": 148} ) self.assertEqual(a.doc_count, 148) a = agg.Terms(f.status) self.assert_expression( a, { "terms": {"field": "status"} } ) a1 = a.clone() self.assertIsNot(a, a1) a = a.build_agg_result( { 'buckets': [ {'doc_count': 7353499, 'key': 0}, {'doc_count': 2267139, 'key': 1}, {'doc_count': 1036951, 'key': 4}, {'doc_count': 438384, 'key': 2}, {'doc_count': 9594, 'key': 3}, {'doc_count': 46, 'key': 5} ] } ) self.assertEqual(len(a.buckets), 6) self.assertEqual(list(iter(a)), a.buckets) self.assertEqual(a.buckets[0].key, 0) self.assertEqual(a.buckets[0].doc_count, 7353499) self.assertEqual(repr(a.buckets[0]), '<Bucket key=0 doc_count=7353499>') self.assertIs(a.buckets[0], a.get_bucket(0)) self.assertEqual(a.buckets[1].key, 1) self.assertEqual(a.buckets[1].doc_count, 2267139) self.assertIs(a.buckets[1], a.get_bucket(1)) self.assertEqual(repr(a.buckets[1]), '<Bucket key=1 doc_count=2267139>') self.assertEqual(a.buckets[2].key, 4) self.assertEqual(a.buckets[2].doc_count, 1036951) self.assertIs(a.buckets[2], a.get_bucket(4)) self.assertEqual(repr(a.buckets[2]), '<Bucket key=4 doc_count=1036951>') self.assertEqual(a.buckets[3].key, 2) self.assertEqual(a.buckets[3].doc_count, 438384) self.assertIs(a.buckets[3], a.get_bucket(2)) self.assertEqual(repr(a.buckets[3]), '<Bucket key=2 doc_count=438384>') self.assertEqual(a.buckets[4].key, 3) self.assertEqual(a.buckets[4].doc_count, 9594) self.assertIs(a.buckets[4], a.get_bucket(3)) self.assertEqual(repr(a.buckets[4]), '<Bucket key=3 doc_count=9594>') self.assertEqual(a.buckets[5].key, 5) self.assertEqual(a.buckets[5].doc_count, 46) self.assertIs(a.buckets[5], a.get_bucket(5)) self.assertEqual(repr(a.buckets[5]), '<Bucket key=5 doc_count=46>') a = agg.Terms(f.is_visible, type=Boolean) self.assert_expression( a, { "terms": {"field": "is_visible"} } ) a = a.build_agg_result( { 'buckets': [ {'doc_count': 7, 'key': 'T'}, {'doc_count': 2, 'key': 'F'}, ] } ) self.assertEqual(len(a.buckets), 2) self.assertEqual(a.buckets[0].key, True) self.assertEqual(a.buckets[0].doc_count, 7) self.assertIs(a.buckets[0], a.get_bucket(True)) self.assertEqual(a.buckets[1].key, False) self.assertEqual(a.buckets[1].doc_count, 2) self.assertIs(a.buckets[1], a.get_bucket(False)) a = agg.Terms(f.category, type=List(Integer)) self.assert_expression( a, { "terms": {"field": "category"} } ) a = a.build_agg_result( { 'buckets': [ {'doc_count': 792, 'key': 28}, {'doc_count': 185, 'key': 3}, ] } ) self.assertEqual(len(a.buckets), 2) self.assertEqual(a.buckets[0].key, 28) self.assertEqual(a.buckets[0].doc_count, 792) self.assertIs(a.buckets[0], a.get_bucket(28)) self.assertEqual(a.buckets[1].key, 3) self.assertEqual(a.buckets[1].doc_count, 185) self.assertIs(a.buckets[1], a.get_bucket(3)) class ProductDocument(Document): is_visible = Field(Boolean) a = agg.Terms(ProductDocument.is_visible) self.assert_expression( a, { "terms": {"field": "is_visible"} } ) a = a.build_agg_result( { 'buckets': [ {'doc_count': 7, 'key': 'T'}, {'doc_count': 2, 'key': 'F'}, ] } ) self.assertEqual(len(a.buckets), 2) self.assertEqual(a.buckets[0].key, True) self.assertEqual(a.buckets[0].doc_count, 7) self.assertIs(a.buckets[0], a.get_bucket(True)) self.assertEqual(a.buckets[1].key, False) self.assertEqual(a.buckets[1].doc_count, 2) self.assertIs(a.buckets[1], a.get_bucket(False)) a = agg.SignificantTerms(f.crime_type) self.assert_expression( a, { "significant_terms": {"field": "crime_type"} } ) a = a.build_agg_result( { "doc_count": 47347, "buckets" : [ { "key": "Bicycle theft", "doc_count": 3640, "score": 0.371, "bg_count": 66799, }, { "key": "Mobile phone theft", "doc_count": 27617, "score": 0.0599, "bg_count": 53182, } ] } ) self.assertEqual(len(a.buckets), 2) self.assertEqual(a.buckets[0].key, 'Bicycle theft') self.assertEqual(a.buckets[0].doc_count, 3640) self.assertAlmostEqual(a.buckets[0].score, 0.371) self.assertEqual(a.buckets[0].bg_count, 66799) self.assertIs(a.buckets[0], a.get_bucket('Bicycle theft')) self.assertEqual(a.buckets[1].key, 'Mobile phone theft') self.assertEqual(a.buckets[1].doc_count, 27617) self.assertAlmostEqual(a.buckets[1].score, 0.0599) self.assertEqual(a.buckets[1].bg_count, 53182) self.assertIs(a.buckets[1], a.get_bucket('Mobile phone theft')) a = agg.Range( f.price, ranges=[{'to': 200}, {'from': 200, 'to': 1000}, {'from': 1000}], type=Integer, ) self.assert_expression( a, { "range": { "field": "price", "ranges": [ {"to": 200}, {"from": 200, "to": 1000}, {"from": 1000} ] } } ) a1 = a.clone() self.assertIsNot(a1, a) a = a.build_agg_result( { "buckets": [ { "to": 200, "doc_count": 12 }, { "from": 200, "to": 1000, "doc_count": 197 }, { "from": 1000, "doc_count": 8 } ] } ) self.assertEqual(len(a.buckets), 3) self.assertEqual(a.buckets[0].doc_count, 12) self.assertEqual(a.buckets[1].doc_count, 197) self.assertEqual(a.buckets[2].doc_count, 8) a = agg.Filters([Term(f.body, 'error'), Term(f.body, 'warning')]) self.assert_expression( a, { "filters": { "filters": [ {"term": {"body": "error"}}, {"term": {"body": "warning"}} ] } } ) a = a.build_agg_result( { "buckets": [ { "doc_count" : 34 }, { "doc_count" : 439 }, ] } ) self.assertEqual(len(a.buckets), 2) self.assertIs(a.buckets[0].key, None) self.assertEqual(a.buckets[0].doc_count, 34) self.assertIs(a.buckets[1].key, None) self.assertEqual(a.buckets[1].doc_count, 439) self.assertIs(a.get_bucket(None), None) a = agg.Filters(Params(errors=Term(f.body, 'error'), warnings=Term(f.body, 'warning'))) self.assert_expression( a, { "filters": { "filters": { "errors": {"term": {"body": "error"}}, "warnings": {"term": {"body": "warning"}} } } } ) a = a.build_agg_result( { "buckets": { "errors": { "doc_count" : 34 }, "warnings": { "doc_count" : 439 }, } } ) self.assertEqual(len(a.buckets), 2) self.assertIs(a.buckets[0].key, 'errors') self.assertEqual(a.buckets[0].doc_count, 34) self.assertIs(a.buckets[0], a.get_bucket('errors')) self.assertIs(a.buckets[1].key, 'warnings') self.assertEqual(a.buckets[1].doc_count, 439) self.assertIs(a.buckets[1], a.get_bucket('warnings')) a = agg.Nested(f.resellers, aggs={'min_price': agg.Min(f.resellers.price)}) self.assert_expression( a, { "nested": {"path": "resellers"}, "aggregations": { "min_price": {"min": {"field": "resellers.price"}} } } ) a = a.build_agg_result( { "min_price": { "value" : 350 } } ) self.assertEqual(a.get_aggregation('min_price').value, 350) a = agg.Nested( f.resellers, aggs={"resellers": agg.Terms( f.resellers.id, aggs={"reverse": agg.ReverseNested( f.resellers, aggs={"reseller_volume": agg.Sum(f.price)} )})}) self.assert_expression( a, { "nested": {"path": "resellers"}, "aggregations": { "resellers": { "terms": {"field": "resellers.id"}, "aggregations": { "reverse": { "reverse_nested": {"path": "resellers"}, "aggregations": { "reseller_volume": { "sum": {"field": "price"} } } } } } } } ) a = a.build_agg_result( { "doc_count": 100, "resellers": { "buckets": [ { "key": 1122, "doc_count": 48, "reverse": { "reseller_volume": {"value": 500100} } }, { "key": 2233, "doc_count": 52, "reverse": { "reseller_volume": {"value": 100500} } } ] } } ) self.assertEqual( a .get_aggregation("resellers") .buckets[1] .get_aggregation("reverse") .get_aggregation("reseller_volume") .value, 100500 ) a = agg.Sampler(shard_size=1000, aggs={'avg_price': agg.Avg(f.price)}) self.assert_expression( a, { "sampler": {"shard_size": 1000}, "aggregations": { "avg_price": {"avg": {"field": "price"}} } } ) a = a.build_agg_result( { "doc_count": 1000, "avg_price": { "value" : 750 } } ) self.assertEqual(a.doc_count, 1000) self.assertEqual(a.get_aggregation('avg_price').value, 750) # complex aggregation with sub aggregations a = agg.Global() a = a.aggs({ 'selling_type': agg.Terms( f.selling_type, aggs={ 'price_avg': agg.Avg(f.price), 'price_min': agg.Min(f.price), 'price_max': agg.Max(f.price), 'price_hist': agg.Histogram(f.price, interval=50), } ), 'price_avg': agg.Avg(f.price), } ) self.assert_expression( a, { "global": {}, "aggregations": { "selling_type": { "terms": {"field": "selling_type"}, "aggregations": { "price_avg": {"avg": {"field": "price"}}, "price_min": {"min": {"field": "price"}}, "price_max": {"max": {"field": "price"}}, "price_hist": { "histogram": { "field": "price", "interval": 50, 'min_doc_count': 1 } }, } }, "price_avg": {"avg": {"field": "price"}} } } ) a = a.build_agg_result( { 'doc_count': 100, 'selling_type': { 'buckets': [ { 'key': 'retail', 'doc_count': 70, 'price_avg': {'value': 60.5}, 'price_min': {'value': 1.1}, 'price_max': {'value': 83.4}, 'price_hist': { 'buckets': [ {'key': 50, 'doc_count': 60}, {'key': 100, 'doc_count': 7}, {'key': 150, 'doc_count': 3}, ] }, }, { 'key': 'wholesale', 'doc_count': 30, 'price_avg': {'value': 47.9}, 'price_min': {'value': 20.1}, 'price_max': {'value': 64.8}, 'price_hist': { 'buckets': [ {'key': 0, 'doc_count': 17}, {'key': 50, 'doc_count': 5}, {'key': 100, 'doc_count': 6}, {'key': 150, 'doc_count': 2}, ] }, }, ], }, 'price_avg': {'value': 56.3}, } ) self.assertEqual(a.doc_count, 100) type_agg = a.get_aggregation('selling_type') self.assertEqual(len(type_agg.buckets), 2) self.assertEqual(type_agg.buckets[0].key, 'retail') self.assertEqual(type_agg.buckets[0].doc_count, 70) self.assertIs(type_agg.buckets[0], type_agg.get_bucket('retail')) self.assertAlmostEqual(type_agg.buckets[0].get_aggregation('price_avg').value, 60.5) self.assertAlmostEqual(type_agg.buckets[0].get_aggregation('price_min').value, 1.1) self.assertAlmostEqual(type_agg.buckets[0].get_aggregation('price_max').value, 83.4) price_hist_agg = type_agg.buckets[0].get_aggregation('price_hist') self.assertEqual(price_hist_agg.buckets[0].key, 50) self.assertEqual(price_hist_agg.buckets[0].doc_count, 60) self.assertIs(price_hist_agg.buckets[0], price_hist_agg.get_bucket(50)) self.assertEqual(price_hist_agg.buckets[1].key, 100) self.assertEqual(price_hist_agg.buckets[1].doc_count, 7) self.assertIs(price_hist_agg.buckets[1], price_hist_agg.get_bucket(100)) self.assertEqual(price_hist_agg.buckets[2].key, 150) self.assertEqual(price_hist_agg.buckets[2].doc_count, 3) self.assertIs(price_hist_agg.buckets[2], price_hist_agg.get_bucket(150)) self.assertEqual(len(price_hist_agg.buckets), 3) self.assertEqual(type_agg.buckets[1].key, 'wholesale') self.assertEqual(type_agg.buckets[1].doc_count, 30) self.assertIs(type_agg.buckets[1], type_agg.get_bucket('wholesale')) self.assertAlmostEqual(type_agg.buckets[1].get_aggregation('price_avg').value, 47.9) self.assertAlmostEqual(type_agg.buckets[1].get_aggregation('price_min').value, 20.1) self.assertAlmostEqual(type_agg.buckets[1].get_aggregation('price_max').value, 64.8) price_hist_agg = type_agg.buckets[1].get_aggregation('price_hist') self.assertEqual(len(price_hist_agg.buckets), 4) self.assertEqual(price_hist_agg.buckets[0].key, 0) self.assertEqual(price_hist_agg.buckets[0].doc_count, 17) self.assertIs(price_hist_agg.buckets[0], price_hist_agg.get_bucket(0)) self.assertEqual(price_hist_agg.buckets[1].key, 50) self.assertEqual(price_hist_agg.buckets[1].doc_count, 5) self.assertIs(price_hist_agg.buckets[1], price_hist_agg.get_bucket(50)) self.assertEqual(price_hist_agg.buckets[2].key, 100) self.assertEqual(price_hist_agg.buckets[2].doc_count, 6) self.assertIs(price_hist_agg.buckets[2], price_hist_agg.get_bucket(100)) self.assertEqual(price_hist_agg.buckets[3].key, 150) self.assertEqual(price_hist_agg.buckets[3].doc_count, 2) self.assertIs(price_hist_agg.buckets[3], price_hist_agg.get_bucket(150)) self.assertEqual(a.get_aggregation('price_avg').value, 56.3) class QuestionDocument(DynamicDocument): pass class PaperDocument(DynamicDocument): pass question_mapper = Mock( return_value={ '602679': Mock(id=602679, type='question'), '602678': Mock(id=602678, type='question'), } ) paper_mapper = Mock(return_value={'602672': Mock(id=602672, type='paper')}) top_hits_agg = agg.Terms( f.tags, size=3, aggs={ 'top_tags_hits': agg.TopHits( size=1, sort=f.last_activity_date.desc(), _source={'include': f.title}, instance_mapper={ QuestionDocument: question_mapper, PaperDocument: paper_mapper }, ) } ) self.assert_expression( top_hits_agg, { "terms": { "field": "tags", "size": 3 }, "aggregations": { "top_tags_hits": { "top_hits": { "sort": { "last_activity_date": "desc" }, "_source": { "include": "title" }, "size" : 1 } } } } ) a = top_hits_agg.build_agg_result( { "buckets": [ { "key": "windows-7", "doc_count": 25365, "top_tags_hits": { "hits": { "total": 25365, "max_score": 1, "hits": [ { "_index": "stack", "_type": "question", "_id": "602679", "_score": 1, "_source": { "title": "Windows port opening" }, "sort": [ 1370143231177 ] } ] } } }, { "key": "linux", "doc_count": 18342, "top_tags_hits": { "hits": { "total": 18342, "max_score": 1, "hits": [ { "_index": "stack", "_type": "paper", "_id": "602672", "_score": 1, "_source": { "title": "Ubuntu RFID Screensaver lock-unlock" }, "sort": [ 1370143379747 ] } ] } } }, { "key": "windows", "doc_count": 18119, "top_tags_hits": { "hits": { "total": 18119, "max_score": 1, "hits": [ { "_index": "stack", "_type": "question", "_id": "602678", "_score": 1, "_source": { "title": "If I change my computers date / time, what could be affected?" }, "sort": [ 1370142868283 ] } ] } } } ] }, doc_cls_map={'question': QuestionDocument, 'paper': PaperDocument}, mapper_registry={}, ) self.assertEqual(len(a.buckets), 3) self.assertEqual(a.buckets[0].doc_count, 25365) self.assertEqual(a.buckets[0].key, 'windows-7') top_tags_agg = a.buckets[0].get_aggregation('top_tags_hits') self.assertEqual(top_tags_agg.total, 25365) self.assertEqual(top_tags_agg.max_score, 1) self.assertEqual(len(top_tags_agg.hits), 1) self.assertIsInstance(top_tags_agg.hits[0], QuestionDocument) self.assertEqual(top_tags_agg.hits[0]._index, 'stack') self.assertEqual(top_tags_agg.hits[0]._type, 'question') self.assertEqual(top_tags_agg.hits[0]._score, 1) self.assertEqual(top_tags_agg.hits[0]._id, '602679') self.assertEqual(top_tags_agg.hits[0].title, 'Windows port opening') self.assertEqual(top_tags_agg.hits[0].instance.id, 602679) self.assertEqual(top_tags_agg.hits[0].instance.type, 'question') self.assertEqual(a.buckets[1].doc_count, 18342) self.assertEqual(a.buckets[1].key, 'linux') top_tags_agg = a.buckets[1].get_aggregation('top_tags_hits') self.assertEqual(top_tags_agg.total, 18342) self.assertEqual(top_tags_agg.max_score, 1) self.assertEqual(len(top_tags_agg.hits), 1) self.assertIsInstance(top_tags_agg.hits[0], PaperDocument) self.assertEqual(top_tags_agg.hits[0]._index, 'stack') self.assertEqual(top_tags_agg.hits[0]._type, 'paper') self.assertEqual(top_tags_agg.hits[0]._score, 1) self.assertEqual(top_tags_agg.hits[0]._id, '602672') self.assertEqual(top_tags_agg.hits[0].title, 'Ubuntu RFID Screensaver lock-unlock') self.assertEqual(top_tags_agg.hits[0].instance.id, 602672) self.assertEqual(top_tags_agg.hits[0].instance.type, 'paper') self.assertEqual(a.buckets[2].doc_count, 18119) self.assertEqual(a.buckets[2].key, 'windows') top_tags_agg = a.buckets[2].get_aggregation('top_tags_hits') self.assertEqual(top_tags_agg.total, 18119) self.assertEqual(top_tags_agg.max_score, 1) self.assertEqual(len(top_tags_agg.hits), 1) self.assertIsInstance(top_tags_agg.hits[0], DynamicDocument) self.assertEqual(top_tags_agg.hits[0]._index, 'stack') self.assertEqual(top_tags_agg.hits[0]._type, 'question') self.assertEqual(top_tags_agg.hits[0]._score, 1) self.assertEqual(top_tags_agg.hits[0]._id, '602678') self.assertEqual(top_tags_agg.hits[0].title, 'If I change my computers date / time, what could be affected?') self.assertEqual(top_tags_agg.hits[0].instance.id, 602678) self.assertEqual(top_tags_agg.hits[0].instance.type, 'question') self.assertEqual(question_mapper.call_count, 1) self.assertEqual(paper_mapper.call_count, 1)
def test_expression(self): f = DynamicDocument.fields e = Params({'foo': 'bar'}) self.assert_expression( e, {"foo": "bar"} ) self.assertEqual(e['foo'], 'bar') self.assertTrue('foo' in e) self.assert_expression( Match(f.message, 'this is a test'), { "match": { "message": "this is a test", } } ) self.assert_expression( Match( f.message, 'this is a test', minimum_should_match='100%', cutoff_frequency=0.001, boost=2.1 ), { "match": { "message": { "query": "this is a test", "minimum_should_match": "100%", "cutoff_frequency": 0.001, "boost": 2.1, } } } ) self.assert_expression( Term(f.user, 'kimchy'), { "term": {"user": "******"} } ) self.assert_expression( Term(f.user, 'kimchy', boost=1.2), { "term": {"user": {"value": "kimchy", "boost": 1.2}} } ) self.assert_expression( Term('user.login', 'kimchy'), { "term": {"user.login": "******"} } ) self.assert_expression( Terms(f.status, [0]), { "terms": { "status": [0] } } ) self.assert_expression( Terms(f.tags, ['blue', 'pill'], minimum_should_match=1), { "terms": { "tags": ["blue", "pill"], "minimum_should_match": 1 } } ) self.assert_expression( Exists(f.tags), { "exists": {"field": "tags"} } ) self.assert_expression( Missing(f.tags, _cache=True), { "missing": { "field": "tags", "_cache": True } } ) self.assert_expression( Bool( must=Term(f.user, 'kimchy'), filter=Term(f.tag, 'tech'), must_not=Range(f.age, from_=10, to=20), should=[Term(f.tag, 'wow'), Term(f.tag, 'elasticsearch', boost=2.1)], minimum_should_match=1, boost=1.0, ), { "bool": { "must": { "term": {"user": "******"} }, "filter": { "term": {"tag": "tech"} }, "must_not": { "range": { "age": {"from": 10, "to": 20} } }, "should": [ { "term": {"tag": "wow"} }, { "term": {"tag": {"value": "elasticsearch", "boost": 2.1}} } ], "minimum_should_match": 1, "boost": 1.0 } } ) e = MultiMatch( "Will Smith", [self.index.star.title.boost(4), self.index.star.wildcard('*_name').boost(2)], minimum_should_match='100%' ) self.assert_expression( e, { "multi_match": { "query": "Will Smith", "fields": ["title^4", "*_name^2"], "minimum_should_match": "100%" } } ) self.assertEqual( e._collect_doc_classes(), {self.index.star} ) self.assert_expression( Range(self.index.product.price, lte=100, boost=2.2, execution='index', _cache=False), { "range": { "price": {"lte": 100, "boost": 2.2}, "execution": "index", "_cache": False } } ) self.assert_expression( Boosting( positive=Term(f.field1, 'value1'), negative=Term(f.field2, 'value2'), negative_boost=0.2 ), { "boosting": { "positive": { "term": { "field1": "value1" } }, "negative": { "term": { "field2": "value2" } }, "negative_boost": 0.2 } } ) self.assert_expression( Common( f.body, 'nelly the elephant not as a cartoon', cutoff_frequency=0.001, minimum_should_match=dict(low_freq=2, high_freq=3), ), { "common": { "body": { "query": "nelly the elephant not as a cartoon", "cutoff_frequency": 0.001, "minimum_should_match": { "low_freq": 2, "high_freq": 3 } } } } ) self.assert_expression( ConstantScore(filter=Term(f.user, 'kimchy'), boost=1.2), { "constant_score": { "filter": { "term": { "user": "******"} }, "boost": 1.2 } } ) self.assert_expression( FunctionScore( query=MatchAll(), field_value_factor={ 'field': f.popularity, 'factor': 1.2, 'modifier': 'sqrt', } ), { "function_score": { "query": {"match_all": {}}, "field_value_factor": { "field": "popularity", "factor": 1.2, "modifier": "sqrt" } } } ) self.assert_expression( DisMax([Term(f.age, 34), Term(f.age, 35)], boost=1.2, tie_breaker=0.7), { "dis_max": { "tie_breaker": 0.7, "boost": 1.2, "queries": [ { "term" : { "age" : 34 } }, { "term" : { "age" : 35 } } ] } } ) self.assert_expression( Filtered( filter=Range(f.created, gte='now - 1d / d'), query=Match(f.tweet, 'full text search') ), { "filtered": { "query": { "match": { "tweet": "full text search" } }, "filter": { "range": { "created": { "gte": "now - 1d / d" }} } } } ) self.assert_expression( Ids(['123456']), { "ids": { "values": ["123456"] } } ) self.assert_expression( Ids(['1', '4', '100'], type="my_type"), { "ids": { "type": "my_type", "values": ["1", "4", "100"] } } ) self.assert_expression( Prefix(f.user, 'ki', boost=2.0), { "prefix": { "user": { "value": "ki", "boost": 2.0 } } } ) self.assert_expression( MatchAll(), {"match_all": {}} ) self.assert_expression( MatchAll(boost=1.2), { "match_all": { "boost" : 1.2 } } ) self.assert_expression( Query(Match(f.title, 'this that thus')), { "query": { "match": { "title": "this that thus" } } } ) self.assert_expression( Query(Match(f.title, 'this that thus'), _cache=True), { "fquery": { "query": { "match": { "title": "this that thus" } }, "_cache": True } } ) self.assertRaises(NotImplementedError, BooleanExpression) self.assert_expression( And( Range(f.post_date, from_='2010-03-01', to='2010-04-01'), Prefix(f.name.second, 'ba') ), { "and": [ { "range": { "post_date": { "from": "2010-03-01", "to": "2010-04-01" } } }, { "prefix" : { "name.second" : "ba" } } ] } ) self.assert_expression( And( Range(f.post_date, from_='2010-03-01', to='2010-04-01'), Prefix(f.name.second, 'ba'), _cache=True ), { "and": { "filters": [ { "range": { "post_date": { "from": "2010-03-01", "to": "2010-04-01" } } }, { "prefix" : { "name.second" : "ba" } } ], "_cache": True } } ) self.assert_expression( Or(Term(f.name.second, 'banon'), Term(f.name.nick, 'kimchy')), { "or": [ { "term": {"name.second": "banon"} }, { "term": {"name.nick": "kimchy"} } ] } ) self.assert_expression( And(Or(Term(f.name.nick, 'kimchy'))), { "term": {"name.nick": "kimchy"} } ) self.assert_expression( Not( Range(f.post_date, from_='2010-03-01', to='2010-04-01'), ), { "not": { "range": { "post_date": { "from": "2010-03-01", "to": "2010-04-01" } } } } ) self.assert_expression( Not( Range(f.post_date, from_='2010-03-01', to='2010-04-01'), _cache=True, ), { "not": { "filter": { "range": { "post_date": { "from": "2010-03-01", "to": "2010-04-01" } } }, "_cache": True } } ) self.assert_expression( Sort(f.post_date), "post_date" ) self.assert_expression( Sort(f.age, 'desc'), { "age": "desc" } ) self.assert_expression( Sort(f.price, 'asc', mode='avg'), { "price": { "order": "asc", "mode": "avg" } } ) self.assert_expression( Sort( f.offer.price.sort, 'asc', mode='avg', nested_filter=Term(f.offer.color, 'blue') ), { "offer.price.sort": { "order": "asc", "mode": "avg", "nested_filter": { "term": {"offer.color": "blue"} } } } ) self.assert_expression( SpanFirst(SpanTerm(f.user, 'kimchy'), end=3), { "span_first": { "match": { "span_term": {"user": "******"} }, "end": 3 } } ) self.assert_expression( SpanMulti(Prefix(f.user, 'ki', boost=1.08)), { "span_multi": { "match": { "prefix": { "user": {"value": "ki", "boost": 1.08} } } } } ) self.assert_expression( SpanNear( [SpanTerm(f.field, 'value1'), SpanTerm(f.field, 'value2'), SpanTerm(f.field, 'value3')], slop=12, in_order=False, collect_payloads=False, ), { "span_near": { "clauses": [ {"span_term": {"field": "value1"}}, {"span_term": {"field": "value2"}}, {"span_term": {"field": "value3"}} ], "slop": 12, "in_order": False, "collect_payloads": False } } ) self.assert_expression( SpanNot( SpanTerm(f.field1, 'hoya'), SpanNear([SpanTerm(f.field1, 'la'), SpanTerm(f.field1, 'hoya')], slop=0, in_order=True), ), { "span_not": { "include": { "span_term": {"field1": "hoya"} }, "exclude": { "span_near": { "clauses": [ {"span_term": {"field1": "la"}}, {"span_term": {"field1": "hoya"}} ], "slop": 0, "in_order": True } } } } ) self.assert_expression( SpanOr( [ SpanTerm(f.field, 'value1'), SpanTerm(f.field, 'value2'), SpanTerm(f.field, 'value3') ], boost=2, ), { "span_or": { "clauses": [ {"span_term": {"field": "value1"}}, {"span_term": {"field": "value2"}}, {"span_term": {"field": "value3"}} ], "boost": 2 } } ) self.assert_expression( Limit(1000), { "limit": { "value": 1000 } } ) e = Nested( self.index.movie.stars, Match(self.index.movie.stars.full_name, 'Will Smith'), score_mode='max', ) self.assert_expression( e, { "nested": { "path": "stars", "query": { "match": { "stars.full_name": "Will Smith" } }, "score_mode": "max" } } ) self.assertEqual( e._collect_doc_classes(), {self.index.movie} ) e = HasParent( self.index.blog.tag == 'something', parent_type=self.index.blog, score_mode='score', ) self.assert_expression( e, { "has_parent": { "parent_type": "blog", "query": { "term": { "tag": "something" } }, "score_mode": "score" } } ) self.assertEqual( e._collect_doc_classes(), set() ) e = HasParent( self.index.blog.tag == 'something', score_mode='score', ) self.assert_expression( e, { "has_parent": { "parent_type": "blog", "query": { "term": { "tag": "something" } }, "score_mode": "score" } } ) self.assertEqual( e._collect_doc_classes(), set() ) e = HasChild( self.index.blog_tag.tag == 'something', type=self.index.blog_tag, score_mode='sum', ) self.assert_expression( e, { "has_child": { "type": "blog_tag", "query": { "term": { "tag": "something" } }, "score_mode": "sum" } } ) self.assertEqual( e._collect_doc_classes(), set() ) e = HasChild( self.index.blog_tag.tag == 'something', score_mode='sum', ) self.assert_expression( e, { "has_child": { "type": "blog_tag", "query": { "term": { "tag": "something" } }, "score_mode": "sum" } } ) self.assertEqual( e._collect_doc_classes(), set() )
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
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))