Skip to content

Instantly share code, notes, and snippets.

@2e3s
Last active August 29, 2015 14:11
Show Gist options
  • Save 2e3s/9cab65985d932a060704 to your computer and use it in GitHub Desktop.
Save 2e3s/9cab65985d932a060704 to your computer and use it in GitHub Desktop.
Elasticsearch histogram can start only from divisible by interval value
# Remove old data
curl -XDELETE "http://localhost:9200/divisible"
# Create Document
curl -XPOST "http://localhost:9200/_bulk" -d '
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 1}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 2}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 3}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 4}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 5}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 6}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 7}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 8}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 9}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 10}
{"create": {"_index": "divisible", "_type": "divisible"}}
{"value": 11}
'
# Wait for ES to be synced (aka refresh indices)
curl -XPOST "http://localhost:9200/divisible/_refresh"
# Search
curl -XPOST "http://localhost:9200/divisible/divisible/_search?pretty=true&search_type=count" -d '
{
"query":{
"range": {"value": {"gte": 7}}
},
"aggregations": {
"rows": {
"histogram": {
"field": "value",
"interval": 3,
"min_doc_count": 0,
"extended_bounds": { "min": 7 }
}
}
}
}
'
@2e3s
Copy link
Author

2e3s commented Dec 21, 2014

The result

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "rows" : {
      "buckets" : [ {
        "key" : 6,
        "doc_count" : 2
      }, {
        "key" : 9,
        "doc_count" : 3
      } ]
    }
  }
}

Expected: [{"key" : 7, "doc_count" : 3}, {"key" : 10, "doc_count" : 2}]

@2e3s
Copy link
Author

2e3s commented Dec 22, 2014

That works

# Search
curl -XPOST "http://localhost:9200/divisible/divisible/_search?pretty=true&search_type=count" -d '
{
    "query":{
        "range": {"value": {"gte": 7}}
    },
    "aggregations": {
        "rows": {
            "histogram": {
                "field": "value",
                "interval": 3,
                "script": "_value-1",
                "lang":"groovy",
                "min_doc_count": 0,
                "extended_bounds": { "min": 7 }
            }
        }
    }
}
'

Result:

  "aggregations" : {
    "rows" : {
      "buckets" : [ {
        "key" : 6,
        "doc_count" : 3
      }, {
        "key" : 9,
        "doc_count" : 2
      } ]
    }
  }

I can just add 6+7%3, 9+7%3 in my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment