Search code examples
elasticsearchelasticsearch-painless

Elasticsearch scripted query painless exponential function


With Elasticsearch and painless is there a way to implement an exponential function? I can't seem to find anything. I have something like this.

bdy = {
    "from" : 0,
    "size" : 10,
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": must_terms
                }
            },
            "script_score": {
                "script": {
                "lang": "expression",
                "source": "doc['release_year'].value"
              }
            }
        }
    }
}

I want to add some more complex math in the source field, like this.

"source": "Math.exponential(1/doc['release_year'].value)"

Is that possible? Or is there another scripting language that you can do that in within elasticsearch?

UPDATE

Actually looks like I can use.

"lang": "expression"
"source": "_score/10 + 1/(1+ exp(-(doc['release_year'].value*a)))"

http://lucene.apache.org/core/6_0_0/expressions/index.html?org/apache/lucene/expressions/js/package-summary.html

If anyone has other options that would be cool.


Solution

  • You can do it in Painless the same way with Math.exp()

    "source": "_score/10 + 1/(1+ Math.exp(-(doc['release_year'].value*a)))"
    

    See the full Painless API here: https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference.html