Is it possible to get the nested sub elements in a document in a paginated fashion. Example we have a document as below and we would know the document id
{
"_index": "index1",
"_type": "type1",
"_id": "81cabf3c-51f5-442a-b78d-4324c4344f51",
"_version": 1,
"found": true,
"_source": {
"chiild1" : "child1 value"
"names": [
{
"name": "John"
},
{
"name": "Walter"
},
{
"name": "Justin"
},
{
"name": "Cramer"
}
]
}
}
Can we run a query like below (It does not work but are there other options ?)
GET index1/type1/81cabf3c-51f5-442a-b78d-4324c4344f51?_source_include=names.name &start=0&size=2
{
"_index": "index1",
"_type": "type1",
"_id": "81cabf3c-51f5-442a-b78d-4324c4344f51",
"_version": 1,
"found": true,
"_source": {
"names": [
{
"name": "John"
},
{
"name": "Walter"
}
]
}
}
GET index1/type1/81cabf3c-51f5-442a-b78d-4324c4344f51?_source_include=names.name &start=2&size=2
{
"_index": "index1",
"_type": "type1",
"_id": "81cabf3c-51f5-442a-b78d-4324c4344f51",
"_version": 1,
"found": true,
"_source": {
"names": [
{
"name": "Justin"
},
{
"name": "Cramer"
}
]
}
}
warkolm
(Mark Walkom)
June 14, 2016, 7:39am
2
No as it's still a single document.
Interesting idea though, maybe raise a FR on github?
Thanks for the response. I have created a FR on github - https://github.com/elastic/elasticsearch/issues/18850
To add to the information the sub elements are of nested type.
mvg
(Martijn Van Groningen)
June 14, 2016, 8:22am
5
There are two ways to get access to nested objects via inner hits or using
aggregations (using top_hits aggregation inside a nested aggregation).
Via inner hits you can get access to the nested objects on a per
document basis:
curl -XGET "http://localhost:9200/_search " -d'
{
"query": {
"bool": {
"must": [
{
"match": {
"_id": "81cabf3c-51f5-442a-b78d-4324c4344f51"
}
},
{
"nested": {
"path": "names",
"query": {
"match_all": {}
},
"inner_hits" : {
"from" : 0,
"size" : 5
}
}
}
]
}
}
}'
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-inner-hits.html#nested-inner-hits
Via aggregations you can get access to nested documents too, but as
individual documents:
curl -XGET "http://localhost:9200/_search " -d'
{
"query": {
"match": {
"_id": "81cabf3c-51f5-442a-b78d-4324c4344f51"
}
},
"aggs": {
"to-names": {
"nested": {
"path": "names"
},
"aggs": {
"names": {
"top_hits": {
"from": 0,
"size": 5
}
}
}
}
}
}'
If the query on the id field wasn't specified inner objects of other
documents would be returned too.
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-metrics-top-hits-aggregation.html#_top_hits_support_in_a_nested_or_reverse_nested_aggregator