Skip to content

Instantly share code, notes, and snippets.

@etki
Created August 22, 2020 11:36
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 etki/677b2d570520029f4b6a45646405a67b to your computer and use it in GitHub Desktop.
Save etki/677b2d570520029f4b6a45646405a67b to your computer and use it in GitHub Desktop.
Nested elasticsearch agg
# PUT localhost:9200/index
{
"mappings": {
"properties": {
"nested": {
"type": "nested",
"properties": {
"values": {
"type": "keyword"
}
}
}
}
}
}
# PUT localhost:9200/index/_doc/1
{
"nested": {
"values": ["1", "2", "1"]
}
}
# PUT localhost:9200/index/_doc/2
{
"nested": {
"values": ["1", "3"]
}
}
# POST localhost:9200/index/_search
{
"size": 0,
"aggs": {
"root": {
"terms": {
"field": "_id"
},
"aggs": {
"values": {
"nested": {
"path": "nested"
},
"aggs": {
"combined": {
"terms": {
"field": "nested.values"
}
}
}
}
}
}
}
}
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"root": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1",
"doc_count": 1,
"values": {
"doc_count": 1,
"combined": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1",
"doc_count": 1
},
{
"key": "2",
"doc_count": 1
}
]
}
}
},
{
"key": "2",
"doc_count": 1,
"values": {
"doc_count": 1,
"combined": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1",
"doc_count": 1
},
{
"key": "3",
"doc_count": 1
}
]
}
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment