ajyang818
ajyang818

Reputation: 505

Python custom scripting in ElasticSearch

The index has the capability of taking custom scripting in Python, but I can't find an example of custom scripting written in Python anywhere. Does anybody have an example of a working script? One with something as simple as an if-statement would be amazing.

Upvotes: 2

Views: 2934

Answers (2)

jifeng.yin
jifeng.yin

Reputation: 2151

Quoted from elasticsearch ML -

Luca pointed out that ES calls python with an 'eval'

PyObject ret = interp.eval((PyCode) compiledScript);

Just make sure your code pass through the eval.

Upvotes: 0

agross
agross

Reputation: 746

A simple custom scoring query using python (assuming you have the plugin installed).

{
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "script_score": {
        "lang": "python",
        "script": [
          "if _score:",
          "  _score"
        ]
      },
      "boost_mode": "replace"
    }
  },
  "track_scores": true
}

Upvotes: 2

Related Questions