inner hits:
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
{
"mappings": {
"_doc": {
"properties": {
"comments": {
"type": "nested"
}
}
}
}
}
'
curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d'
{
"title": "Test title",
"comments": [
{
"author": "kimchy",
"number": 1
},
{
"author": "nik9000",
"number": 2
}
]
}
'
inner hit 查询匹配在nested中描述。
curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d' { "query": { "nested": { "path": "comments", "query": { "match": {"comments.number" : 2} }, "inner_hits": {} } } } '
Nested inner hits and _source:在nested字段里面查询_source可能消耗很多时间,特别是size大于默认值的状况下,可以通过设置_source:false,查询fields字段的值减少这种消耗
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d' { "mappings": { "_doc": { "properties": { "comments": { "type": "nested" } } } } } ' curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d' { "title": "Test title", "comments": [ { "author": "kimchy", "text": "comment text" }, { "author": "nik9000", "text": "words words words" } ] } ' curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d' { "query": { "nested": { "path": "comments", "query": { "match": {"comments.text" : "words"} }, "inner_hits": { "_source" : false, "docvalue_fields" : ["comments.text.keyword"] } } } } '
Hierarchical levels of nested object fields and inner hits.(嵌套里面包含嵌套的状况)
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d' { "mappings": { "_doc": { "properties": { "comments": { "type": "nested", "properties": { "votes": { "type": "nested" } } } } } } } ' curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d' { "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"} ] } ] } ' curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d' { "query": { "nested": { "path": "comments.votes", "query": { "match": { "comments.votes.voter": "kimchy" } }, "inner_hits" : { "_source":false, "docvalue_field":["comments.votes.voter.keyword"]} } } } '
Parent/child inner hits(通过路由机制实现父子关系)
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d' { "mappings": { "_doc": { "properties": { "my_join_field": { "type": "join", "relations": { "my_parent": "my_child" } } } } } } ' curl -X PUT "localhost:9200/test/_doc/1?refresh" -H 'Content-Type: application/json' -d' { "number": 1, "my_join_field": "my_parent" } ' curl -X PUT "localhost:9200/test/_doc/2?routing=1&refresh" -H 'Content-Type: application/json' -d' { "number": 1, "my_join_field": { "name": "my_child", "parent": "1" } } ' curl -X POST "localhost:9200/test/_search" -H 'Content-Type: application/json' -d' { "query": { "has_child": { "type": "my_child", "query": { "match": { "number": 1 } }, "inner_hits": {} } } } '