# What this query mean?

**URL:** <https://discuss.elastic.co/t/what-this-query-mean/279669>\
**Category:** Elasticsearch\
**Created:** [July 26, 2021, 9:49pm UTC](https://discuss.elastic.co/t/what-this-query-mean/279669 "2021-07-26T21:49:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgelb](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@rgelb](https://discuss.elastic.co/u/rgelb)\
**Post date:** [July 26, 2021, 9:49pm UTC](https://discuss.elastic.co/t/what-this-query-mean/279669/1 "2021-07-26T21:49:20Z")

</div>

I've inherited a project with the following query:

```auto
POST listings/_search
{
  "aggs": {
    "gridcells": {
      "aggs": {
        "latlons": {
          "aggs": {
            "latStats": {
              "stats": {
                "field": "latitude"
              }
            },
            "lonStats": {
              "stats": {
                "field": "longitude"
              }
            }
          },
          "terms": {
            "field": "longitude",
            "script": {
              "params": {
                "interval": 0.1651051651051651
              },
              "source": "_value % params.interval < 0 ? _value -((_value % params.interval) + params.interval) : (_value - (_value % params.interval))"
            }
          }
        }
      },
      "terms": {
        "field": "latitude",
        "script": {
          "params": {
            "interval": 0.14865207628182212
          },
          "source": "_value % params.interval < 0 ? _value -((_value % params.interval) + params.interval) : (_value - (_value % params.interval))"
        }
      }
    }
  },
  "size": 0
}

```

I see what the query does. It returns aggregations latStats/lonStats based on latitude/longitude fields in the index. The aggregations subdivide records into latitude/longitude buckets. Here is the example of what is being returned:

```auto
{
  "key" : -77.26921726921726,
  "doc_count" : 216736,
  "latStats" : {
    "count" : 216736,
    "min" : 38.79819869995117,
    "max" : 38.94683837890625,
    "avg" : 38.87041338256063,
    "sum" : 8424617.91488266
  },
  "lonStats" : {
    "count" : 216736,
    "min" : -77.26920318603516,
    "max" : -77.10411834716797,
    "avg" : -77.17316415780547,
    "sum" : -1.6726202906906128E7
  }
}

```

It allows me to construct a rectangle on the map of where the documents are physically located.

What I don't understand is the "terms" node:

```auto
"terms": {
  "field": "latitude",
  "script": {
    "params": {
      "interval": 0.14865207628182212
    },
    "source": "_value % params.interval < 0 ? _value -((_value % params.interval) + params.interval) : (_value - (_value % params.interval))"
  }
}

```

The `source` seems to be a formula that takes the params.interval number 0.14865207628182212, the `_value` (assuming refers to the latitude field in the index) and applies it to the `_value % params.interval < 0 ? _value -((_value % params.interval) + params.interval) : (_value - (_value % params.interval))` formula.

1. Is the `source` supposed to indicate how big the aggregation buckets are?
2. What about `params.interval` value? Does it refer to some sort of geographical value?
3. And finally, the formula itself - what does it do?

---

<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:** [August 23, 2021, 9:49pm UTC](https://discuss.elastic.co/t/what-this-query-mean/279669/2 "2021-08-23T21:49:44Z")

</div>

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