Elasticsearch脚本查询涉及到根和嵌套值 [英] Elasticsearch script query involving root and nested values

查看:188
本文介绍了Elasticsearch脚本查询涉及到根和嵌套值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个简化的组织文档,嵌套的发布值如下(ES 2.3):

Suppose I have a simplified Organization document with nested publication values like so (ES 2.3):

{ 
  "organization" : { 
    "dateUpdated" : 1395211600000,

    "publications" : [ 
      { 
        "dateCreated" : 1393801200000
      },
      { 
        "dateCreated" : 1401055200000
      }
    ]
  }
}

我想查找发布dateCreated的所有组织组织的日期更新:

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

我的问题是,当我执行嵌套查询时,嵌套查询不能访问根文档值,所以 doc ['dateUpdated']。value 无效,我得到0点击。

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.

谢谢。

推荐答案

您无法从嵌套查询上下文中访问根值。它们被索引为单独的文档。从文档

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.

您可以借助 copy_to 参数。另一种方法是使用 include_in_parent include_in_root ,但它们可能是不推荐使用,并且还将增加索引大小,因为每个嵌套类型的字段都将包含在根文档中,因此在这种情况下 copy_to 功能更好。

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.

这是一个示例索引

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

这里,每个 nested_date 的值将被复制到 copy_of_nested_date ,所以copy_of_nested_date看起来像[1401055200000,1393801200000,1221542100000],然后你可以使用这样的简单查询来获得结果。

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

您不必更改嵌套结构,但在添加 reindex > copy_to to publication dateCreated

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脚本查询涉及到根和嵌套值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆