From 9a98ad39bca2478cae9622e0ff8824e9f7564821 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 30 Apr 2024 09:46:39 +0100 Subject: [PATCH] Added "adjacency_matrix" and "top_metrics" aggregations (#1788) Fixes #1553 Fixes #1706 (cherry picked from commit ea0054f45bbf39c6f9a5534ff64654b7377a5f4b) --- elasticsearch_dsl/aggs.py | 8 ++++++++ tests/test_aggs.py | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/elasticsearch_dsl/aggs.py b/elasticsearch_dsl/aggs.py index 96b830e0..8025d1ed 100644 --- a/elasticsearch_dsl/aggs.py +++ b/elasticsearch_dsl/aggs.py @@ -197,6 +197,10 @@ class AutoDateHistogram(DateHistogram): name = "auto_date_histogram" +class AdjacencyMatrix(Bucket): + name = "adjacency_matrix" + + class DateRange(Bucket): name = "date_range" @@ -385,6 +389,10 @@ class Sum(Agg): name = "sum" +class TopMetrics(Agg): + name = "top_metrics" + + class TTest(Agg): name = "t_test" diff --git a/tests/test_aggs.py b/tests/test_aggs.py index e2848864..f2eedac4 100644 --- a/tests/test_aggs.py +++ b/tests/test_aggs.py @@ -449,3 +449,30 @@ def test_random_sampler_aggregation(): }, }, } == a.to_dict() + + +def test_adjancecy_matrix_aggregation(): + a = aggs.AdjacencyMatrix( + filters={ + "grpA": {"terms": {"accounts": ["hillary", "sidney"]}}, + "grpB": {"terms": {"accounts": ["donald", "mitt"]}}, + "grpC": {"terms": {"accounts": ["vladimir", "nigel"]}}, + } + ) + assert { + "adjacency_matrix": { + "filters": { + "grpA": {"terms": {"accounts": ["hillary", "sidney"]}}, + "grpB": {"terms": {"accounts": ["donald", "mitt"]}}, + "grpC": {"terms": {"accounts": ["vladimir", "nigel"]}}, + } + } + } == a.to_dict() + + +def test_top_metrics_aggregation(): + a = aggs.TopMetrics(metrics={"field": "m"}, sort={"s": "desc"}) + + assert { + "top_metrics": {"metrics": {"field": "m"}, "sort": {"s": "desc"}} + } == a.to_dict()