diff --git a/core/src/main/java/org/elasticsearch/search/SearchHit.java b/core/src/main/java/org/elasticsearch/search/SearchHit.java index 7566d5ad279..8d434b8af20 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchHit.java +++ b/core/src/main/java/org/elasticsearch/search/SearchHit.java @@ -328,6 +328,14 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable "offset": 0 <2> diff --git a/docs/reference/search/request/inner-hits.asciidoc b/docs/reference/search/request/inner-hits.asciidoc index a9da7378809..fceb37418eb 100644 --- a/docs/reference/search/request/inner-hits.asciidoc +++ b/docs/reference/search/request/inner-hits.asciidoc @@ -152,6 +152,9 @@ An example of a response snippet that could be generated from the above search r "max_score": 1.0, "hits": [ { + "_index": "test", + "_type": "doc", + "_id": "1", "_nested": { "field": "comments", "offset": 1 @@ -278,6 +281,9 @@ Response not included in text but tested for completeness sake. "max_score": 1.0444683, "hits": [ { + "_index": "test", + "_type": "doc", + "_id": "1", "_nested": { "field": "comments", "offset": 1 @@ -394,6 +400,9 @@ Which would look like: "max_score": 0.6931472, "hits": [ { + "_index": "test", + "_type": "doc", + "_id": "1", "_nested": { "field": "comments", "offset": 1, @@ -505,6 +514,7 @@ An example of a response snippet that could be generated from the above search r "max_score": 1.0, "hits": [ { + "_index": "test", "_type": "doc", "_id": "2", "_score": 1.0, diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml index 50f4d55750d..2b7368f9ec4 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml @@ -41,6 +41,6 @@ setup: - match: { hits.total: 1 } - match: { hits.hits.0._index: "test" } - match: { hits.hits.0._id: "1" } - - is_false: hits.hits.0.inner_hits.child.hits.hits.0._index + - match: { hits.hits.0.inner_hits.child.hits.hits.0._index: "test"} - match: { hits.hits.0.inner_hits.child.hits.hits.0._id: "2" } - is_false: hits.hits.0.inner_hits.child.hits.hits.0._nested diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/200_top_hits_metric.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/200_top_hits_metric.yml new file mode 100644 index 00000000000..f48aee28dc2 --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/200_top_hits_metric.yml @@ -0,0 +1,82 @@ +--- +"top_hits aggregation with nested documents": + - skip: + version: "5.99.99 - " + reason: "5.x nodes don't include index or id in nested top hits" + - do: + indices.create: + index: my-index + body: + settings: + number_of_shards: 1 + number_of_replicas: 0 + mappings: + doc: + properties: + users: + type: nested + + - do: + index: + index: my-index + type: doc + id: 1 + refresh: true + body: | + { + "group" : "fans", + "users" : [ + { + "first" : "John", + "last" : "Smith" + }, + { + "first" : "Alice", + "last" : "White" + } + ] + } + + - do: + index: + index: my-index + type: doc + id: 2 + refresh: true + body: | + { + "group" : "fans", + "users" : [ + { + "first" : "Mark", + "last" : "Doe" + } + ] + } + + - do: + search: + body: + aggs: + to-users: + nested: + path: users + aggs: + users: + top_hits: + sort: "users.last.keyword" + + - match: { hits.total: 2 } + - length: { aggregations.to-users.users.hits.hits: 3 } + - match: { aggregations.to-users.users.hits.hits.0._id: "2" } + - match: { aggregations.to-users.users.hits.hits.0._index: my-index } + - match: { aggregations.to-users.users.hits.hits.0._nested.field: users } + - match: { aggregations.to-users.users.hits.hits.0._nested.offset: 0 } + - match: { aggregations.to-users.users.hits.hits.1._id: "1" } + - match: { aggregations.to-users.users.hits.hits.1._index: my-index } + - match: { aggregations.to-users.users.hits.hits.1._nested.field: users } + - match: { aggregations.to-users.users.hits.hits.1._nested.offset: 0 } + - match: { aggregations.to-users.users.hits.hits.2._id: "1" } + - match: { aggregations.to-users.users.hits.hits.2._index: my-index } + - match: { aggregations.to-users.users.hits.hits.2._nested.field: users } + - match: { aggregations.to-users.users.hits.hits.2._nested.offset: 1 } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search.inner_hits/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search.inner_hits/10_basic.yml index 4264083c13f..80d3f924d2b 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search.inner_hits/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search.inner_hits/10_basic.yml @@ -34,9 +34,9 @@ setup: - match: { hits.hits.0._index: "test" } - match: { hits.hits.0._type: "type_1" } - match: { hits.hits.0._id: "1" } - - is_false: hits.hits.0.inner_hits.nested_field.hits.hits.0._index - - is_false: hits.hits.0.inner_hits.nested_field.hits.hits.0._type - - is_false: hits.hits.0.inner_hits.nested_field.hits.hits.0._id + - match: { hits.hits.0.inner_hits.nested_field.hits.hits.0._index: "test" } + - match: { hits.hits.0.inner_hits.nested_field.hits.hits.0._type: "type1" } + - match: { hits.hits.0.inner_hits.nested_field.hits.hits.0._id: "1" } - match: { hits.hits.0.inner_hits.nested_field.hits.hits.0._nested.field: "nested_field" } - match: { hits.hits.0.inner_hits.nested_field.hits.hits.0._nested.offset: 0 } - is_false: hits.hits.0.inner_hits.nested_field.hits.hits.0._nested.child