4

We use many ScriptQuery and ScriptField in our project, and provide inline scripts for them in painless language.

My question is, how can we validate these painless scripts? In other words, how to make sure that they will compile?

The approach we use today is to test them out locally through Kibana. However, this is manual, error prone and not scalable. I am looking for a programmatic way or utility to validate painless scripts, so that I can plug it into the CI/CD pipeline. Is the compiler Elasticsearch uses under the hood open source ? Or is there some other way?

Elasticsearch version 5.4

Sample Query in Kibana with Painless script used for ScriptField and 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"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Sunil Purushothaman
  • 8,435
  • 1
  • 22
  • 20

3 Answers3

0

An integration test can start an Elasticsearch node on the local computer, and then send requests to it. The embedded-elasticsearch library can conveniently download a specified Elasticsearch version and start it in a separate process.

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();
Chin Huang
  • 12,912
  • 4
  • 46
  • 47
0

elasticserach support dev tools to validate scripts, by using this tool you can compile scrips and if its not compiled it will show you validation error.

https://www.elastic.co/guide/en/kibana/current/devtools-kibana.html

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31220464) – TallChuck Mar 11 '22 at 05:17
0

Elasticsearch now exposes a Painless execute API for this purpose:

The Painless execute API runs a script and returns a result.

Use this API to build and test scripts, such as when defining a script for a runtime field.

The API is in technical preview as of version 8.5. See here for the related Opensearch API docs.

Aleksi
  • 4,483
  • 33
  • 45