Inner hits
The parent-join and nested 功能允许返回具有不同范围匹配的文档。在父/子案例中,基于子文档中的匹配返回父文档,或者基于父文档中的匹配返回子文档。在嵌套的情况下,基于嵌套内部对象中的匹配返回文档。 在这两种情况下,隐藏了导致文档返回的不同范围中的实际匹配。在许多情况下,知道哪些内部嵌套对象(在嵌套的情况下)或子/父文档(在父/子的情况下)返回某些信息非常有用。内部命中功能可用于此目的。此功能会在搜索响应中返回每次搜索命中,这会导致搜索匹配在不同范围内匹配。
可以通过在nested,has_child或has_parent查询和过滤器上定义inner_hits定义来使用内部命中。结构如下所示:
"<query>" : {
"inner_hits" : {
<inner_hits_options>
}
}
如果inner_hits
在支持它的查询上定义,则每个搜索命中将包含inner_hits
具有以下结构的json对象:
"hits": [
{
"_index": ...,
"_type": ...,
"_id": ...,
"inner_hits": {
"<inner_hits_name>": {
"hits": {
"total": ...,
"hits": [
{
"_type": ...,
"_id": ...,
...
},
...
]
}
}
},
...
},
...
]
选项
内部命中支持以下选项:
| |
| 每个返回的最大匹配数 |
| 应如何对内部命中进行排序 |
| 用于响应中特定内部命中定义的名称。在单个搜索请求中定义了多个内部命中时很有用。默认值取决于定义内部命中的查询。对于 |
内部命中还支持以下每个文档功能:
- Highlighting
- Explain
- Source filtering
- Script fields
- Doc value fields
- Include versions
Nested inner hits
嵌套的inner_hits可用于包括嵌套的内部对象作为搜索命中的内部命中。
PUT test
{
"mappings": {
"_doc": {
"properties": {
"comments": {
"type": "nested"
}
}
}
}
}
PUT test/_doc/1?refresh
{
"title": "Test title",
"comments": [
{
"author": "kimchy",
"number": 1
},
{
"author": "nik9000",
"number": 2
}
]
}
POST test/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"match": {"comments.number" : 2}
},
"inner_hits": {} ①
}
}
}
| 嵌套查询中的内部命中定义。没有其他选择需要定义。 |
可以从上述搜索请求生成的响应代码段示例:
{
...,
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": ...,
"inner_hits": {
"comments": { ①
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "comments",
"offset": 1
},
"_score": 1.0,
"_source": { ②
"author": "nik9000",
"number": 2
}
}
]
}
}
}
}
]
}
}
| 搜索请求中内部匹配定义中使用的名称。可以通过该 |
在上述示例中,嵌套元数据是至关重要的,因为它定义了从内部嵌套对象中产生的内部嵌套对象。字段定义嵌套命中的对象数组字段和相对于其在源中的位置的偏移量。由于排序和评分,inner_hits中命中对象的实际位置通常与定义嵌套内部对象的位置不同。
默认情况下,在内命中命中对象也返回了_source,但这可以被改变。通过源过滤功能,可以返回或禁用源的一部分。如果在嵌套级别上定义了存储字段,那么这些字段也可以通过字段特性返回。
一个重要的默认值是,在内射命中中的命中返回的_source与嵌套的元数据相对应。因此,在上面的示例中,每次嵌套命中只返回注释部分,而不返回包含注释的顶级文档的整个源。
Hierarchical levels of nested object fields and inner hits.
如果映射具有多级分层嵌套对象字段,则可以通过点标记路径访问每个级别。例如,如果存在comments
包含votes
嵌套字段的嵌套字段,并且应该直接返回带有根命中的投票,则可以定义以下路径:
PUT test
{
"mappings": {
"_doc": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"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" : {}
}
}
}
看起来像是这样的:
{
...,
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 0.6931472,
"_source": ...,
"inner_hits": {
"comments.votes": {
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "comments",
"offset": 1,
"_nested": {
"field": "votes",
"offset": 0
}
},
"_score": 0.6931472,
"_source": {
"value": 1,
"voter": "kimchy"
}
}
]
}
}
}
}
]
}
}
仅对嵌套的内部命中支持此间接引用。
Parent/child inner hits
父/子inner_hits
可以用于包括父或子:
PUT test
{
"mappings": {
"_doc": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"my_parent": "my_child"
}
}
}
}
}
}
PUT test/_doc/1?refresh
{
"number": 1,
"my_join_field": "my_parent"
}
PUT test/_doc/2?routing=1&refresh
{
"number": 1,
"my_join_field": {
"name": "my_child",
"parent": "1"
}
}
POST test/_search
{
"query": {
"has_child": {
"type": "my_child",
"query": {
"match": {
"number": 1
}
},
"inner_hits": {}
}
}
}
| 内部命中定义,如嵌套示例中所示。 |
{
...,
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"number": 1,
"my_join_field": "my_parent"
},
"inner_hits": {
"my_child": {
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_routing": "1",
"_source": {
"number": 1,
"my_join_field": {
"name": "my_child",
"parent": "1"
}
}
}
]
}
}
}
}
]
}
}