Skip to content

Commit

Permalink
Add many missing queries and aggs
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson committed Sep 16, 2020
1 parent 73b8eda commit 5d13771
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
38 changes: 38 additions & 0 deletions elasticsearch_dsl/aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ class Range(Bucket):
name = "range"


class RareTerms(Bucket):
name = "rare_terms"

def result(self, search, data):
return FieldBucketData(self, search, data)


class ReverseNested(Bucket):
name = "reverse_nested"

Expand Down Expand Up @@ -282,6 +289,13 @@ class Composite(Bucket):
}


class VariableWidthHistogram(Bucket):
name = "variable_width_histogram"

def result(self, search, data):
return FieldBucketData(self, search, data)


# metric aggregations
class TopHits(Agg):
name = "top_hits"
Expand Down Expand Up @@ -318,6 +332,10 @@ class Max(Agg):
name = "max"


class MedianAbsoluteDeviation(Agg):
name = "median_absolute_deviation"


class Min(Agg):
name = "min"

Expand All @@ -342,6 +360,10 @@ class Sum(Agg):
name = "sum"


class TTest(Agg):
name = "t_test"


class ValueCount(Agg):
name = "value_count"

Expand All @@ -363,6 +385,10 @@ class CumulativeSum(Pipeline):
name = "cumulative_sum"


class CumulativeCardinality(Pipeline):
name = "cumulative_cardinality"


class Derivative(Pipeline):
name = "derivative"

Expand All @@ -371,6 +397,10 @@ class ExtendedStatsBucket(Pipeline):
name = "extended_stats_bucket"


class Inference(Pipeline):
name = "inference"


class MaxBucket(Pipeline):
name = "max_bucket"

Expand All @@ -387,6 +417,14 @@ class MovingAvg(Pipeline):
name = "moving_avg"


class MovingPercentiles(Pipeline):
name = "moving_percentiles"


class Normalize(Pipeline):
name = "normalize"


class PercentilesBucket(Pipeline):
name = "percentiles_bucket"

Expand Down
19 changes: 18 additions & 1 deletion elasticsearch_dsl/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,16 @@ class FieldMaskingSpan(Query):
_param_defs = {"query": {"type": "query"}}


class SpanContainining(Query):
class SpanContaining(Query):
name = "span_containing"
_param_defs = {"little": {"type": "query"}, "big": {"type": "query"}}


# Original implementation contained
# a typo: remove in v8.0.
SpanContainining = SpanContaining


class SpanWithin(Query):
name = "span_within"
_param_defs = {"little": {"type": "query"}, "big": {"type": "query"}}
Expand Down Expand Up @@ -398,6 +403,10 @@ class Ids(Query):
name = "ids"


class Intervals(Query):
name = "intervals"


class Limit(Query):
name = "limit"

Expand Down Expand Up @@ -450,6 +459,10 @@ class Regexp(Query):
name = "regexp"


class Shape(Query):
name = "shape"


class SimpleQueryString(Query):
name = "simple_query_string"

Expand Down Expand Up @@ -488,3 +501,7 @@ class Type(Query):

class ParentId(Query):
name = "parent_id"


class Wrapper(Query):
name = "wrapper"
82 changes: 82 additions & 0 deletions test_elasticsearch_dsl/test_aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,85 @@ def test_boxplot_aggregation():
a = aggs.Boxplot(field="load_time")

assert {"boxplot": {"field": "load_time"}} == a.to_dict()


def test_rare_terms_aggregation():
a = aggs.RareTerms(field="the-field")
a.bucket("total_sales", "sum", field="price")
a.bucket(
"sales_bucket_sort",
"bucket_sort",
sort=[{"total_sales": {"order": "desc"}}],
size=3,
)

assert {
"aggs": {
"sales_bucket_sort": {
"bucket_sort": {"size": 3, "sort": [{"total_sales": {"order": "desc"}}]}
},
"total_sales": {"sum": {"field": "price"}},
},
"rare_terms": {"field": "the-field"},
} == a.to_dict()


def test_variable_width_histogram_aggregation():
a = aggs.VariableWidthHistogram(field="price", buckets=2)
assert {"variable_width_histogram": {"buckets": 2, "field": "price"}} == a.to_dict()


def test_median_absolute_deviation_aggregation():
a = aggs.MedianAbsoluteDeviation(field="rating")

assert {"median_absolute_deviation": {"field": "rating"}} == a.to_dict()


def test_t_test_aggregation():
a = aggs.TTest(
a={"field": "startup_time_before"},
b={"field": "startup_time_after"},
type="paired",
)

assert {
"t_test": {
"a": {"field": "startup_time_before"},
"b": {"field": "startup_time_after"},
"type": "paired",
}
} == a.to_dict()


def test_inference_aggregation():
a = aggs.Inference(model_id="model-id", buckets_path={"agg_name": "agg_name"})
assert {
"inference": {"buckets_path": {"agg_name": "agg_name"}, "model_id": "model-id"}
} == a.to_dict()


def test_moving_percentiles_aggregation():
a = aggs.DateHistogram()
a.bucket("the_percentile", "percentiles", field="price", percents=[1.0, 99.0])
a.pipeline(
"the_movperc", "moving_percentiles", buckets_path="the_percentile", window=10
)

assert {
"aggs": {
"the_movperc": {
"moving_percentiles": {"buckets_path": "the_percentile", "window": 10}
},
"the_percentile": {
"percentiles": {"field": "price", "percents": [1.0, 99.0]}
},
},
"date_histogram": {},
} == a.to_dict()


def test_normalize_aggregation():
a = aggs.Normalize(buckets_path="normalized", method="percent_of_sum")
assert {
"normalize": {"buckets_path": "normalized", "method": "percent_of_sum"}
} == a.to_dict()

0 comments on commit 5d13771

Please sign in to comment.