# Top trend/variation in terms aggregation

**URL:** <https://discuss.elastic.co/t/top-trend-variation-in-terms-aggregation/126244>\
**Category:** Elasticsearch\
**Created:** [March 30, 2018, 3:03pm UTC](https://discuss.elastic.co/t/top-trend-variation-in-terms-aggregation/126244 "2018-03-30T15:03:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VanMicky](https://avatars.discourse-cdn.com/v4/letter/v/e9a140/32.png) [@VanMicky](https://discuss.elastic.co/u/VanMicky)\
**Post date:** [March 30, 2018, 3:03pm UTC](https://discuss.elastic.co/t/top-trend-variation-in-terms-aggregation/126244/1 "2018-03-30T15:03:28Z")

</div>

Hello,

Let's say I have a date histogram over a terms aggregation :

```
{
  "size": 0,
  "aggs": {
    "my_date_histo": {
      "date_histogram": {
        "field": "date",
        "interval": "month"
      },
      "aggs": {
        "categories": {
          "terms": {
            "field": "category"
          }
        }
      }
    }
  }
}

```

With returns something like:

```
"aggregations": {
    "my_date_histo": {
      "buckets": [
        {
          "key_as_string": "2018-02-01T00:00:00.000Z",
          "categories": {
            "buckets": [
              {
                "key": "A",
                "doc_count": 10
              },
              {
                "key": "B",
                "doc_count": 5
              },
              {
                "key": "C",
                "doc_count": 0
              }
            ],
          }
        },
        {
          "key_as_string": "2018-03-01T00:00:00.000Z",
          "categories": {
            "buckets": [
              {
                "key": "A",
                "doc_count": 10
              },
              {
                "key": "B",
                "doc_count": 4
              },
              {
                "key": "C",
                "doc_count": 3
              }
            ],
          }
        }
      ],
    }

```

I would like to know the top terms variations (positive and negation) between the two dates, something like:

```
 "categories": {
            "buckets": [
              {
                "key": "C",
                "doc_count": 3
              },
              {
                "key": "A",
                "doc_count": 0
              },
              {
                "key": "B",
                "doc_count": -1
              }
            ]
}

```

Any idea of how to do such aggregation?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 30, 2018, 3:17pm UTC](https://discuss.elastic.co/t/top-trend-variation-in-terms-aggregation/126244/2 "2018-03-30T15:17:22Z")

</div>

May be pipeline aggregations can do that.

---

<div class="post-metadata">

**Author:** ![VanMicky](https://avatars.discourse-cdn.com/v4/letter/v/e9a140/32.png) [@VanMicky](https://discuss.elastic.co/u/VanMicky)\
**Post date:** [March 30, 2018, 3:44pm UTC](https://discuss.elastic.co/t/top-trend-variation-in-terms-aggregation/126244/3 "2018-03-30T15:44:26Z")

</div>

I actually found a way. Not very pretty, but the following will returns the top positive trend.

```
{
  "size": 0,
  "aggs": {
    "genres": {
      "terms": {
        "field": "category"
      },
      "aggs": {
        "sales_bucket_sort": {
          "bucket_sort": {
            "sort": [
              {"histo>thirtieth_difference": {"order": "desc"}}
            ],
            "size": 10,
            "gap_policy": "insert_zeros"
          }
        },
        "histo": {
          "date_histogram": {
            "field": "date",
            "interval": "month"
          },
          "aggs": {
            "thirtieth_difference": {
              "serial_diff": {
                "buckets_path": "_count",
                "lag": 1
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 27, 2018, 3:58pm UTC](https://discuss.elastic.co/t/top-trend-variation-in-terms-aggregation/126244/4 "2018-04-27T15:58:45Z")

</div>

Closed on April 27, 2018, 3:58pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
