First lets set-up our data.
Create Schema with nested
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
curl -XPUT 'http://localhost:9200/park_maintenance' -d '{ | |
"settings": { | |
"number_of_shards": 5 | |
}, | |
"mappings": { | |
"park": { | |
"properties": { | |
"park": { | |
"properties": { | |
"name": { | |
"type": "string" | |
}, | |
"cost": { | |
"type": "long" | |
}, | |
"tree": { | |
"type": "nested", | |
"properties": { | |
"height": { | |
"type": "long" | |
}, | |
"species": { | |
"type": "string" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}' | |
curl -X PUT 'http://localhost:9200/park_maintenance/_alias/pm' |
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
{"acknowledged":true} |
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
curl -X POST localhost:9200/park_maintenance/park/central -d '{ | |
"park": { | |
"name": "central", | |
"cost": "5000", | |
"tree": [ | |
{ | |
"height": 100, | |
"species": "oak" | |
}, | |
{ | |
"height": 70, | |
"species": "oak" | |
}, | |
{ | |
"height": 10, | |
"species": "maple" | |
} | |
] | |
} | |
}' | |
curl -X POST localhost:9200/park_maintenance/park/yellowstone -d '{ | |
"park": { | |
"name": "yellowstone", | |
"cost": "50000", | |
"tree": [ | |
{ | |
"height": 99, | |
"species": "oak" | |
}, | |
{ | |
"height": 70, | |
"species": "maple" | |
}, | |
{ | |
"height": 10, | |
"species": "dogwood" | |
} | |
] | |
} | |
}' |
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
curl -X POST localhost:9200/park_maintenance_plain/park/central -d '{ | |
"park": { | |
"name": "central", | |
"cost": "5000", | |
"tree": [ | |
{ | |
"height": 100, | |
"species": "oak" | |
}, | |
{ | |
"height": 70, | |
"species": "oak" | |
}, | |
{ | |
"height": 10, | |
"species": "maple" | |
} | |
] | |
} | |
}' | |
curl -X POST localhost:9200/park_maintenance_plain/park/yellowstone -d '{ | |
"park": { | |
"name": "yellowstone", | |
"cost": "50000", | |
"tree": [ | |
{ | |
"height": 99, | |
"species": "oak" | |
}, | |
{ | |
"height": 70, | |
"species": "maple" | |
}, | |
{ | |
"height": 10, | |
"species": "dogwood" | |
} | |
] | |
} | |
}' | |
curl -X PUT 'http://localhost:9200/park_maintenance_plain/_alias/pmp' |
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
curl -X POST 'http://localhost:9200/pmp/park/_search?pretty=true' -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"size": 0, | |
"aggs": { | |
"species": { | |
"terms": { | |
"field": "species" | |
}, | |
"aggs": { | |
"total_height": { | |
"sum": { | |
"field": "height" | |
} | |
} | |
} | |
} | |
} | |
}' |
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
{ | |
"took" : 4, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 0.0, | |
"hits" : [ ] | |
}, | |
"aggregations" : { | |
"species" : { | |
"buckets" : [ { | |
"key" : "maple", | |
"doc_count" : 2, | |
"total_height" : { | |
"value" : 359.0 | |
} | |
}, { | |
"key" : "oak", | |
"doc_count" : 2, | |
"total_height" : { | |
"value" : 359.0 | |
} | |
}, { | |
"key" : "dogwood", | |
"doc_count" : 1, | |
"total_height" : { | |
"value" : 179.0 | |
} | |
} ] | |
} | |
} | |
} |
To avoid this we need to make the tree type nested.
Query for aggregations with nested
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
curl -X POST 'http://localhost:9200/pm/park/_search?pretty=true' -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"size": 0, | |
"aggs": { | |
"all_species": { | |
"nested": { | |
"path": "park.tree" | |
}, | |
"aggs": { | |
"species": { | |
"terms": { | |
"field": "species" | |
}, | |
"aggs": { | |
"total_height": { | |
"sum": { | |
"field": "height" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}' |
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
{ | |
"took" : 6, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 0.0, | |
"hits" : [ ] | |
}, | |
"aggregations" : { | |
"all_species" : { | |
"doc_count" : 6, | |
"species" : { | |
"buckets" : [ { | |
"key" : "oak", | |
"doc_count" : 3, | |
"total_height" : { | |
"value" : 269.0 | |
} | |
}, { | |
"key" : "maple", | |
"doc_count" : 2, | |
"total_height" : { | |
"value" : 80.0 | |
} | |
}, { | |
"key" : "dogwood", | |
"doc_count" : 1, | |
"total_height" : { | |
"value" : 10.0 | |
} | |
} ] | |
} | |
} | |
} | |
} |
Below I show a limitation of nest when doing aggregations. Since nested creates another Lucene under the covers it cannot reach back out of that Lucene index to aggregate data.
Query for aggregations with nested but, will reach outside the sub document
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
curl -X POST 'http://localhost:9200/pm/park/_search?pretty=true' -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"size": 0, | |
"aggs": { | |
"all_species": { | |
"nested": { | |
"path": "park.tree" | |
}, | |
"aggs": { | |
"species": { | |
"terms": { | |
"field": "species" | |
}, | |
"aggs": { | |
"total_height": { | |
"sum": { | |
"field": "cost" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}' |
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
{ | |
"took" : 6, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 0.0, | |
"hits" : [ ] | |
}, | |
"aggregations" : { | |
"all_species" : { | |
"doc_count" : 6, | |
"species" : { | |
"buckets" : [ { | |
"key" : "oak", | |
"doc_count" : 3, | |
"total_height" : { | |
"value" : 0.0 | |
} | |
}, { | |
"key" : "maple", | |
"doc_count" : 2, | |
"total_height" : { | |
"value" : 0.0 | |
} | |
}, { | |
"key" : "dogwood", | |
"doc_count" : 1, | |
"total_height" : { | |
"value" : 0.0 | |
} | |
} ] | |
} | |
} | |
} | |
} |