Elasticsearch 5.4:在同一无痛脚本查询中使用普通字段和嵌套字段? [英] Elasticsearch 5.4: Use normal and nested fields in same Painless script query?

查看:73
本文介绍了Elasticsearch 5.4:在同一无痛脚本查询中使用普通字段和嵌套字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的映射:

{
  printings: { 
    type: "nested",
    properties: {
      prop1: {type: "number"}
    }  
  },
  prop2: {type: "number"}
}

然后我要构建一个无痛查询,如下所示:

I then want to build a Painless query like this:

"script": {
          "lang": "painless",
          "inline": "doc['prop1'] > (3 * doc['printings.prop2'])"
        }

但是在感觉不起作用。如果我用一个简单的数字替换嵌套的prop2,则它确实起作用。

However testing this in Sense doesn't work. If I replace the nested prop2 with a simple number then it does work. Is there a way to access both root and nested props in a single scripted query?

推荐答案

不幸的是,您不能从以下位置访问嵌套上下文: root,并且您无法从嵌套访问root上下文,因为嵌套文档是单独的文档,即使它们存储在父文档附近。但是您可以使用 copy_to 字段功能使用其他映射来解决它。这是一个映射:

Unfortunately, you cannot access nested context from root and you cannot access root context from nested because nested documents are separate documents, even though they are stored close to the parent. But you can solve it with a different mapping using copy_to field feature. Here is a mapping:

{
    "mappings": {
        "sample": {
            "properties": {
                "printings": {
                    "type": "nested",
                    "properties": {
                        "prop2": {
                            "type": "integer",
                            "copy_to": "child_prop2"
                        }
                    }
                },
                "prop1": {
                    "type": "integer"
                },
                "child_prop2": {
                    "type": "integer"
                }
            }
        }
    }
}

在这种情况下,嵌套文档中的值将被复制到父级。您不必显式填写此新字段,这是批量索引的一个示例:

In this case the values from nested documents will be copied to the parent. You don't have to explicitly fill this new field, here is ax example of bulk indexing:

POST http://localhost:9200/_bulk HTTP/1.1

{"index":{"_index":"my_index","_type":"sample","_id":null}}
{"printings":[{"prop2":1},{"prop2":4}],"prop1":2}
{"index":{"_index":"my_index","_type":"sample","_id":null}}
{"printings":[{"prop2":0},{"prop2":1}],"prop1":2}
{"index":{"_index":"my_index","_type":"sample","_id":null}}
{"printings":[{"prop2":1},{"prop2":0}],"prop1":2}

之后,您可以使用此查询

After that you can use this query

{
    "query": {
        "script": {
            "script": {
                "inline": "doc['prop1'].value > (3 * doc['child_prop2'].value)",
                "lang": "painless"
            }
        }
    }
}

第一个文档不匹配。第二个将与第一个子文档匹配。第三个将与第二个子文档匹配。

The first document won't match. The second one will match by the first subdocument. The third one will match by the second subdocument.

这篇关于Elasticsearch 5.4:在同一无痛脚本查询中使用普通字段和嵌套字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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