Aggregations

Overview

Aggregations (aka filters) are used to filter and discover data in easy and fast way.

Terms aggregations

Simple term aggregation:

{
  "schema": {
    "country": {
      "type": "string",
      "index": "not_analyzed",
      "store": true
    }
  },
  "aggregations": {
    "country": {
      "type": "terms",
      "field": "country",
      "size": 10,
      "conjunction": true,
      "title": "Country"
    }
  }
}

Term aggregation with sorting:

{
  "schema": {
    "country": {
      "type": "string",
      "index": "not_analyzed",
      "store": true
    }
  },
  "aggregations": {
    "country": {
      "type": "terms",
      "field": "country",
      "size": 10,
      "title": "Country",
      "sort": "_term",
      "order": "asc"
    }
  }
}
Note: "sort" can be "_term" or "_count" and "order" can be "asc" or "desc"

Range aggregations

Range aggregation can be used for votes, years and a different numbers.

{
  "schema": {
    "rating": {
      "type": "float"
    }
  },
  "aggregations": {
    "ratings": {
      "type": "range",
      "field": "rating",
      "title": "Rating",
      "ranges": [
        {
          "lte": 1,
          "name": "0 - 1"
        },
        {
          "gte": 1,
          "lte": 2,
          "name": "1 - 2"
        },
        {
          "gte": 2,
          "lte": 3,
          "name": "2 - 3"
        },
        {
          "gte": 3,
          "lte": 4,
          "name": "3 - 4"
        },
        {
          "gte": 4,
          "name": "4 - 5"
        }
      ]
    }
  }
}

Geo aggregations

{
  "schema": {
    "geo": {
      "type": "geo_point"
    }
  },
  "aggregations": {
    "distance_ranges": {
      "type": "geo_distance",
      "field": "geo",
      "ranges": [{
          "lte": 500,
          "name": "Less than 500"
        }, {
          "gte": 500,
          "lte": 1000,
          "name": "500 - 1000"
        }, {
          "gte": 500,
          "name": "More than 1000"
        }
      ],
      "unit": "km",
      "title": "Distance ranges [km]"
    }
  }
}
Was this page helpful ?