如何验证Elasticsearch无痛脚本?

问题描述 投票:1回答:1

我们在项目中使用了许多ScriptQuery和ScriptField,并以无痛语言为它们提供内联脚本。

我的问题是,我们如何验证这些无痛脚本?换句话说,如何确保它们会编译?

我们今天使用的方法是通过Kibana在本地测试它们。但是,这是手动的,容易出错且无法扩展。我正在寻找一种编程方式或实用程序来验证无痛脚本,以便我可以将其插入CI / CD管道。编译器Elasticsearch是否在开源下使用?或者还有其他方式吗?

Elasticsearch版本5.4

使用用于ScriptField和ScriptQuery的无痛脚本的Kibana中的示例查询

GET myIndex/_search
{
  "script_fields": {
    "LastName": {
      "script": {
        "inline": "if(doc['Author']!= null && doc['Author'].value != null){return doc['Author'].value.toUpperCase();}return null;",
        "lang": "painless"
      }
    }
  },
  "query": {
    "bool": {
      "must_not": [
        {
          "bool": {
            "must": [
              {
                "script": {
                  "script": {
                    "inline": "def lastName = '';if(doc['Author']!= null && doc['Author'].value != null){lastName = doc['Author'].value.toUpperCase();}if(doc.containsKey('LastName') && doc['LastName']!= null && doc['LastName'].value != null){lastName = doc['LastName'].value;}return (lastName.toLowerCase().startsWith(params.SearchParam.toLowerCase()));",
                    "lang": "painless",
                    "params": {
                      "SearchParam": "d"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
elasticsearch compilation kibana elasticsearch-5 elasticsearch-painless
1个回答
0
投票

集成测试可以在本地计算机上启动Elasticsearch节点,然后向其发送请求。 embedded-elasticsearch库可以方便地下载指定的Elasticsearch版本并在单独的过程中启动它。

embeddedElastic = EmbeddedElastic.builder()
    .withElasticVersion(Version.CURRENT.number())
    .withSetting(PopularProperties.CLUSTER_NAME, CLUSTER_NAME)
    .withSetting(PopularProperties.TRANSPORT_TCP_PORT, transportTcpPort)
    .withIndex(INDEX_NAME, IndexSettings.builder()
        .withSettings(getClass().getResourceAsStream("settings-mappings.json"))
        .build())
    .build()
    .start();
© www.soinside.com 2019 - 2024. All rights reserved.