Last active
April 17, 2020 16:26
-
-
Save djptek/a776bb26b18c1f3bf383e331305a8c2a to your computer and use it in GitHub Desktop.
recurse over properties map and remove trailing % symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a pipline using painless to remove trailing "%" symbols | |
PUT _ingest/pipeline/remove_percent_symbols | |
{ | |
"description": "recurse over properties map and remove trailing % symbols", | |
"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.substring(child.length()-1,child.length()).equals("%")) | |
o[childKey] = child.substring(0,child.length()-1); | |
} | |
} | |
} | |
} | |
traverse(ctx) | |
""" | |
} | |
} | |
] | |
} | |
# test the pipeline using the simulate API | |
POST _ingest/pipeline/remove_percent_symbols/_simulate | |
{ | |
"docs": [ | |
{ | |
"_index": "index", | |
"_source": { | |
"test1": "ABC", | |
"test2": "123%", | |
"more_tests": { | |
"my_string": "ABC", | |
"my_date": "2016-11-16T23:00:00.000Z", | |
"my_bool": true | |
}, | |
"yet_more_tests": { | |
"level2": { | |
"level3": { | |
"level4": { | |
"level5": { | |
"test3": "99%" | |
} | |
} | |
} | |
} | |
}, | |
"interview_likelihood": { | |
"gte": "50%", | |
"lte": "100%" | |
} | |
} | |
} | |
] | |
} | |
# create a new template using default pipline | |
PUT /_template/my_index | |
{ | |
"index_patterns": [ | |
"my_index" | |
], | |
"settings": { | |
"index.default_pipeline": "remove_percent_symbols" | |
}, | |
"order": 0, | |
"mappings": { | |
"properties": { | |
"interview_likelihood": { | |
"type": "integer_range" | |
} | |
} | |
} | |
} | |
# add a test document | |
PUT my_index/_doc/2 | |
{ | |
"interview_likelihood": { | |
"gte": "50%", | |
"lte": "100%" | |
} | |
} | |
# retrieve the document | |
GET my_index/_doc/2 | |
# expected result | |
{ | |
"_index" : "my_index", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_version" : 1, | |
"_seq_no" : 0, | |
"_primary_term" : 1, | |
"found" : true, | |
"_source" : { | |
"interview_likelihood" : { | |
"gte" : "50", | |
"lte" : "100" | |
} | |
} | |
} | |
# Note: don't worry about the quotes on gte/lte they are in the _source but the parent object | |
# in integer_range so they are coerced to a numeric | |
# and now run a query to test: | |
GET my_index/_search | |
{ | |
"query": { | |
"bool": { | |
"filter": { | |
"range": { | |
"interview_likelihood": { | |
"gt": 40, | |
"lt": 70, | |
"relation": "intersects" | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment