2019-07-17 08:49:22 -04:00
|
|
|
[[request-body-search-inner-hits]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Inner hits
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2017-12-26 11:25:05 -05:00
|
|
|
The <<parent-join, parent-join>> and <<nested, nested>> features allow the return of documents that
|
2016-08-11 15:14:24 -04:00
|
|
|
have matches in a different scope. In the parent/child case, parent documents are returned based on matches in child
|
|
|
|
documents or child documents are returned based on matches in parent documents. In the nested case, documents are returned
|
2014-10-13 03:41:55 -04:00
|
|
|
based on matches in nested inner objects.
|
|
|
|
|
2018-01-31 05:55:53 -05:00
|
|
|
In both cases, the actual matches in the different scopes that caused a document to be returned are hidden. In many cases,
|
2015-09-30 05:07:51 -04:00
|
|
|
it's very useful to know which inner nested objects (in the case of nested) or children/parent documents (in the case
|
2014-12-02 14:51:06 -05:00
|
|
|
of parent/child) caused certain information to be returned. The inner hits feature can be used for this. This feature
|
2014-10-13 03:41:55 -04:00
|
|
|
returns per search hit in the search response additional nested hits that caused a search hit to match in a different scope.
|
|
|
|
|
2016-01-10 15:51:29 -05:00
|
|
|
Inner hits can be used by defining an `inner_hits` definition on a `nested`, `has_child` or `has_parent` query and filter.
|
2014-12-03 12:18:34 -05:00
|
|
|
The structure looks like this:
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2015-01-19 12:56:25 -05:00
|
|
|
"<query>" : {
|
|
|
|
"inner_hits" : {
|
|
|
|
<inner_hits_options>
|
2014-10-13 03:41:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
// NOTCONSOLE
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2016-08-11 06:35:40 -04:00
|
|
|
If `inner_hits` is defined on a query that supports it then each search hit will contain an `inner_hits` json object with the following structure:
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_index": ...,
|
|
|
|
"_type": ...,
|
|
|
|
"_id": ...,
|
|
|
|
"inner_hits": {
|
|
|
|
"<inner_hits_name>": {
|
|
|
|
"hits": {
|
|
|
|
"total": ...,
|
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_type": ...,
|
|
|
|
"_id": ...,
|
|
|
|
...
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
// NOTCONSOLE
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2019-07-19 14:35:36 -04:00
|
|
|
===== Options
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
Inner hits support the following options:
|
|
|
|
|
|
|
|
[horizontal]
|
|
|
|
`from`:: The offset from where the first hit to fetch for each `inner_hits` in the returned regular search hits.
|
|
|
|
`size`:: The maximum number of hits to return per `inner_hits`. By default the top three matching hits are returned.
|
|
|
|
`sort`:: How the inner hits should be sorted per `inner_hits`. By default the hits are sorted by the score.
|
2014-12-03 12:18:34 -05:00
|
|
|
`name`:: The name to be used for the particular inner hit definition in the response. Useful when multiple inner hits
|
|
|
|
have been defined in a single search request. The default depends in which query the inner hit is defined.
|
|
|
|
For `has_child` query and filter this is the child type, `has_parent` query and filter this is the parent type
|
|
|
|
and the nested query and filter this is the nested path.
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
Inner hits also supports the following per document features:
|
|
|
|
|
2020-07-17 10:57:00 -04:00
|
|
|
* <<highlighting,Highlighting>>
|
2019-07-19 09:16:35 -04:00
|
|
|
* <<request-body-search-explain,Explain>>
|
|
|
|
* <<request-body-search-source-filtering,Source filtering>>
|
|
|
|
* <<request-body-search-script-fields,Script fields>>
|
2020-06-11 11:25:04 -04:00
|
|
|
* <<docvalue-fields,Doc value fields>>
|
2019-07-19 09:16:35 -04:00
|
|
|
* <<request-body-search-version,Include versions>>
|
|
|
|
* <<request-body-search-seq-no-primary-term,Include Sequence Numbers and Primary Terms>>
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
[[nested-inner-hits]]
|
2019-07-19 14:35:36 -04:00
|
|
|
===== Nested inner hits
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
The nested `inner_hits` can be used to include nested inner objects as inner hits to a search hit.
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-10-13 03:41:55 -04:00
|
|
|
--------------------------------------------------
|
2019-01-18 08:11:18 -05:00
|
|
|
PUT test
|
2014-10-13 03:41:55 -04:00
|
|
|
{
|
2017-05-04 17:30:54 -04:00
|
|
|
"mappings": {
|
2019-01-18 08:11:18 -05:00
|
|
|
"properties": {
|
|
|
|
"comments": {
|
|
|
|
"type": "nested"
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT test/_doc/1?refresh
|
2017-05-04 17:30:54 -04:00
|
|
|
{
|
|
|
|
"title": "Test title",
|
|
|
|
"comments": [
|
|
|
|
{
|
|
|
|
"author": "kimchy",
|
2017-06-16 05:13:23 -04:00
|
|
|
"number": 1
|
2017-05-04 17:30:54 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"author": "nik9000",
|
2017-06-16 05:13:23 -04:00
|
|
|
"number": 2
|
2014-10-13 03:41:55 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
POST test/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"nested": {
|
|
|
|
"path": "comments",
|
|
|
|
"query": {
|
2017-06-16 05:13:23 -04:00
|
|
|
"match": {"comments.number" : 2}
|
2017-05-04 17:30:54 -04:00
|
|
|
},
|
|
|
|
"inner_hits": {} <1>
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 03:41:55 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-12-03 12:18:34 -05:00
|
|
|
<1> The inner hit definition in the nested query. No other options need to be defined.
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
An example of a response snippet that could be generated from the above search request:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2014-10-13 03:41:55 -04:00
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
{
|
|
|
|
...,
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2017-06-16 05:13:23 -04:00
|
|
|
"max_score": 1.0,
|
2017-05-04 17:30:54 -04:00
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2014-10-13 03:41:55 -04:00
|
|
|
"_id": "1",
|
2017-06-16 05:13:23 -04:00
|
|
|
"_score": 1.0,
|
2014-10-13 03:41:55 -04:00
|
|
|
"_source": ...,
|
|
|
|
"inner_hits": {
|
2017-05-04 17:30:54 -04:00
|
|
|
"comments": { <1>
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2017-06-16 05:13:23 -04:00
|
|
|
"max_score": 1.0,
|
2017-05-04 17:30:54 -04:00
|
|
|
"hits": [
|
|
|
|
{
|
2017-11-01 02:34:14 -04:00
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-11-01 02:34:14 -04:00
|
|
|
"_id": "1",
|
2017-05-04 17:30:54 -04:00
|
|
|
"_nested": {
|
|
|
|
"field": "comments",
|
|
|
|
"offset": 1
|
|
|
|
},
|
2017-06-16 05:13:23 -04:00
|
|
|
"_score": 1.0,
|
2017-05-04 17:30:54 -04:00
|
|
|
"_source": {
|
2017-10-12 05:29:01 -04:00
|
|
|
"author": "nik9000",
|
|
|
|
"number": 2
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 03:41:55 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 03:41:55 -04:00
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
|
|
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2014-12-03 12:18:34 -05:00
|
|
|
<1> The name used in the inner hit definition in the search request. A custom key can be used via the `name` option.
|
|
|
|
|
2014-10-13 03:41:55 -04:00
|
|
|
The `_nested` metadata is crucial in the above example, because it defines from what inner nested object this inner hit
|
|
|
|
came from. The `field` defines the object array field the nested hit is from and the `offset` relative to its location
|
|
|
|
in the `_source`. Due to sorting and scoring the actual location of the hit objects in the `inner_hits` is usually
|
|
|
|
different than the location a nested inner object was defined.
|
|
|
|
|
|
|
|
By default the `_source` is returned also for the hit objects in `inner_hits`, but this can be changed. Either via
|
|
|
|
`_source` filtering feature part of the source can be returned or be disabled. If stored fields are defined on the
|
|
|
|
nested level these can also be returned via the `fields` feature.
|
|
|
|
|
|
|
|
An important default is that the `_source` returned in hits inside `inner_hits` is relative to the `_nested` metadata.
|
|
|
|
So in the above example only the comment part is returned per nested hit and not the entire source of the top level
|
2016-02-09 05:07:32 -05:00
|
|
|
document that contained the comment.
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2017-04-28 05:09:24 -04:00
|
|
|
[[nested-inner-hits-source]]
|
2019-07-19 14:35:36 -04:00
|
|
|
===== Nested inner hits and +_source+
|
2017-04-28 05:09:24 -04:00
|
|
|
|
|
|
|
Nested document don't have a `_source` field, because the entire source of document is stored with the root document under
|
|
|
|
its `_source` field. To include the source of just the nested document, the source of the root document is parsed and just
|
|
|
|
the relevant bit for the nested document is included as source in the inner hit. Doing this for each matching nested document
|
|
|
|
has an impact on the time it takes to execute the entire search request, especially when `size` and the inner hits' `size`
|
2017-06-06 07:17:29 -04:00
|
|
|
are set higher than the default. To avoid the relatively expensive source extraction for nested inner hits, one can disable
|
2017-11-29 11:31:39 -05:00
|
|
|
including the source and solely rely on doc values fields. Like this:
|
2017-04-28 05:09:24 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2017-04-28 05:09:24 -04:00
|
|
|
--------------------------------------------------
|
2019-01-18 08:11:18 -05:00
|
|
|
PUT test
|
2017-04-28 05:09:24 -04:00
|
|
|
{
|
2017-05-04 17:30:54 -04:00
|
|
|
"mappings": {
|
2019-01-18 08:11:18 -05:00
|
|
|
"properties": {
|
|
|
|
"comments": {
|
|
|
|
"type": "nested"
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT test/_doc/1?refresh
|
2017-05-04 17:30:54 -04:00
|
|
|
{
|
|
|
|
"title": "Test title",
|
|
|
|
"comments": [
|
|
|
|
{
|
|
|
|
"author": "kimchy",
|
|
|
|
"text": "comment text"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"author": "nik9000",
|
|
|
|
"text": "words words words"
|
2017-04-28 05:09:24 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
POST test/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"nested": {
|
|
|
|
"path": "comments",
|
|
|
|
"query": {
|
|
|
|
"match": {"comments.text" : "words"}
|
|
|
|
},
|
|
|
|
"inner_hits": {
|
|
|
|
"_source" : false,
|
2018-05-23 08:39:04 -04:00
|
|
|
"docvalue_fields" : [
|
2019-01-30 04:31:51 -05:00
|
|
|
"comments.text.keyword"
|
2018-05-23 08:39:04 -04:00
|
|
|
]
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 05:09:24 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
|
|
|
|
////
|
2017-04-28 05:09:24 -04:00
|
|
|
|
2017-05-04 17:30:54 -04:00
|
|
|
Response not included in text but tested for completeness sake.
|
2017-04-28 05:09:24 -04:00
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2017-04-28 05:09:24 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2017-05-04 17:30:54 -04:00
|
|
|
...,
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2018-09-06 08:42:06 -04:00
|
|
|
"max_score": 1.0444684,
|
2017-05-04 17:30:54 -04:00
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-05-04 17:30:54 -04:00
|
|
|
"_id": "1",
|
2018-09-06 08:42:06 -04:00
|
|
|
"_score": 1.0444684,
|
2017-05-04 17:30:54 -04:00
|
|
|
"_source": ...,
|
|
|
|
"inner_hits": {
|
|
|
|
"comments": { <1>
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2018-09-06 08:42:06 -04:00
|
|
|
"max_score": 1.0444684,
|
2017-05-04 17:30:54 -04:00
|
|
|
"hits": [
|
|
|
|
{
|
2017-11-01 02:34:14 -04:00
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-11-01 02:34:14 -04:00
|
|
|
"_id": "1",
|
2017-05-04 17:30:54 -04:00
|
|
|
"_nested": {
|
|
|
|
"field": "comments",
|
|
|
|
"offset": 1
|
|
|
|
},
|
2018-09-06 08:42:06 -04:00
|
|
|
"_score": 1.0444684,
|
2017-05-04 17:30:54 -04:00
|
|
|
"fields": {
|
2017-11-29 11:31:39 -05:00
|
|
|
"comments.text.keyword": [
|
2017-05-04 17:30:54 -04:00
|
|
|
"words words words"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-04-28 05:09:24 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
2017-04-28 05:09:24 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2017-04-28 05:09:24 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
|
|
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|
|
|
|
|
|
|
|
////
|
2017-04-28 05:09:24 -04:00
|
|
|
|
2015-01-12 07:11:46 -05:00
|
|
|
[[hierarchical-nested-inner-hits]]
|
2019-07-19 14:35:36 -04:00
|
|
|
===== Hierarchical levels of nested object fields and inner hits.
|
2015-01-12 07:11:46 -05:00
|
|
|
|
|
|
|
If a mapping has multiple levels of hierarchical nested object fields each level can be accessed via dot notated path.
|
|
|
|
For example if there is a `comments` nested field that contains a `votes` nested field and votes should directly be returned
|
2016-02-09 05:07:32 -05:00
|
|
|
with the root hits then the following path can be defined:
|
2015-01-12 07:11:46 -05:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2015-01-12 07:11:46 -05:00
|
|
|
--------------------------------------------------
|
2019-01-18 08:11:18 -05:00
|
|
|
PUT test
|
2015-01-12 07:11:46 -05:00
|
|
|
{
|
2017-05-04 17:30:54 -04:00
|
|
|
"mappings": {
|
2019-01-18 08:11:18 -05:00
|
|
|
"properties": {
|
|
|
|
"comments": {
|
|
|
|
"type": "nested",
|
|
|
|
"properties": {
|
|
|
|
"votes": {
|
|
|
|
"type": "nested"
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-12 07:11:46 -05:00
|
|
|
}
|
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT test/_doc/1?refresh
|
2017-05-04 17:30:54 -04:00
|
|
|
{
|
|
|
|
"title": "Test title",
|
|
|
|
"comments": [
|
|
|
|
{
|
|
|
|
"author": "kimchy",
|
|
|
|
"text": "comment text",
|
|
|
|
"votes": []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"author": "nik9000",
|
|
|
|
"text": "words words words",
|
|
|
|
"votes": [
|
|
|
|
{"value": 1 , "voter": "kimchy"},
|
|
|
|
{"value": -1, "voter": "other"}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
POST test/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"nested": {
|
|
|
|
"path": "comments.votes",
|
|
|
|
"query": {
|
|
|
|
"match": {
|
|
|
|
"comments.votes.voter": "kimchy"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"inner_hits" : {}
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 07:11:46 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
|
|
|
|
Which would look like:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2017-05-04 17:30:54 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
...,
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2019-12-10 12:04:58 -05:00
|
|
|
"max_score": 0.6931471,
|
2017-05-04 17:30:54 -04:00
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-05-04 17:30:54 -04:00
|
|
|
"_id": "1",
|
2019-12-10 12:04:58 -05:00
|
|
|
"_score": 0.6931471,
|
2017-05-04 17:30:54 -04:00
|
|
|
"_source": ...,
|
|
|
|
"inner_hits": {
|
|
|
|
"comments.votes": { <1>
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2019-12-10 12:04:58 -05:00
|
|
|
"max_score": 0.6931471,
|
2017-05-04 17:30:54 -04:00
|
|
|
"hits": [
|
|
|
|
{
|
2017-11-01 02:34:14 -04:00
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-11-01 02:34:14 -04:00
|
|
|
"_id": "1",
|
2017-05-04 17:30:54 -04:00
|
|
|
"_nested": {
|
|
|
|
"field": "comments",
|
|
|
|
"offset": 1,
|
|
|
|
"_nested": {
|
|
|
|
"field": "votes",
|
|
|
|
"offset": 0
|
|
|
|
}
|
|
|
|
},
|
2019-12-10 12:04:58 -05:00
|
|
|
"_score": 0.6931471,
|
2017-05-04 17:30:54 -04:00
|
|
|
"_source": {
|
2017-10-12 05:29:01 -04:00
|
|
|
"value": 1,
|
|
|
|
"voter": "kimchy"
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
|
|
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|
2015-01-12 07:11:46 -05:00
|
|
|
|
|
|
|
This indirect referencing is only supported for nested inner hits.
|
|
|
|
|
2014-10-13 03:41:55 -04:00
|
|
|
[[parent-child-inner-hits]]
|
2019-07-19 14:35:36 -04:00
|
|
|
===== Parent/child inner hits
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2017-06-16 05:13:23 -04:00
|
|
|
The parent/child `inner_hits` can be used to include parent or child:
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-10-13 03:41:55 -04:00
|
|
|
--------------------------------------------------
|
2019-01-18 08:11:18 -05:00
|
|
|
PUT test
|
2014-10-13 03:41:55 -04:00
|
|
|
{
|
2017-05-04 17:30:54 -04:00
|
|
|
"mappings": {
|
2019-01-18 08:11:18 -05:00
|
|
|
"properties": {
|
|
|
|
"my_join_field": {
|
|
|
|
"type": "join",
|
|
|
|
"relations": {
|
|
|
|
"my_parent": "my_child"
|
2017-06-16 05:13:23 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT test/_doc/1?refresh
|
2017-05-04 17:30:54 -04:00
|
|
|
{
|
2017-06-16 05:13:23 -04:00
|
|
|
"number": 1,
|
|
|
|
"my_join_field": "my_parent"
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT test/_doc/2?routing=1&refresh
|
2017-05-04 17:30:54 -04:00
|
|
|
{
|
2017-06-16 05:13:23 -04:00
|
|
|
"number": 1,
|
|
|
|
"my_join_field": {
|
|
|
|
"name": "my_child",
|
|
|
|
"parent": "1"
|
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
POST test/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"has_child": {
|
|
|
|
"type": "my_child",
|
|
|
|
"query": {
|
|
|
|
"match": {
|
2017-06-16 05:13:23 -04:00
|
|
|
"number": 1
|
2014-10-13 03:41:55 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
},
|
|
|
|
"inner_hits": {} <1>
|
2014-10-13 03:41:55 -04:00
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
2014-10-13 03:41:55 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-12-03 12:18:34 -05:00
|
|
|
<1> The inner hit definition like in the nested example.
|
2014-10-13 03:41:55 -04:00
|
|
|
|
|
|
|
An example of a response snippet that could be generated from the above search request:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2014-10-13 03:41:55 -04:00
|
|
|
--------------------------------------------------
|
2017-05-04 17:30:54 -04:00
|
|
|
{
|
2017-06-16 05:13:23 -04:00
|
|
|
...,
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2017-06-16 05:13:23 -04:00
|
|
|
"max_score": 1.0,
|
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-06-16 05:13:23 -04:00
|
|
|
"_id": "1",
|
|
|
|
"_score": 1.0,
|
|
|
|
"_source": {
|
|
|
|
"number": 1,
|
|
|
|
"my_join_field": "my_parent"
|
|
|
|
},
|
|
|
|
"inner_hits": {
|
|
|
|
"my_child": {
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2017-06-16 05:13:23 -04:00
|
|
|
"max_score": 1.0,
|
|
|
|
"hits": [
|
|
|
|
{
|
2017-11-01 02:34:14 -04:00
|
|
|
"_index": "test",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-06-16 05:13:23 -04:00
|
|
|
"_id": "2",
|
|
|
|
"_score": 1.0,
|
|
|
|
"_routing": "1",
|
|
|
|
"_source": {
|
|
|
|
"number": 1,
|
|
|
|
"my_join_field": {
|
|
|
|
"name": "my_child",
|
|
|
|
"parent": "1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 05:13:23 -04:00
|
|
|
]
|
|
|
|
}
|
2017-05-04 17:30:54 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
|
|
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|