问题描述
假设我有一个带有嵌套发布值的简化组织文档(ES 2.3):
<代码>{组织" : {更新日期":1395211600000,出版物":[{创建日期":1393801200000},{创建日期":1401055200000}]}}
我想找到所有出版日期创建时间
的组织.组织的日期更新:
<代码>{询问": {嵌套":{"path": "出版物",询问": {布尔":{筛选": [{脚本": {脚本":doc['publications.dateCreated'].value < doc['dateUpdated'].value"}}]}}}}}
我的问题是,当我执行嵌套查询时,嵌套查询无法访问根文档值,因此 doc['dateUpdated'].value
无效,我得到 0 个命中.
有没有办法将值传递到嵌套查询中?或者我的嵌套方法在这里完全关闭?如有必要,我希望避免仅为出版物创建单独的文档.
谢谢.
您无法从嵌套查询上下文访问根值.它们被索引为单独的文档.来自文档
<块引用>嵌套子句向下"进入嵌套的注释字段.它没有不再可以访问根文档中的字段,也不能访问任何其他嵌套文档.
你可以在的帮助下得到想要的结果copy_to 参数.另一种方法是使用 include_in_parent 或 include_in_root 但它们可能是 将来不推荐使用,它还会增加索引大小,因为每个嵌套类型的字段都将包含在根文档中,因此在这种情况下 copy_to
功能更好.
这是一个示例索引
PUTnested_index{映射":{博文":{特性": {根日期":{类型":日期"},copy_of_nested_date":{类型":日期"},评论": {类型":嵌套",特性": {嵌套日期":{类型":日期","copy_to": "copy_of_nested_date"}}}}}}}
这里 nested_date 的每个值都将被复制到 copy_of_nested_date 所以 copy_of_nested_date 看起来像 [1401055200000,1393801200000,1221542100000] 然后你可以像这样使用简单的查询得到结果.
<代码>{询问": {布尔":{筛选": [{脚本": {脚本":doc['rootdate'].value < doc['copy_of_nested_date'].value"}}]}}}
您不必更改嵌套结构,但在将 copy_to
添加到 publication dateCreated 后,您必须重新索引文档>
Suppose I have a simplified Organization document with nested publication values like so (ES 2.3):
{
"organization" : {
"dateUpdated" : 1395211600000,
"publications" : [
{
"dateCreated" : 1393801200000
},
{
"dateCreated" : 1401055200000
}
]
}
}
I want to find all Organizations that have a publication dateCreated < the organization's dateUpdated:
{
"query": {
"nested": {
"path": "publications",
"query": {
"bool": {
"filter": [
{
"script": {
"script": "doc['publications.dateCreated'].value < doc['dateUpdated'].value"
}
}
]
}
}
}
}
}
My problem is that when I perform a nested query, the nested query does not have access to the root document values, so doc['dateUpdated'].value
is invalid and I get 0 hits.
Is there a way to pass in a value into the nested query? Or is my nested approach completely off here? I would like to avoid creating a separate document just for publications if necessary.
Thanks.
You can not access the root values from nested query context. They are indexed as separate documents. From the documentation
The nested clause "steps down" into the nested comments field. It no longer has access to fields in the root document, nor fields in any other nested document.
You can get the desired results with the help of copy_to parameter. Another way to do this would be to use include_in_parent or include_in_root but they might be deprecated in future and it will also increase the index size as every field of nested type will be included in root document so in this case copy_to
functionality is better.
This is a sample index
PUT nested_index
{
"mappings": {
"blogpost": {
"properties": {
"rootdate": {
"type": "date"
},
"copy_of_nested_date": {
"type": "date"
},
"comments": {
"type": "nested",
"properties": {
"nested_date": {
"type": "date",
"copy_to": "copy_of_nested_date"
}
}
}
}
}
}
}
Here every value of nested_date will be copied to copy_of_nested_date so copy_of_nested_date will look something like [1401055200000,1393801200000,1221542100000] and then you could use simple query like this to get the results.
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": "doc['rootdate'].value < doc['copy_of_nested_date'].value"
}
}
]
}
}
}
You don't have to change your nested structure but you would have to reindex the documents after adding copy_to
to publication dateCreated
这篇关于涉及根值和嵌套值的 Elasticsearch 脚本查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2