def test_equal(self): q1 = {"bool": {"must": [{"term": {"field_A": 1}}, {"term": {"field_B": 2}}]}} q2 = {"bool": {"must": [{"term": {"field_B": 2}}, {"term": {"field_A": 1}}]}} non_equal_q = { "bool": {"must": [{"term": {"field_B": 2}}, {"term": {"field_A": 123}}]} } self.assertTrue(equal_queries(q1, q2)) self.assertFalse(equal_queries(q1, non_equal_q)) self.assertTrue( equal_search( { "query": q1, "sort": ["title", {"category": {"order": "desc"}}, "_score"], }, { "query": q2, "sort": ["title", {"category": {"order": "desc"}}, "_score"], }, ) ) self.assertFalse( equal_search( { "query": q1, "sort": ["title", {"category": {"order": "desc"}}, "_score"], }, { "query": q2, "sort": ["title", "_score", {"category": {"order": "desc"}}], }, ) )
def test_equal(): q1 = {"bool": {"must": [{"term": {"field_A": 1}}, {"term": {"field_B": 2}}]}} q2 = {"bool": {"must": [{"term": {"field_B": 2}}, {"term": {"field_A": 1}}]}} non_equal_q = { "bool": {"must": [{"term": {"field_B": 2}}, {"term": {"field_A": 123}}]} } assert equal_queries(q1, q2) assert not equal_queries(q1, non_equal_q) assert equal_search( {"query": q1, "sort": ["title", {"category": {"order": "desc"}}, "_score"]}, {"query": q2, "sort": ["title", {"category": {"order": "desc"}}, "_score"]}, ) assert not equal_search( {"query": q1, "sort": ["title", {"category": {"order": "desc"}}, "_score"]}, {"query": q2, "sort": ["title", "_score", {"category": {"order": "desc"}}]}, )
def test_equal_queries(self): q1 = { "bool": { "must": [{ "term": { "field_A": 1 } }, { "term": { "field_B": 2 } }] } } q2 = { "bool": { "must": [{ "term": { "field_B": 2 } }, { "term": { "field_A": 1 } }] } } non_equal_q = { "bool": { "must": [{ "term": { "field_B": 2 } }, { "term": { "field_A": 123 } }] } } self.assertTrue(equal_queries(q1, q2)) self.assertFalse(equal_queries(q1, non_equal_q))
def assertUnorderedEqual(self, first, second, msg=None): self.assertIsInstance(first, dict, msg) self.assertIsInstance(second, dict, msg) # preserve regular formatting if not equal_queries(first, second): self.assertEqual(first, second, msg)
def test_client_bound_response(self, uuid_mock): uuid_mock.side_effect = range(1000) client_mock = Mock(spec=["search"]) my_agg = Aggs(sample.EXPECTED_AGG_QUERY, mapping=MAPPING) response_tree = AggsResponseTree(aggs=my_agg, index=None).parse( sample.ES_AGG_RESPONSE) response = IResponse( client=client_mock, tree=response_tree, index_name="some_index", depth=1, query={"term": { "some_field": 1 }}, ) # ensure that navigation to attributes works with autocompletion (dir is used in ipython) self.assertIn("classification_type_multiclass", dir(response)) self.assertIn("classification_type_multilabel", dir(response)) multilabel = response.classification_type_multilabel self.assertIsInstance(multilabel, IResponse) self.assertIs(multilabel._initial_tree, response._tree) self.assertIn("global_metrics_field_name_gpc", dir(multilabel)) gpc = multilabel.global_metrics_field_name_gpc self.assertIsInstance(gpc, IResponse) self.assertIs(gpc._initial_tree, response._tree) # test filter query used to list documents belonging to bucket self.assertTrue( equal_queries( gpc.get_bucket_filter(), { "bool": { "must": [ { "term": { "global_metrics.field.name": { "value": "gpc" } } }, { "term": { "classification_type": { "value": "multilabel" } } }, { "term": { "some_field": { "value": 1 } } }, ] } }, ))
if genres_in is not None: q = q.must(TermsFilter("genres", terms=genres_in)) if rank_above is not None: q = q.must(Range("rank", gte=rank_above)) # we name the nested query that we would potentially use q = q.query(Nested(_name="nested_roles", path="roles")) # a compound clause (bool, nested etc..) without any children clauses is not serialized assert q.to_dict() == { "bool": { "must": [ {"terms": {"genres": ["Action", "Thriller"]}}, {"range": {"rank": {"gte": 7}}}, ] } } # we declare that those clauses must be placed below 'nested_roles' condition if filter_role_gender is not None: q = q.query(Term("roles.gender", value=filter_role_gender), parent="nested_roles") if filter_role is not None: q = q.query(Term("roles.role", value=filter_role), parent="nested_roles") assert equal_queries(q.to_dict(), expected_query) q # In[ ]:
def test_complex_example(): s = (Search().query("match", title="python").must_not( "match", title="ruby").should("term", category="meetup").should( "term", category="conference").post_filter( "terms", tags=["prague", "czech"]).script_fields( more_attendees="doc['attendees'].value + 42").groupby( "per_country", "terms", field="country").agg( "avg_attendees", "avg", field="attendees").bool( minimum_should_match=2).highlight_options( order="score").highlight("title", "body", fragment_size=50)) assert equal_queries( { "aggs": { "per_country": { "aggs": { "avg_attendees": { "avg": { "field": "attendees" } } }, "terms": { "field": "country" }, } }, "highlight": { "fields": { "body": { "fragment_size": 50 }, "title": { "fragment_size": 50 }, }, "order": "score", }, "post_filter": { "terms": { "tags": ["prague", "czech"] } }, "query": { "bool": { "minimum_should_match": 2, "must": [{ "match": { "title": { "query": "python" } } }], "must_not": [{ "match": { "title": { "query": "ruby" } } }], "should": [ { "term": { "category": { "value": "conference" } } }, { "term": { "category": { "value": "meetup" } } }, ], } }, "script_fields": { "more_attendees": { "script": "doc['attendees'].value + 42" } }, }, s.to_dict(), )