嵌套-查询
查询嵌套对象
因嵌套对象(nested objects)会被索引为分离的隐藏文档,我们不能直接查询它们。而是使用 nested查询或 nested 过滤器来存取它们:
GET /my_index/blogpost/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "eggs" }}, <1>
{
"nested": {
"path": "comments", <2>
"query": {
"bool": {
"must": [ <3>
{ "match": { "comments.name": "john" }},
{ "match": { "comments.age": 28 }}
]
}}}}
]
}}}
<1> title条件运作在根文档上1>
<2> nested条件深入嵌套的comments栏位。它不会在存取根文档的栏位,或是其他嵌套文档的栏位。2>
<3> comments.name以及comments.age运作在相同的嵌套文档。3>
TIP
一个
nested栏位可以包含其他nested栏位。 相同的,一个nested查询可以包含其他nested查询。 嵌套阶层会如同你预期的运作。
当然,一个nested查询可以匹配多个嵌套文档。
每个文档的匹配会有各自的关联分数,但多个分数必须减少至单一分数才能应用至根文档。
在预设中,它会平均所有嵌套文档匹配的分数。这可以藉由设定score_mode参数为avg, max, sum或甚至none(为了防止根文档永远获得1.0的匹配分数时)来控制。
GET /my_index/blogpost/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "eggs" }},
{
"nested": {
"path": "comments",
"score_mode": "max", <1>
"query": {
"bool": {
"must": [
{ "match": { "comments.name": "john" }},
{ "match": { "comments.age": 28 }}
]
}}}}
]
}}}
<1> 从最匹配的嵌套文档中给予根文档的_score值。1>
注意
nested过滤器类似於nested查询,除了无法使用score_mode参数。 只能使用在filter context—例如在filtered查询中--其作用类似其他的过滤器: 包含或不包含,但不评分。
nested过滤器的结果本身不会缓存,通常缓存规则会被应用於nested过滤器之中的过滤器。