I would like to avoid parent/child solution and I am wondering if in a single query/request I can return some documents.
I have documents “rate:
{ id="123", docType="rate", "grade":100}
{ id="456", docType="rate", "grade":0}
{ id="789", docType="rate", "grade":45}
I have documents “topics”:
{ id="abc", docType="topic", "title":"Alien"}
{ id="def", docType="topic", "title":"Suspiria"}
{ id="a12", docType="topic", "title":"Ring"}
All documents are in the same index.
_search Request (to improve) is:
{
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"docType": {
"value": "rate"
}
}
}
]
}
},
"_source": false,
"aggs": {
"Topics": {
"terms": {
"field": "topicId",
"size": 2
},
"aggs": {
"Grades": {
"terms": {
"field": "grade"
}
}
}
}
}
}
Response is:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": null,
"hits": []
},
"aggregations": {
"Topics": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 25511,
"buckets": [
{
"key": "abc", <---------------------------------- here
"doc_count": 3,
"Grades": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 100,
"doc_count": 2
},
{
"key": 0,
"doc_count": 1
}
]
}
},
{
"key": "def", <---------------------------------- here
"doc_count": 2,
"Grades": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 0,
"doc_count": 1
},
{
"key": 100,
"doc_count": 1
}
]
}
}
]
}
}
}
I am wondering if it is possible to have a query/request to get (via “script” or any other solution, or another query ^^) _source documents from the aggregations (“topic” documents) by the result key id returned (see “<——— here” above)
Excepted top hits returned documents _source response I would like:
.../...
_source .../...:
{ id="abc", docType="topic", "title":"Alien"}
{ id="def", docType="topic", "title":"Suspiria"}
.../...