Skip to content

Conversation

@mayya-sharipova
Copy link
Contributor

@mayya-sharipova mayya-sharipova commented Oct 29, 2025

Currently top_hits aggs inside nested aggs inherit the parent
query's inner_hits context, causing an error "Error retrieving path".
This happenes because nested top_hits is already operating on nested child docs,
and asking it to retrieve inner_hits adds an extra incorrect level of nestedness.

This fix proposes to create an empty InnerHitsContext for this case.

Closes #136893

Top_hits aggregations inside nested aggregations were inheriting the parent
query's inner_hits context, causing conflicting fetch scopes and nested source
extraction to fail with "Error retrieving path".

When top_hits is a child of NestedAggregator, now creates an empty
InnerHitsContext to prevent InnerHitsPhase from running.

Closes elastic#136893
@elasticsearchmachine
Copy link
Collaborator

Hi @mayya-sharipova, I've created a changelog YAML for you.

@elasticsearchmachine elasticsearchmachine added the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Oct 30, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-relevance (Team:Search Relevance)

@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Oct 30, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@benwtrent
Copy link
Member

@mayya-sharipova maybe I misunderstand how this will look for users.

But as a user, I would still expect to get inner hits for both whenever I request them. One that is present within _source the other that is present within the aggregator results. Especially since they will likely be different documents (top-hits under a terms agg will definitely return different inner hits than those that are present in the most relevant documents given the top level query).

@mayya-sharipova
Copy link
Contributor Author

mayya-sharipova commented Oct 30, 2025

@benwtrent Thanks for your comment. This fix applies only when top_hits agg is a part of nested. When we do nested top_hits agg we already retrieving nested child docs.
So we discussed this offline:

  • Documentation how nested agg works with top_hits agg
  • We should first check if there is second level of nestedness and allow inner_hits, if not it makes sense to remove them.

@mayya-sharipova
Copy link
Contributor Author

@benwtrent I've checked an it looks like inner_hits for double nested fields in top_hits doesn't work (for most docs doesn't return anything).
So something like this doesn't work:

PUT comments
{
    "mappings": {
        "properties": {
            "product": {
                "type": "keyword"
            },
            "comments": {
                "type": "nested",
                "properties": {
                  "comment": {
                    "type": "text"
                    },
                    "user": {
                      "type" : "keyword"
                    },
                    "ratings": {
                      "type": "nested",
                        "properties": {
                            "rating": {
                                "type": "integer"
                            },
                            "user": {
                              "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

POST /comments/_search?_source=false
{
  "aggs": {
    "nested_ratings": {
      "nested": {
        "path": "comments"  
      },
      "aggs": {
        "ratings_top_hits": {
            "top_hits": {
              "size": 10
            }
        }
      }
    }
  },
  "query": {
      "nested": {
          "inner_hits": {},
          "path": "comments",
          "query": {
              "match_all": {}
          }
      }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/Aggregations Aggregations >bug :Search Relevance/Search Catch all for Search Relevance Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

illegal_state_exception Error retrieving path nested_item inner_hits and top_hits error

3 participants