(这是一个小系列:请戳:Elasticsearch之Nested(嵌套)系列,查看其他nested相关文章)
Because nested objects are indexed as separate hidden documents, we can’t query them directly.Instead,
we have to use the nested query or nested filter to access them:
nested object作为一个独立隐藏文档单独建索引,因此,我们不能直接查询它们。取而代之,我们必须使用nested查询或者nested filter来接触它们:
curl -XGET 'localhost:9200/my_index' -d ' { "query":{ "bool":{ "must":[ {"match":{"title":"eggs"}}, { "nested":{ "path":"comments", "query":{ "bool":{ "must":[ {"match":{"comments.name":"john"}}, {"match":{"comments.age":28}} ] } } } } ] } } }
A nested field can contain other nested fields. Similarly, a nested query can contain other nested queries.
The nesting hierarchy is applied as you would expect.
一个nested字段可以包含其他的nested 字段。相似地,一个nested查询可以包含其他nested查询。只要你希望,你就可以使用嵌套层。
Of course, a nested query could match several nested documents. Each matching nested document would have its own
relevance score, but these multiple scores need to be reduced to a single score that can be applied to the root document.
当然,一个nested查询可以匹配多个nested文本。每个匹配的nested文本都有它自己相关评分,但是这些评分必须归为一个总分应用于根文本上。
By default, it averages the scores of the matching nested documents. This can be controlled by setting the score_mode
parameter to avg, max, sum, or even none (in which case the root document gets a constant score of 1.0).
默认会平均所有匹配的nested文本的分数。当然,也可以通过设定score_modec参数为avg,max,sum,或者甚至为none(根文本获得一致评分1.0)。
curl -XGET 'localhost:9200/my_index' -d ' { "query":{ "bool":{ "must":[ {"match":{"title":"eggs"}}, { "nested":{ "path":"comments", "score_mode":"max", "query":{ "bool":{ "must":[ {"match":{"comments.name":"john"}}, {"match":{"comments.age":28}} ] } } } } ] } } }
A nested filter behaves much like a nested query, except that it doesn’t accept the score_mode parameter. It can
be used only in filter context—such as inside a filtered query—and it behaves like any other filter:
it includes or excludes, but it doesn’t score.
一个nested过滤行为和一个nested 查询非常像,除了它不接受score_mode。它只会用在过滤场景中——比如一个过滤查询中——它的行为同样类似其他过滤:
要么包括,要么不包括,不会评分。
原文:http://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html