I thought it was a simple task, but I'm having a problem trying to trim a field.
I have the City field in my index, it contains "MyCity", "blank_spaceMyCity", "Mycityblank_space" so I tried to apply the trim funcion in a painless scripted field, like
doc['City.keyword'].value.trim()
This returns just nothing in the field test and throws errors in Discover. Removing the trim() part I get the city names as expected, but with surrounding spaces of course.
I found no useful doc around or examples.
How do I use the trim function?
Using 6.5.
Thanks
Are you getting any errors in your ES logs? Also, can you check that the field types are set OK? as trim will only work on strings.
That is the correct syntax for the trim function and I just tried it on 6.6.0 and it was fine.
[2019-01-07T12:03:32,957][DEBUG][o.e.a.s.TransportSearchAction] [oiZDz7D] [609645] Failed to execute fetch phase
org.elasticsearch.transport.RemoteTransportException: [oiZDz7D][127.0.0.1:9300][indices:data/read/search[phase/fetch/id]]
Caused by: org.elasticsearch.script.ScriptException: runtime error
at org.elasticsearch.painless.PainlessScript.convertToScriptException(PainlessScript.java:94) ~[?:?]
at org.elasticsearch.painless.PainlessScript$Script.execute(doc['Country.keyword'].value.trim() ...:29) ~[?:?]
at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:104) ~[?:?]
at org.elasticsearch.search.fetch.subphase.ScriptFieldsFetchSubPhase.hitsExecute(ScriptFieldsFetchSubPhase.java:67) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:167) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.search.SearchService.lambda$executeFetchPhase$3(SearchService.java:556) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.search.SearchService$3.doRun(SearchService.java:361) [elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:723) [elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:41) [elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.5.4.jar:6.5.4]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_191]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]
Caused by: java.lang.IllegalArgumentException: dynamic getter [java.lang.String, trim] not found
at org.elasticsearch.painless.Def.lookupGetter(Def.java:395) ~[?:?]
at org.elasticsearch.painless.DefBootstrap$PIC.lookup(DefBootstrap.java:154) ~[?:?]
at org.elasticsearch.painless.DefBootstrap$PIC.fallback(DefBootstrap.java:208) ~[?:?]
at org.elasticsearch.painless.PainlessScript$Script.execute(doc['Country.keyword'].value.trim ...:29) ~[?:?]
at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:104) ~[?:?]
at org.elasticsearch.search.fetch.subphase.ScriptFieldsFetchSubPhase.hitsExecute(ScriptFieldsFetchSubPhase.java:67) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:167) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.search.SearchService.lambda$executeFetchPhase$3(SearchService.java:556) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.search.SearchService$3.doRun(SearchService.java:361) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:723) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:41) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-6.5.4.jar:6.5.4]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[?:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[?:1.8.0_191]
at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_191]
Running
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
on Ubuntu 16
This looks weird. I presume you are setting the script type as painless, as default. That should be OK.
I'll have to refer somebody from ES team to this issue.
Yes, painless scripting and string as in your pic above. I just added "Title case" transform at the end, since I have mixed "City", "city", "CITY" and so on. (Same for country and other details... bad data collection)
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.