yuji kumada
yuji kumada

Reputation: 53

Elasticsearch Java API Function Score Query with script function

I am currently trying to build this function score query with the Java API of elasticsearch:

"query": {
"function_score": {
  "query": {
    "query_string": {
      "query": " ( ((Feild1:\"KeyWord\"^100)  OR (Feild2:\"KeyWord\"^50) ))",
      "default_operator": "and"
    }
  },
  "boost_mode": "replace",
  "script_score": {
    "script": "_score * doc [ 'calc_feild'].value"
  }
}

But I can't find any documentation regarding the java api and the function score queries. This is what I have so far:

So far I realized it

searchParam = ((Feild1:\"KeyWord\"^100)  OR (Feild2:\"KeyWord\"^50))
searchBulider.setQuery(new FunctionScoreQueryBuilder(QueryBuilders.queryString(searchParam).defaultOperator(Operator.AND)).boostMode("replace"));

query

"query" : {
"function_score" : {
  "query" : {
    "query_string" : {
      "query" : " (  ((Feild1:\"KeyWord\"^100)  OR (Feild2:\"KeyWord\"^50) ) )",
      "default_operator" : "and"
    }
  },
  "functions" : [ ],
  "boost_mode" : "replace"
}

I do not know the method in the future And the next question is how I can provide to functions in the FunctionScore Builder

Function empty?

thank you

Upvotes: 5

Views: 5448

Answers (1)

Val
Val

Reputation: 217304

You're on the right way, here is the whole code to build the desired query:

searchParam = "((Feild1:\"KeyWord\"^100)  OR (Feild2:\"KeyWord\"^50))";
QueryStringQueryBuilder query = QueryBuilders.queryString(searchParam)
    .defaultOperator(Operator.AND);
ScriptScoreFunctionBuilder scoreFunction = ScoreFunctionBuilders
    .scriptFunction("_score * doc['calc_feild'].value");
searchBulider.setQuery(new FunctionScoreQueryBuilder(query, scoreFunction)
    .boostMode("replace"));

Upvotes: 7

Related Questions