mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-23 05:15:04 +00:00
CONSOLEify inner hits docs
Rewrites most of the snippets in the `innert_hits` docs to be complete examples and enables `VIEW IN CONSOLE`, `COPY AS CURL`, and automatic testing of the snippets.
This commit is contained in:
parent
9bc7e210a0
commit
9f431543fc
@ -59,7 +59,6 @@ buildRestTests.expectedUnconvertedCandidates = [
|
|||||||
'reference/modules/cross-cluster-search.asciidoc', // this is hard to test since we need 2 clusters -- maybe we can trick it into referencing itself...
|
'reference/modules/cross-cluster-search.asciidoc', // this is hard to test since we need 2 clusters -- maybe we can trick it into referencing itself...
|
||||||
'reference/search/field-stats.asciidoc',
|
'reference/search/field-stats.asciidoc',
|
||||||
'reference/search/profile.asciidoc',
|
'reference/search/profile.asciidoc',
|
||||||
'reference/search/request/inner-hits.asciidoc',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
integTestCluster {
|
integTestCluster {
|
||||||
|
@ -22,6 +22,7 @@ The structure looks like this:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// NOTCONSOLE
|
||||||
|
|
||||||
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:
|
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:
|
||||||
|
|
||||||
@ -52,6 +53,7 @@ If `inner_hits` is defined on a query that supports it then each search hit will
|
|||||||
...
|
...
|
||||||
]
|
]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// NOTCONSOLE
|
||||||
|
|
||||||
==== Options
|
==== Options
|
||||||
|
|
||||||
@ -80,22 +82,50 @@ Inner hits also supports the following per document features:
|
|||||||
|
|
||||||
The nested `inner_hits` can be used to include nested inner objects as inner hits to a search hit.
|
The nested `inner_hits` can be used to include nested inner objects as inner hits to a search hit.
|
||||||
|
|
||||||
The example below assumes that there is a nested object field defined with the name `comments`:
|
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
PUT test
|
||||||
{
|
{
|
||||||
"query" : {
|
"mappings": {
|
||||||
"nested" : {
|
"doc": {
|
||||||
"path" : "comments",
|
"properties": {
|
||||||
"query" : {
|
"comments": {
|
||||||
"match" : {"comments.message" : "[actual query]"}
|
"type": "nested"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PUT test/doc/1?refresh
|
||||||
|
{
|
||||||
|
"title": "Test title",
|
||||||
|
"comments": [
|
||||||
|
{
|
||||||
|
"author": "kimchy",
|
||||||
|
"text": "comment text"
|
||||||
},
|
},
|
||||||
"inner_hits" : {} <1>
|
{
|
||||||
|
"author": "nik9000",
|
||||||
|
"text": "words words words"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
POST test/_search
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"nested": {
|
||||||
|
"path": "comments",
|
||||||
|
"query": {
|
||||||
|
"match": {"comments.text" : "words"}
|
||||||
|
},
|
||||||
|
"inner_hits": {} <1>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
<1> The inner hit definition in the nested query. No other options need to be defined.
|
<1> The inner hit definition in the nested query. No other options need to be defined.
|
||||||
|
|
||||||
@ -103,35 +133,46 @@ An example of a response snippet that could be generated from the above search r
|
|||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
...
|
{
|
||||||
"hits": {
|
...,
|
||||||
...
|
"hits": {
|
||||||
|
"total": 1,
|
||||||
|
"max_score": 0.9651416,
|
||||||
"hits": [
|
"hits": [
|
||||||
{
|
{
|
||||||
"_index": "my-index",
|
"_index": "test",
|
||||||
"_type": "question",
|
"_type": "doc",
|
||||||
"_id": "1",
|
"_id": "1",
|
||||||
|
"_score": 0.9651416,
|
||||||
"_source": ...,
|
"_source": ...,
|
||||||
"inner_hits": {
|
"inner_hits": {
|
||||||
"comments": { <1>
|
"comments": { <1>
|
||||||
"hits": {
|
"hits": {
|
||||||
"total": ...,
|
"total": 1,
|
||||||
|
"max_score": 0.9651416,
|
||||||
"hits": [
|
"hits": [
|
||||||
{
|
{
|
||||||
"_nested": {
|
"_nested": {
|
||||||
"field": "comments",
|
"field": "comments",
|
||||||
"offset": 2
|
"offset": 1
|
||||||
},
|
},
|
||||||
"_source": ...
|
"_score": 0.9651416,
|
||||||
},
|
"_source": {
|
||||||
...
|
"author": "nik9000",
|
||||||
|
"text": "words words words"
|
||||||
|
}
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
...
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
||||||
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|
||||||
|
|
||||||
<1> The name used in the inner hit definition in the search request. A custom key can be used via the `name` option.
|
<1> The name used in the inner hit definition in the search request. A custom key can be used via the `name` option.
|
||||||
|
|
||||||
@ -156,39 +197,53 @@ its `_source` field. To include the source of just the nested document, the sour
|
|||||||
the relevant bit for the nested document is included as source in the inner hit. Doing this for each matching nested document
|
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`
|
has an impact on the time it takes to execute the entire search request, especially when `size` and the inner hits' `size`
|
||||||
are set higher than the default. To avoid the relative expensive source extraction for nested inner hits, one can disable
|
are set higher than the default. To avoid the relative expensive source extraction for nested inner hits, one can disable
|
||||||
including the source and solely rely on stored fields.
|
including the source and solely rely on stored fields. Like this:
|
||||||
|
|
||||||
Enabled stored field for fields under the nested object field in your mapping:
|
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
PUT test
|
||||||
{
|
{
|
||||||
|
"mappings": {
|
||||||
|
"doc": {
|
||||||
"properties": {
|
"properties": {
|
||||||
"comment": {
|
"comments": {
|
||||||
"type": "comments",
|
"type": "nested",
|
||||||
"properties" : {
|
"properties": {
|
||||||
"message" : {
|
"text": {
|
||||||
"type" : "text",
|
"type": "text",
|
||||||
"store" : true
|
"store": true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
|
||||||
|
|
||||||
Disable including source and include specific stored fields in the inner hits definition:
|
PUT test/doc/1?refresh
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
{
|
{
|
||||||
"query" : {
|
"title": "Test title",
|
||||||
"nested" : {
|
"comments": [
|
||||||
"path" : "comments",
|
{
|
||||||
"query" : {
|
"author": "kimchy",
|
||||||
"match" : {"comments.message" : "[actual query]"}
|
"text": "comment text"
|
||||||
},
|
},
|
||||||
"inner_hits" : {
|
{
|
||||||
|
"author": "nik9000",
|
||||||
|
"text": "words words words"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
POST test/_search
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"nested": {
|
||||||
|
"path": "comments",
|
||||||
|
"query": {
|
||||||
|
"match": {"comments.text" : "words"}
|
||||||
|
},
|
||||||
|
"inner_hits": {
|
||||||
"_source" : false,
|
"_source" : false,
|
||||||
"stored_fields" : ["comments.text"]
|
"stored_fields" : ["comments.text"]
|
||||||
}
|
}
|
||||||
@ -196,6 +251,57 @@ Disable including source and include specific stored fields in the inner hits de
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
Response not included in text but tested for completeness sake.
|
||||||
|
|
||||||
|
[source,js]
|
||||||
|
--------------------------------------------------
|
||||||
|
{
|
||||||
|
...,
|
||||||
|
"hits": {
|
||||||
|
"total": 1,
|
||||||
|
"max_score": 0.9651416,
|
||||||
|
"hits": [
|
||||||
|
{
|
||||||
|
"_index": "test",
|
||||||
|
"_type": "doc",
|
||||||
|
"_id": "1",
|
||||||
|
"_score": 0.9651416,
|
||||||
|
"_source": ...,
|
||||||
|
"inner_hits": {
|
||||||
|
"comments": { <1>
|
||||||
|
"hits": {
|
||||||
|
"total": 1,
|
||||||
|
"max_score": 0.9651416,
|
||||||
|
"hits": [
|
||||||
|
{
|
||||||
|
"_nested": {
|
||||||
|
"field": "comments",
|
||||||
|
"offset": 1
|
||||||
|
},
|
||||||
|
"_score": 0.9651416,
|
||||||
|
"fields": {
|
||||||
|
"comments.text": [
|
||||||
|
"words words words"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
||||||
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
[[hierarchical-nested-inner-hits]]
|
[[hierarchical-nested-inner-hits]]
|
||||||
==== Hierarchical levels of nested object fields and inner hits.
|
==== Hierarchical levels of nested object fields and inner hits.
|
||||||
@ -206,16 +312,113 @@ with the root hits then the following path can be defined:
|
|||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
PUT test
|
||||||
{
|
{
|
||||||
"query" : {
|
"mappings": {
|
||||||
"nested" : {
|
"doc": {
|
||||||
"path" : "comments.votes",
|
"properties": {
|
||||||
"query" : { ... },
|
"comments": {
|
||||||
|
"type": "nested",
|
||||||
|
"properties": {
|
||||||
|
"message": {
|
||||||
|
"type": "text",
|
||||||
|
"store": true
|
||||||
|
},
|
||||||
|
"votes": {
|
||||||
|
"type": "nested"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PUT test/doc/1?refresh
|
||||||
|
{
|
||||||
|
"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" : {}
|
"inner_hits" : {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
|
Which would look like:
|
||||||
|
|
||||||
|
[source,js]
|
||||||
|
--------------------------------------------------
|
||||||
|
{
|
||||||
|
...,
|
||||||
|
"hits": {
|
||||||
|
"total": 1,
|
||||||
|
"max_score": 0.6931472,
|
||||||
|
"hits": [
|
||||||
|
{
|
||||||
|
"_index": "test",
|
||||||
|
"_type": "doc",
|
||||||
|
"_id": "1",
|
||||||
|
"_score": 0.6931472,
|
||||||
|
"_source": ...,
|
||||||
|
"inner_hits": {
|
||||||
|
"comments.votes": { <1>
|
||||||
|
"hits": {
|
||||||
|
"total": 1,
|
||||||
|
"max_score": 0.6931472,
|
||||||
|
"hits": [
|
||||||
|
{
|
||||||
|
"_nested": {
|
||||||
|
"field": "comments",
|
||||||
|
"offset": 1,
|
||||||
|
"_nested": {
|
||||||
|
"field": "votes",
|
||||||
|
"offset": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"_score": 0.6931472,
|
||||||
|
"_source": {
|
||||||
|
"value": 1,
|
||||||
|
"voter": "kimchy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
||||||
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|
||||||
|
|
||||||
This indirect referencing is only supported for nested inner hits.
|
This indirect referencing is only supported for nested inner hits.
|
||||||
|
|
||||||
@ -224,22 +427,49 @@ This indirect referencing is only supported for nested inner hits.
|
|||||||
|
|
||||||
The parent/child `inner_hits` can be used to include parent or child
|
The parent/child `inner_hits` can be used to include parent or child
|
||||||
|
|
||||||
The examples below assumes that there is a `_parent` field mapping in the `comment` type:
|
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
PUT test
|
||||||
{
|
{
|
||||||
"query" : {
|
"settings": {
|
||||||
"has_child" : {
|
"mapping.single_type": false
|
||||||
"type" : "comment",
|
|
||||||
"query" : {
|
|
||||||
"match" : {"message" : "[actual query]"}
|
|
||||||
},
|
},
|
||||||
"inner_hits" : {} <1>
|
"mappings": {
|
||||||
|
"my_parent": {},
|
||||||
|
"my_child": {
|
||||||
|
"_parent": {
|
||||||
|
"type": "my_parent"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PUT test/my_parent/1?refresh
|
||||||
|
{
|
||||||
|
"test": "test"
|
||||||
|
}
|
||||||
|
|
||||||
|
PUT test/my_child/1?parent=1&refresh
|
||||||
|
{
|
||||||
|
"test": "test"
|
||||||
|
}
|
||||||
|
|
||||||
|
POST test/_search
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"has_child": {
|
||||||
|
"type": "my_child",
|
||||||
|
"query": {
|
||||||
|
"match": {
|
||||||
|
"test": "test"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"inner_hits": {} <1>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
<1> The inner hit definition like in the nested example.
|
<1> The inner hit definition like in the nested example.
|
||||||
|
|
||||||
@ -247,30 +477,42 @@ An example of a response snippet that could be generated from the above search r
|
|||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
...
|
{
|
||||||
"hits": {
|
...,
|
||||||
...
|
"hits": {
|
||||||
|
"total": 1,
|
||||||
|
"max_score": 1.0,
|
||||||
"hits": [
|
"hits": [
|
||||||
{
|
{
|
||||||
"_index": "my-index",
|
"_index": "test",
|
||||||
"_type": "question",
|
"_type": "my_parent",
|
||||||
"_id": "1",
|
"_id": "1",
|
||||||
|
"_score": 1.0,
|
||||||
"_source": ...,
|
"_source": ...,
|
||||||
"inner_hits": {
|
"inner_hits": {
|
||||||
"comment": {
|
"my_child": {
|
||||||
"hits": {
|
"hits": {
|
||||||
"total": ...,
|
"total": 1,
|
||||||
|
"max_score": 0.18232156,
|
||||||
"hits": [
|
"hits": [
|
||||||
{
|
{
|
||||||
"_type": "comment",
|
"_type": "my_child",
|
||||||
"_id": "5",
|
"_id": "1",
|
||||||
"_source": ...
|
"_score": 0.18232156,
|
||||||
},
|
"_routing": "1",
|
||||||
...
|
"_parent": "1",
|
||||||
|
"_source": {
|
||||||
|
"test": "test"
|
||||||
|
}
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
...
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// TESTRESPONSE[s/"_source": \.\.\./"_source": $body.hits.hits.0._source/]
|
||||||
|
// TESTRESPONSE[s/\.\.\./"timed_out": false, "took": $body.took, "_shards": $body._shards/]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user