0

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 :)
  • Finally I figure out how to write it by using JD-JUI to check the "elasticsearch.jar" file.

EX:

Map<String, Object> params = new HashMap<>();
    params.put("num1", 10);
    params.put("num2", 4);

    String inlineScript = "doc['score'].value * num1 * num2";

    Script script = new Script(script, ScriptType.INLINE, "groovy", params);

    ScriptScoreFunctionBuilder scriptBuilder = ScoreFunctionBuilders.scriptFunction(ss);

that's all

1 Answer 1

1

Here is how you can implement scrip_ score function in java

Map<String, Object> params = new HashMap<>();                   
params.put("paramName1", "paramVal1")
params.put("paramName2", "paramVal2");

String script = "price  = doc['price'].value; margin = doc['margin'].value;
      if (price < threshold) { return price * margin / target };
      return price * (1 - discount) * margin / target;"
XContentBuilder builder = 
                    new ScriptScoreFunctionBuilder()
                    .script(script)
                    .lang("groovy")
                    .params(params)
                    .toXContent(XContentBuilder, params);
Sign up to request clarification or add additional context in comments.

6 Comments

Please add some details on why is this an answer.
Thank you so much for helping me, but the new ScriptScoreFunctionBuilder() does not work in my project. The API must be new ScriptScoreFunctionBuilder(Script) , it can not be write like your's ,only can write like new ScriptScoreFunctionBuilder(script).toXContent(builder, params) Am I use the wrong API? My english is poor ,sorry :(
I am not able to understand. Why doesn't new ScriptScoreFunctionBuilder() work ? There is no other constructor. Also, you can always do new ScriptScoreFunctionBuilder().scrip(SCRIPT).toXContent(builder, params)
Because the public method of new ScriptScoreFunctionBuilder() must have args "Script" like new ScriptScoreFunctionBuilder(script),now I am working on how to write new ScriptScoreFunctionBuilder().scrip(SCRIPT).toXContent(builder, params)
The class already has a public method which takes script as an argument. I have updated my answer to showcase.
|

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.