1

I know that solr supports if conditions in the query (e.g., fl=if(min(2,myField),2,myField)). I am not able to find similar functionality in elasticsearch. The closest I can find is script fields.

  1. Does elasticsearch support nested function queries like min(max(myField,0),1)?
  2. If so, does it support such queries for sorting as well, like sort=min(max(myField,0),1)?

1 Answer 1

5

Scripting is what you're looking for. The default scripting language is mvel, but you can easily install plugins that support other languages, like javascript, groovy, python. You can also write your own native script in Java.

You can use scripts in different apis, here are some examples:

It's possible to preload scripts placing them on file system, from the documentation page:

Scripts can always be provided as part of the relevant API, but they can also be preloaded by placing them under config/scripts and then referencing them by the script name (instead of providing the full script). This helps reduce the amount of data passed between the client and the nodes.

The name of the script is derived from the hierarchy of directories it exists under, and the file name without the lang extension. For example, a script placed under config/scripts/group1/group2/test.py will be named group1_group2_test.

Only native scripts written in java have to implement an interface. In case of either mvel, javascript python or groovy, you can just write the script that you need on file instead of passing it together with the relevant api and refer to it using its name.

Sign up to request clarification or add additional context in comments.

2 Comments

@javana Great. In most of the examples, I see simple scripts like script" : "doc['field_name'].value * factor" . Have you come across something that talks about how to plugin an myPython.py file and goes into details like where the .py/.js file should be put, what methods & signatures it needs to implement etc.?
It's hard for me to follow here but I saw you posted on the elasticsearch google group a specific question with more details. I would consider this question answered given its title and content, but you are free to create a new one with a detailed description whenever you feel like you need more help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.