简体   繁体   中英

ElasticSearch Groovy script called from Java, how to iterate on all parameters?

In my java code I call a groovy script that update an index elasticsearch. I pass a Map in parameter of this script. The call in the java code is done like this :

// params is a Map<String, String> fullfill
params.put("date","2017-0-23");
params.put("sun","good");
request.script(new Script("myscript", ScriptType.FILE, "groovy", params));

request.upsert("{}");
request.scriptedUpsert(true);
try {
    client.update(request).get();
}

Then in the groovy script i do something like this :

newmap = ['date': date,
        'sun': sun,
         ]

So i create a new map and i fill it with some values of the map 'params' from java. If i create a new map it is because i want to do a little traitment on the map i passed in parameter.

But my problem is that if my java map contains a key "date" with a value "2017-08-23" i only know how to access to the value "2017-08-23" and i do this just by writting date in the script (so the key of the java map).

Is there a way to access to some element of the map that i don't know the name ? In fact I want to iterate on all the content of my java map and get the key and the value.

I read all of that page but i don't find any solution https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html

I have never use Groovy, excepted in that case, but it's seems that the content of my java map is transformed in something called "named parameter" in groovy.

Thank you by advance.

In groovy, you can iterate over a map (or a lot of things, really) using .each. For example:

def test = [one: 1, two: 2, three: 3]
test.each { k, v -> println k }

Prints out

one
two
three

The values are accessible in the closure (.each) as "v". You can call your keys and values what you want in the closure, but convention says k and v are convenient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM