How to write elasticsearch script_score in java api


(Kevin Yuan) #1

I find a function in ElasticSearch like

GET /_search
{
  "function_score": {
    "functions": [
      { ...location clause... }, 
      { ...price clause... }, 
      {
        "script_score": {
          "params": { 
            "threshold": 80,
            "discount": 0.1,
            "target": 10
          },
          "script": "price  = doc['price'].value; margin = doc['margin'].value;
          if (price < threshold) { return price * margin / target };
          return price * (1 - discount) * margin / target;" 
        }
      }
    ]
  }
}
  • I use ScoreFunctionBuilder to achive the "location caluse" and "price caluse" ,but I do not know how to write "script_score" and "script" by java api
  • The ES version in my project is 2.2.0 and I use the java api to achieve this function
  • But I can not find the API like ScriptScoreFunctionBuilder. scriptFunction(String script,
    Map<String, Object> params) how can I do? Thx :slight_smile:

(system) #2