Last active
August 29, 2015 14:04
-
-
Save vaidik/051a197654fe4b0ecc80 to your computer and use it in GitHub Desktop.
Displays how to use aggregations in Elasticsearch on documents with nested types
This file contains hidden or 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
# 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