Skip to content

Instantly share code, notes, and snippets.

@djptek
Created July 12, 2022 10:39
Show Gist options
  • Save djptek/510b7889163f9acff91b809f735dc277 to your computer and use it in GitHub Desktop.
Save djptek/510b7889163f9acff91b809f735dc277 to your computer and use it in GitHub Desktop.
Painless Convert "-" to ""
# test the pipeline using the simulate API
POST _ingest/pipeline/_simulate
{
"docs": [
{
"_index": "index",
"_source": {
"leave_as_abc": "abc",
"change_to_empty1": "-",
"more_tests": {
"leave_as_def": "def",
"leave_as_date": "2016-11-16T23:00:00.000Z",
"leave_as_true": true
},
"nested_tests": {
"level2": {
"level3": {
"level4": {
"level5": {
"change_to_empty2": "-"
}
}
}
}
}
}
}
],
"pipeline": {
"description": "recurse over properties map and convert - to empty string",
"processors": [
{
"script": {
"source": """
void traverse(Map o) {
for (String childKey : o.keySet()) {
if (!childKey.substring(0,1).equals("_")) {
def child = o.get(childKey);
if (child instanceof Map) {
traverse(child);
} else if (child instanceof String) {
if (child.equals("-"))
o[childKey] = "";
}
}
}
}
traverse(ctx)
"""
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment