def test_interpret_agg_string(self): some_agg = Aggs() some_agg = some_agg.aggs("some_field", insert_below=None) self.assertEqual( some_agg.to_dict(), {"some_field": {"terms": {"field": "some_field"}}} ) # with default size some_agg = Aggs() some_agg = some_agg.aggs("some_field", insert_below=None, size=10) self.assertEqual( some_agg.to_dict(), {"some_field": {"terms": {"field": "some_field", "size": 10}}}, ) # with parent some_agg = Aggs( {"root_agg_name": {"terms": {"field": "some_field", "size": 5}}} ) some_agg = some_agg.aggs("child_field", insert_below="root_agg_name") self.assertEqual( some_agg.to_dict(), { "root_agg_name": { "aggs": {"child_field": {"terms": {"field": "child_field"}}}, "terms": {"field": "some_field", "size": 5}, } }, ) # with required nested some_agg = Aggs( {"term_workflow": {"terms": {"field": "workflow", "size": 5}}}, mapping=MAPPING, nested_autocorrect=True, ) some_agg = some_agg.aggs( "local_metrics.field_class.name", insert_below="term_workflow" ) self.assertEqual( some_agg.to_dict(), { "term_workflow": { "aggs": { "nested_below_term_workflow": { "aggs": { "local_metrics.field_class.name": { "terms": {"field": "local_metrics.field_class.name"} } }, "nested": {"path": "local_metrics"}, } }, "terms": {"field": "workflow", "size": 5}, } }, )
def test_aggs(self): node = Terms(name="some_name", field="some_field", size=10) some_agg = Aggs().aggs(node, insert_below=None) self.assertEqual( some_agg.to_dict(), {"some_name": {"terms": {"field": "some_field", "size": 10}}}, ) # with parent with required nested some_agg = Aggs( {"term_workflow": {"terms": {"field": "workflow", "size": 5}}}, mapping=MAPPING, nested_autocorrect=True, ) node = Avg(name="min_local_f1", field="local_metrics.performance.test.f1_score") some_agg = some_agg.aggs(node, insert_below="term_workflow") self.assertEqual( some_agg.to_dict(), { "term_workflow": { "aggs": { "nested_below_term_workflow": { "aggs": { "min_local_f1": { "avg": { "field": "local_metrics.performance.test.f1_score" } } }, "nested": {"path": "local_metrics"}, } }, "terms": {"field": "workflow", "size": 5}, } }, )
def test_agg_insert_below(self): a1 = Aggs( Terms("A", field="A", aggs=[Terms("B", field="B"), Terms("C", field="C")]) ) self.assertEqual( a1.to_dict(), { "A": { "terms": {"field": "A"}, "aggs": { "C": {"terms": {"field": "C"}}, "B": {"terms": {"field": "B"}}, }, } }, ) self.assertEqual( a1.aggs(Terms("D", field="D"), insert_below="A").to_dict(), { "A": { "aggs": { "B": {"terms": {"field": "B"}}, "C": {"terms": {"field": "C"}}, "D": {"terms": {"field": "D"}}, }, "terms": {"field": "A"}, } }, ) self.assertEqual( a1.aggs( [Terms("D", field="D"), Terms("E", field="E")], insert_below="A" ).to_dict(), { "A": { "aggs": { "B": {"terms": {"field": "B"}}, "C": {"terms": {"field": "C"}}, "D": {"terms": {"field": "D"}}, "E": {"terms": {"field": "E"}}, }, "terms": {"field": "A"}, } }, )
def test_add_node_with_mapping(self): with_mapping = Aggs(mapping=MAPPING, nested_autocorrect=True) self.assertEqual(len(with_mapping.list()), 0) # add regular node with_mapping = with_mapping.aggs(Terms("workflow", field="workflow")) self.assertEqual( with_mapping.to_dict(), {"workflow": {"terms": {"field": "workflow"}}} ) # try to add field aggregation on non-existing field will fail with self.assertRaises(AbsentMappingFieldError): with_mapping.aggs( Terms("imaginary_agg", field="imaginary_field"), insert_below="workflow", ) self.assertEqual(len(with_mapping.list()), 1) # try to add aggregation on a non-compatible field will fail with self.assertRaises(InvalidOperationMappingFieldError): with_mapping.aggs( Avg("average_of_string", field="classification_type"), insert_below="workflow", ) self.assertEqual(len(with_mapping.list()), 1) # add field aggregation on field passing through nested will automatically add nested with_mapping = with_mapping.aggs( Avg("local_f1_score", field="local_metrics.performance.test.f1_score"), insert_below="workflow", ) self.assertEqual( with_mapping.to_dict(), { "workflow": { "aggs": { "nested_below_workflow": { "aggs": { "local_f1_score": { "avg": { "field": "local_metrics.performance.test.f1_score" } } }, "nested": {"path": "local_metrics"}, } }, "terms": {"field": "workflow"}, } }, ) self.assertIn("nested_below_workflow", with_mapping) nested_node = with_mapping.get("nested_below_workflow") self.assertEqual(nested_node.KEY, "nested") self.assertEqual(nested_node.path, "local_metrics") # add other agg requiring nested will reuse nested agg as parent with_mapping = with_mapping.aggs( Avg("local_precision", field="local_metrics.performance.test.precision"), insert_below="workflow", ) self.assertEqual( with_mapping.to_dict(), { "workflow": { "aggs": { "nested_below_workflow": { "aggs": { "local_f1_score": { "avg": { "field": "local_metrics.performance.test.f1_score" } }, "local_precision": { "avg": { "field": "local_metrics.performance.test.precision" } }, }, "nested": {"path": "local_metrics"}, } }, "terms": {"field": "workflow"}, } }, ) self.assertEqual(len(with_mapping.list()), 4) # add under a nested parent a field aggregation that requires to be located under root will automatically # add reverse-nested with_mapping = with_mapping.aggs( Terms("language_terms", field="language"), insert_below="nested_below_workflow", ) self.assertEqual(len(with_mapping.list()), 6) self.assertEqual( with_mapping.to_dict(), { "workflow": { "aggs": { "nested_below_workflow": { "aggs": { "local_f1_score": { "avg": { "field": "local_metrics.performance.test.f1_score" } }, "local_precision": { "avg": { "field": "local_metrics.performance.test.precision" } }, "reverse_nested_below_nested_below_workflow": { "aggs": { "language_terms": { "terms": {"field": "language"} } }, "reverse_nested": {}, }, }, "nested": {"path": "local_metrics"}, } }, "terms": {"field": "workflow"}, } }, )