Inner hits
The parent-join and nested 功能容许返回具备不一样范围匹配的文档。在父/子案例中,基于子文档中的匹配返回父文档,或者基于父文档中的匹配返回子文档。在嵌套的状况下,基于嵌套内部对象中的匹配返回文档。 在这两种状况下,隐藏了致使文档返回的不一样范围中的实际匹配。在许多状况下,知道哪些内部嵌套对象(在嵌套的状况下)或子/父文档(在父/子的状况下)返回某些信息很是有用。内部命中功能可用于此目的。此功能会在搜索响应中返回每次搜索命中,这会致使搜索匹配在不一样范围内匹配。javascript
能够经过在nested,has_child或has_parent查询和过滤器上定义inner_hits定义来使用内部命中。结构以下所示:html
"<query>" : { "inner_hits" : { <inner_hits_options> } }
若是inner_hits
在支持它的查询上定义,则每一个搜索命中将包含inner_hits
具备如下结构的json对象:java
"hits": [ { "_index": ..., "_type": ..., "_id": ..., "inner_hits": { "<inner_hits_name>": { "hits": { "total": ..., "hits": [ { "_type": ..., "_id": ..., ... }, ... ] } } }, ... }, ... ]
选项
内部命中支持如下选项:json
|
|
|
每一个返回的最大匹配数 |
|
应如何对内部命中进行排序 |
|
用于响应中特定内部命中定义的名称。在单个搜索请求中定义了多个内部命中时颇有用。默认值取决于定义内部命中的查询。对于 |
内部命中还支持如下每一个文档功能:
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" } } } ] } } } } ] } }