【问题标题】:How to validate Elasticsearch Painless scripts?如何验证 Elasticsearch Painless 脚本?
【发布时间】:2018-06-13 18:18:37
【问题描述】:

我们在项目中使用了许多 ScriptQueryScriptField,并以简单的语言为它们提供内联脚本。

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

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

Elasticsearch 5.4 版

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

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】:

    集成测试可以在本地计算机上启动 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();
    

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多