Skip to content

Instantly share code, notes, and snippets.

@vaidik
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaidik/051a197654fe4b0ecc80 to your computer and use it in GitHub Desktop.
Save vaidik/051a197654fe4b0ecc80 to your computer and use it in GitHub Desktop.
Displays how to use aggregations in Elasticsearch on documents with nested types
# Answer for: http://stackoverflow.com/questions/24631409/average-and-histogram-aggregation-on-nested-fields-in-elasticsearch
curl -XPUT "http://localhost:9200/aggs/test/_mapping" -d'
{
"test": {
"properties": {
"doctxt": {
"type": "string"
},
"nested": {
"type": "nested",
"properties": {
"pos": {
"type": "long"
},
"txt": {
"type": "string"
}
}
}
}
}
}'
curl -XPOST "http://localhost:9200/aggs/test/1" -d'
{
"doctxt": "doca",
"nested": [
{
"pos": 1,
"txt": "terma"
},
{
"pos": 2,
"txt": "termb"
},
{
"pos": 3,
"txt": "termc"
}
]
}'
curl -XPOST "http://localhost:9200/aggs/test/2" -d'
{
"doctxt": "docb",
"nested": [
{
"pos": 1,
"txt": "termd"
},
{
"pos": 2,
"txt": "terma"
},
{
"pos": 3,
"txt": "termb"
}
]
}'
curl -XGET "http://localhost:9200/aggs/test/_search" -d'
{
"query": {
"match_all": {}
},
"aggs": {
"count": {
"nested": {
"path": "nested"
},
"aggs": {
"terms": {
"terms": {
"field": "nested.txt"
},
"aggs": {
"avg_pos": {
"avg": {
"field": "nested.pos"
}
}
}
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment