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" } } } ] } } } } ] } }