Skip to content

Instantly share code, notes, and snippets.

@jprante
Created December 28, 2013 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jprante/8159379 to your computer and use it in GitHub Desktop.
Save jprante/8159379 to your computer and use it in GitHub Desktop.
Elasticsearch aggregations demo
curl -XDELETE 'localhost:9200/test'
curl -XPUT 'localhost:9200/test' -d '
{
"mappings" : {
"_default_": {
"properties" : {
"name" : { "type" : "string" },
"age" : { "type" : "integer" },
"country" : { "type" : "string" },
"gender" : { "type" : "string" }
}
}
}
}
'
curl -XPUT 'localhost:9200/test/data/1' -d '
{
"name" : "abc",
"age" : 22,
"country" : "usa",
"gender" : "male"
}
'
curl -XPUT 'localhost:9200/test/data/2' -d '
{
"name" : "xyz",
"age" : 27,
"country" : "usa",
"gender" : "male"
}
'
curl -XPUT 'localhost:9200/test/data/3' -d '
{
"name" : "xyz",
"age" : 22,
"country" : "india",
"gender" : "female"
}
'
curl -XPUT 'localhost:9200/test/data/4' -d '
{
"name" : "abc",
"age" : 22,
"country" : "usa",
"gender" : "female"
}
'
curl -XGET 'localhost:9200/test/_refresh'
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"aggs" : {
"age": {
"terms": {
"field": "age"
},
"aggs" : {
"country" : {
"terms" : {
"field" : "country"
}
}
}
}
}
}'
{"ok":true,"acknowledged":true}{"ok":true,"acknowledged":true}{"ok":true,"_index":"test","_type":"data","_id":"1","_version":1}{"ok":true,"_index":"test","_type":"data","_id":"2","_version":1}{"ok":true,"_index":"test","_type":"data","_id":"3","_version":1}{"ok":true,"_index":"test","_type":"data","_id":"4","_version":1}{"ok":true,"_shards":{"total":10,"successful":5,"failed":0}}{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [ {
"_index" : "test",
"_type" : "data",
"_id" : "3",
"_score" : 1.0, "_source" :
{
"name" : "xyz",
"age" : 22,
"country" : "india",
"gender" : "female"
}
}, {
"_index" : "test",
"_type" : "data",
"_id" : "2",
"_score" : 1.0, "_source" :
{
"name" : "xyz",
"age" : 27,
"country" : "usa",
"gender" : "male"
}
}, {
"_index" : "test",
"_type" : "data",
"_id" : "1",
"_score" : 1.0, "_source" :
{
"name" : "abc",
"age" : 22,
"country" : "usa",
"gender" : "male"
}
}, {
"_index" : "test",
"_type" : "data",
"_id" : "4",
"_score" : 1.0, "_source" :
{
"name" : "abc",
"age" : 22,
"country" : "usa",
"gender" : "female"
}
} ]
},
"aggregations" : {
"age" : {
"buckets" : [ {
"key" : 22,
"doc_count" : 3,
"country" : {
"buckets" : [ {
"key" : "usa",
"doc_count" : 2
}, {
"key" : "india",
"doc_count" : 1
} ]
}
}, {
"key" : 27,
"doc_count" : 1,
"country" : {
"buckets" : [ {
"key" : "usa",
"doc_count" : 1
} ]
}
} ]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment