Last active
August 29, 2015 14:11
-
-
Save 2e3s/9cab65985d932a060704 to your computer and use it in GitHub Desktop.
Elasticsearch histogram can start only from divisible by interval value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 } | |
} | |
} | |
} | |
} | |
' |
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
The result
Expected:
[{"key" : 7, "doc_count" : 3}, {"key" : 10, "doc_count" : 2}]