How can nested queries return only the first nested document but not all nested documents

I need to remove the nested documents but only return the only nested documents in the same document. What should I do

May be this (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html) is what you are looking for?

But what if I wanted to use the fields that are returned in inner_hits to aggregate

What about changing your model? Instead of indexing a big document containing an array of sub-documents, just index individual sub-documents as apparently you want to aggregate on sub-documents, not parent documents themselves.

If you have other question, I'd suggest that you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

"authors" : [
{
"affiliations" : [
{
"country" : "India"
}
]
},
{
"affiliations" : [
{
"country" : "India"
}
]
},
"authors" : [
{
"affiliations" : [
{
"country" : "India"
}
]
},
{
"affiliations" : [
{
"country" : "India"
}
]
},
{
"affiliations" : [
{
"country" : "India"
}
]
}
]

Hello, for example, this is an article with multiple authors, each author has his mailing address and country. I want to know which country this article is published in, so the expected value should be:
{
"key" : "India",
"doc_count" : 1
}
But the aggregate returns a value of:
{
"key" : "India",
"doc_count" : 5
}
So I want to ask if the existing API can do that, just take one of the documents in the nested document to aggregate, if not change the model as you said, how should I change the model, I am new, thank you,
Here's a sample API I used:
GET pubmed/_search
{
"size": 10,
"query": {
"bool": {
"filter": [
{
"term": {
"journal.id": {
"value": "2",
"boost": 1
}
}
},
{
"nested": {
"query": {
"terms": {
"authors.affiliations.country": [
"China",
"Japan"
],
"boost": 1
}
},
"path": "authors.affiliations",
"ignore_unmapped": false,
"score_mode": "max",
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"_source": false,
"aggregations": {
"dateRange": {
"date_range": {
"field": "pubTime",
"ranges": [
{
"from": "1999",
"to": "2200"
}
],
"keyed": false
},
"aggregations": {
"authors.affiliations": {
"nested": {
"path": "authors.affiliations"
},
"aggregations": {
"countrys": {
"terms": {
"field": "authors.affiliations.country",
"size": 2,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
],
"include": [
"China",
"Japan"
]
}
}
}
}
}
}
}
}Preformatted text

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.