Space Vatican

Ramblings of a curious coder

Writing Your Elasticsearch Scripts in Ruby

There are already plugins out there to allow you to write your elasticsearch custom scripts in python, javascript and groovy (in addition to the default mvel), so why not ruby?

Clearly this calls for jruby. There’s quite a lot of boilerplate and xml files but the existing language plugins and this helpful article provide a good template for maven neophytes – there’s actually very little code to write at all.

A more salient question might be “why?” rather than “why not?”. So far with elasticsearch most of the scripts I’ve written have been so trivial (e.g. pulling out some function of a field in a custom_score) that the built-in in mvel language is just fine. Ruby may be more expressive but you’d never get to see that. In the rare cases where I’ve written something larger, performance has been key and so I’ve ended up writing the script in pure java.

Sometimes, “because I can” is a good enough answer though, and so I give you elasticsearch-jruby – an evening’s entertainment for me if nothing else.

One little niggle I have is that I ended up using instance variable to inject the parameters and context provided by elasticsearch. I thought initially that I could use local variables for everything but it would seem that when you use ScriptingContainer’s parse method that subsequent changes to local variables don’t affect the returned EmbedEvalUnit . This was my first time doing anything with jruby so I could easily be wrong!