我们在项目中使用了许多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节点,然后向其发送请求。 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();