def test_aggregations(self): f = DynamicDocument.fields sq = SearchQuery().aggregations(min_price=agg.Min(f.price)) self.assert_expression( sq, { "aggregations": { "min_price": { "min": {"field": "price"} } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = SearchQuery().aggregations(genders=agg.Terms(f.gender)) self.assert_expression( sq, { "aggregations": { "genders": { "terms": {"field": "gender"} } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .aggregations( type=agg.Terms(f.type, aggs={'min_price': agg.Min(f.price)}) ) ) self.assert_expression( sq, { "aggregations": { "type": { "terms": {"field": "type"}, "aggregations": { "min_price": { "min": {"field": "price"} } } } } }, ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .aggregations( top_tags=( agg.Terms( f.tags, size=3, aggs={ 'top_tag_hits': agg.TopHits( sort=f.last_activity_date.desc(), size=1, _source=Params(include=[f.title])) } ) ) ) ) self.assert_expression( sq, { "aggregations": { "top_tags": { "terms": { "field": "tags", "size": 3 }, "aggregations": { "top_tag_hits": { "top_hits": { "sort": { "last_activity_date": "desc" }, "_source": { "include": ["title"] }, "size" : 1 } } } } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument}) sq = ( SearchQuery() .aggregations({ 'top_sites': agg.Terms( f.domain, order=Sort('top_hit', 'desc'), aggs={ 'top_tags_hits': agg.TopHits(), 'top_hit': agg.Max(script='_doc.score'), } ) }) ) self.assert_expression( sq, { "aggregations": { "top_sites": { "terms": { "field": "domain", "order": { "top_hit": "desc" } }, "aggregations": { "top_tags_hits": { "top_hits": {} }, "top_hit" : { "max": { "script": "_doc.score" } } } } } } ) self.assertEqual(collect_doc_classes(sq), {DynamicDocument})
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() )