-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
=================== The custom boost factor, custom script boost and the filters function query all do the same thing: They take a query and for each found document compute a new score based on the query score and some script, come custom boost factor or a combination of these two. However, the json format for these three functionalities is very different. This makes it hard to add new functions. This commit introduces one keyword <code>function_score</code> for all three functions. The new format can be used to either compute a new score with one function: "function_score": { "(query|filter)": {}, "boost": "boost for the whole query", "function": {} } or allow to combine the newly computed scores "function_score": { "(query|filter)": {}, "boost": "boost for the whole query", "functions": [ { "filter": {}, "function": {} }, { "function": {} } ], "score_mode": "(mult|max|...)" } <code>function</code> here can be either "script_score": { "lang": "lang", "params": { "param1": "value1", "param2": "value2" }, "script": "some script" } or "boost_factor" : number New custom functions can be added via the function score module. Changes --------- The custom boost factor query "custom_boost_factor" : { "query" : { .... }, "boost_factor" : 5.2 } becomes "function_score" : { "query" : { .... }, "boost_factor" : 5.2 } The custom script score "custom_score" : { "query" : { .... }, "params" : { "param1" : 2, "param2" : 3.1 }, "script" : "_score * doc['my_numeric_field'].value / pow(param1, param2)" } becomes "custom_score" : { "query" : { .... }, "script_score" : { "params" : { "param1" : 2, "param2" : 3.1 }, "script" : "_score * doc['my_numeric_field'].value / pow(param1, param2)" } } and the custom filters score query "custom_filters_score" : { "query" : { "match_all" : {} }, "filters" : [ { "filter" : { "range" : { "age" : {"from" : 0, "to" : 10} } }, "boost" : "3" }, { "filter" : { "range" : { "age" : {"from" : 10, "to" : 20} } }, "script" : "_score * doc['my_numeric_field'].value / pow(param1, param2)" } ], "score_mode" : "first", "params" : { "param1" : 2, "param2" : 3.1 } "score_mode" : "first" } becomes: "function_score" : { "query" : { "match_all" : {} }, "functions" : [ { "filter" : { "range" : { "age" : {"from" : 0, "to" : 10} } }, "boost" : "3" }, { "filter" : { "range" : { "age" : {"from" : 10, "to" : 20} } }, "script_score" : { "script" : "_score * doc['my_numeric_field'].value / pow(param1, param2)", "params" : { "param1" : 2, "param2" : 3.1 } } } ], "score_mode" : "first", } Partially closes issue #3423
- Loading branch information
Showing
36 changed files
with
1,882 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/org/elasticsearch/common/lucene/search/function/ScriptScoreFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to ElasticSearch and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. ElasticSearch licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common.lucene.search.function; | ||
|
||
import org.apache.lucene.index.AtomicReaderContext; | ||
import org.apache.lucene.search.Explanation; | ||
import org.elasticsearch.script.ExplainableSearchScript; | ||
import org.elasticsearch.script.SearchScript; | ||
|
||
import java.util.Map; | ||
|
||
public class ScriptScoreFunction implements ScoreFunction { | ||
|
||
private final String sScript; | ||
|
||
private final Map<String, Object> params; | ||
|
||
private final SearchScript script; | ||
|
||
public ScriptScoreFunction(String sScript, Map<String, Object> params, SearchScript script) { | ||
this.sScript = sScript; | ||
this.params = params; | ||
this.script = script; | ||
} | ||
|
||
@Override | ||
public void setNextReader(AtomicReaderContext ctx) { | ||
script.setNextReader(ctx); | ||
} | ||
|
||
@Override | ||
public float score(int docId, float subQueryScore) { | ||
script.setNextDocId(docId); | ||
script.setNextScore(subQueryScore); | ||
return script.runAsFloat(); | ||
} | ||
|
||
@Override | ||
public double factor(int docId) { | ||
// just the factor, so don't provide _score | ||
script.setNextDocId(docId); | ||
return script.runAsFloat(); | ||
} | ||
|
||
@Override | ||
public Explanation explainScore(int docId, Explanation subQueryExpl) { | ||
Explanation exp; | ||
if (script instanceof ExplainableSearchScript) { | ||
script.setNextDocId(docId); | ||
script.setNextScore(subQueryExpl.getValue()); | ||
exp = ((ExplainableSearchScript) script).explain(subQueryExpl); | ||
} else { | ||
double score = score(docId, subQueryExpl.getValue()); | ||
exp = new Explanation((float)score, "script score function: composed of:"); | ||
exp.addDetail(subQueryExpl); | ||
} | ||
return exp; | ||
} | ||
|
||
@Override | ||
public Explanation explainFactor(int docId) { | ||
return new Explanation((float)factor(docId), "script_factor"); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "script[" + sScript + "], params [" + params + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.