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_bucket_sort_agg_only_trnunc():
    bucket_sort_agg = aggs.BucketSort(**{"from": 1, "size": 1})
    assert bucket_sort_agg.to_dict() == {"bucket_sort": {"from": 1, "size": 1}}

    a = aggs.DateHistogram(field="date", interval="month")
    a.bucket("bucket_truncate", "bucket_sort", **{"from": 1, "size": 1})
    assert {
        "date_histogram": {"field": "date", "interval": "month"},
        "aggs": {"bucket_truncate": {"bucket_sort": {"from": 1, "size": 1}}},
    } == a.to_dict()
def test_bucket_sort_agg():
    bucket_sort_agg = aggs.BucketSort(sort=[{
        "total_sales": {
            "order": "desc"
        }
    }],
                                      size=3)
    assert bucket_sort_agg.to_dict() == {
        "bucket_sort": {
            "sort": [{
                "total_sales": {
                    "order": "desc"
                }
            }],
            "size": 3
        }
    }

    a = aggs.DateHistogram(field="date", interval="month")
    a.bucket("total_sales", "sum", field="price")
    a.bucket(
        "sales_bucket_sort",
        "bucket_sort",
        sort=[{
            "total_sales": {
                "order": "desc"
            }
        }],
        size=3,
    )
    assert {
        "date_histogram": {
            "field": "date",
            "interval": "month"
        },
        "aggs": {
            "total_sales": {
                "sum": {
                    "field": "price"
                }
            },
            "sales_bucket_sort": {
                "bucket_sort": {
                    "sort": [{
                        "total_sales": {
                            "order": "desc"
                        }
                    }],
                    "size": 3
                }
            },
        },
    } == a.to_dict()
Example #4
0
def test_bucket_sort_agg():
    bucket_sort_agg = aggs.BucketSort(sort=[{
        "total_sales": {
            "order": "desc"
        }
    }],
                                      size=3)
    assert bucket_sort_agg.to_dict() == {
        "bucket_sort": {
            "sort": [{
                "total_sales": {
                    "order": "desc"
                }
            }],
            "size": 3
        }
    }

    a = aggs.DateHistogram(field='date', interval='month')
    a.bucket('total_sales', 'sum', field='price')
    a.bucket('sales_bucket_sort',
             'bucket_sort',
             sort=[{
                 "total_sales": {
                     "order": "desc"
                 }
             }],
             size=3)
    assert {
        "date_histogram": {
            "field": "date",
            "interval": "month"
        },
        "aggs": {
            "total_sales": {
                "sum": {
                    "field": "price"
                }
            },
            "sales_bucket_sort": {
                "bucket_sort": {
                    "sort": [{
                        "total_sales": {
                            "order": "desc"
                        }
                    }],
                    "size": 3
                }
            }
        }
    } == a.to_dict()
Example #5
0
def test_bucket_sort_agg_only_trnunc():
    bucket_sort_agg = aggs.BucketSort(**{'from': 1, 'size': 1})
    assert bucket_sort_agg.to_dict() == {"bucket_sort": {"from": 1, "size": 1}}

    a = aggs.DateHistogram(field='date', interval='month')
    a.bucket('bucket_truncate', 'bucket_sort', **{'from': 1, 'size': 1})
    assert {
        "date_histogram": {
            "field": "date",
            "interval": "month"
        },
        "aggs": {
            "bucket_truncate": {
                "bucket_sort": {
                    "from": 1,
                    "size": 1
                }
            }
        }
    } == a.to_dict()
Example #6
0
 def add_agg_text_tokens_date_histogram_terms(
     self,
     search: Search,
     calendar_interval: str,
     size: int,
     include: object,
 ) -> Search:
     search = copy(search)
     a = search.aggs.bucket(
         self._date_field.replace(".", "__"),
         aggs.DateHistogram(field=self._date_field,
                            calendar_interval=calendar_interval),
     )
     if size:
         a.bucket(
             self._text_tokens_field.replace(".", "__"),
             aggs.Terms(
                 field=self._text_tokens_field,
                 size=size,
                 include=include,
             ),
         )
     return search