弹性搜索:如何编写多语句脚本? [英] Elastic Search: How to write multi statement scripts?

查看:69
本文介绍了弹性搜索:如何编写多语句脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Elasticsearch索引中的文档上存储了值.
我需要对值进行一些日期操作,并返回要在过滤器中使用的布尔值.
该脚本涵盖了几行,但我无法运行它.

I have values stored on a document in an Elasticsearch index.
I need to do some date manipulation on the values and return a boolean value to be used in a filter.
The script covers several lines, and I can't get it to run.

我编写了其他可以正常工作的脚本,但是我对Groovy的了解甚少,而对Elastic search的了解却很少.

I've written other single scripts that work fine, however I know less than nothing about Groovy and very little about Elastic search.

我可以用脚本找到的每个样本只有一行,只有一行.

Each and every sample I can find with a script has one line and only one line.

所以基本上我将如何使用这个完全有效的脚本

So basically how would I take this perfectly valid script

"script": {
    "script": "doc['state'].value == 'completed' && doc['lastStateUpdate'].value < doc['dueDate'].value"
    }

将其转换为

"script": {
    "script": "def isCompleted = doc['state'].value == 'completed' 
               def preSLA = doc['lastStateUpdate'].value < doc['dueDate'].value
               return isCompleted && preSLA"
    }

我对创建一个只写一个表示逻辑的线性的想法并不感到生气,我可以看到更多这样的想法,尽管这个相对简单,但一个线性"却不是.不会削减它.

I'm not mad about the idea of creating a write-only one liner that expresses the logic, I can see more of these coming down the line and while this one is relatively straight-forward, a "one liner" isn't going to cut it.

这里的替代方法是在对文档建立索引之前对其进行一些预处理,然后向其中添加额外的数据.但是,这样做的缺点是它非常不灵活,我们需要重新索引所有数据以更改这些聚合,而我们不希望这样做.

The alternative here is to do some preprocessing on the document before it's indexed, and add extra data to it. However this has drawbacks in that it's rather inflexible and we'd need to reindex all the data to change these aggregations, which we'd rather not do.

推荐答案

您只需要用分号分隔每个语句:

You simply need to separate each statement with a semicolon:

"script": {
    "script": "isCompleted = doc['state'].value == 'completed'; preSLA = doc['lastStateUpdate'].value < doc['dueDate'].value; return isCompleted && preSLA;"
    }

但是请确保不要在脚本字符串中添加换行符,因为它不是有效的JSON.

Make sure to not add line breaks inside your script string, though, as it would not be valid JSON.

这篇关于弹性搜索:如何编写多语句脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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