What this query mean?

I've inherited a project with the following query:

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:

{
  "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:

"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?

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