That script is invalid, which usually will cause a shard failure.
There is at least one curly bracket too much (I changed syntax styling above so you can more easily spot the additional closing curly bracket before the last else).
Also As long as you're field can contain null or not be present in some documents, you need to make sure you check for null on the .value before trying to regex compare it.
Could you please provide more information on the error ES is giving you. This should be possible by one of the following ways:
Use the Elasticsearch API to query your index directly and check the output.
You can use the Browsers Dev Tool (by pressing F12) to inspect the request made by Discover (should be an _msearch request) and copy the full response of that.
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[script] failed to parse field [source]",
"line": 9,
"col": 27
}
],
"type": "parsing_exception",
"reason": "[script] failed to parse field [source]",
"line": 9,
"col": 27,
"caused_by": {
"type": "unchecked_i_o_exception",
"reason": "com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@667ef57e; line: 9, column: 29]",
"caused_by": {
"type": "json_parse_exception",
"reason": "Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@667ef57e; line: 9, column: 29]"
}
}
},
"status": 400
}
To use ultiline strings in Kibanas Dev Tools you can use triple quotes (""") and also remove the escaped quotes in this case, since otherwise JSON doesn't support that syntax. Also make sure you are only querying the index pattern, that you have selected in Discover:
Your request was fine. Based on its response it turned out, that I had to set script.painless.regex.enabled to true. Now The field appears in elasticsearch, but it is empty.
The problem for you most likely will be ignore_above. If you haven't changed the default mapping, string fields will have a setting of 256 for ignore_above, which means all values above that length won't be indexed. You can still see them in the document _source (or in Kibana's Discover view), but they won't appear in the so called inverse index, that is used to build aggregations and search for data.
Also doc in painless scripts access only these doc values, and not the original source values, meaning that string is most likely cut off due to a wrong (the default) ignore_above setting in your index. If you need to be able to access that long values, please make sure your mapping doesn't have ignore_above in there.
Could you please paste the mapping for the metricbeat index, by running GET /metricbeat-*/_mapping in the dev tools?
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.