diff --git a/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java b/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java index 6a8d0c8b68a..a636946a921 100644 --- a/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java +++ b/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java @@ -72,13 +72,15 @@ public class FiltersFunctionScoreQuery extends Query { Query subQuery; final FilterFunction[] filterFunctions; final ScoreMode scoreMode; + final float maxBoost; DocSet[] docSets; - public FiltersFunctionScoreQuery(Query subQuery, ScoreMode scoreMode, FilterFunction[] filterFunctions) { + public FiltersFunctionScoreQuery(Query subQuery, ScoreMode scoreMode, FilterFunction[] filterFunctions, float maxBoost) { this.subQuery = subQuery; this.scoreMode = scoreMode; this.filterFunctions = filterFunctions; this.docSets = new DocSet[filterFunctions.length]; + this.maxBoost = maxBoost; } public Query getSubQuery() { @@ -149,7 +151,7 @@ public class FiltersFunctionScoreQuery extends Query { filterFunction.function.setNextReader(reader); docSets[i] = DocSets.convert(reader, filterFunction.filter.getDocIdSet(reader)); } - return new CustomBoostFactorScorer(getSimilarity(searcher), this, subQueryScorer, scoreMode, filterFunctions, docSets); + return new CustomBoostFactorScorer(getSimilarity(searcher), this, subQueryScorer, scoreMode, filterFunctions, maxBoost, docSets); } @Override @@ -218,6 +220,9 @@ public class FiltersFunctionScoreQuery extends Query { break; } + if (factor > maxBoost) { + factor = maxBoost; + } float sc = factor * subQueryExpl.getValue() * getValue(); Explanation res = new ComplexExplanation(true, sc, "custom score, score mode [" + scoreMode.toString().toLowerCase() + "]"); res.addDetail(subQueryExpl); @@ -242,15 +247,17 @@ public class FiltersFunctionScoreQuery extends Query { private final Scorer scorer; private final FilterFunction[] filterFunctions; private final ScoreMode scoreMode; + private final float maxBoost; private final DocSet[] docSets; private CustomBoostFactorScorer(Similarity similarity, CustomBoostFactorWeight w, Scorer scorer, - ScoreMode scoreMode, FilterFunction[] filterFunctions, DocSet[] docSets) throws IOException { + ScoreMode scoreMode, FilterFunction[] filterFunctions, float maxBoost, DocSet[] docSets) throws IOException { super(similarity); this.subQueryWeight = w.getValue(); this.scorer = scorer; this.scoreMode = scoreMode; this.filterFunctions = filterFunctions; + this.maxBoost = maxBoost; this.docSets = docSets; } @@ -322,6 +329,9 @@ public class FiltersFunctionScoreQuery extends Query { } } } + if (factor > maxBoost) { + factor = maxBoost; + } float score = scorer.score(); return subQueryWeight * score * factor; } diff --git a/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java b/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java index e0300d35fd2..e2f432ecc8f 100644 --- a/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java @@ -65,6 +65,7 @@ public class CustomFiltersScoreQueryParser implements QueryParser { boolean filtersFound = false; ArrayList scripts = new ArrayList(); TFloatArrayList boosts = new TFloatArrayList(); + float maxBoost = Float.MAX_VALUE; String currentFieldName = null; XContentParser.Token token; @@ -141,6 +142,8 @@ public class CustomFiltersScoreQueryParser implements QueryParser { } else { throw new QueryParsingException(parseContext.index(), "[custom_filters_score] illegal score_mode [" + sScoreMode + "]"); } + } else if ("max_boost".equals(currentFieldName) || "maxBoost".equals(currentFieldName)) { + maxBoost = parser.floatValue(); } else { throw new QueryParsingException(parseContext.index(), "[custom_filters_score] query does not support [" + currentFieldName + "]"); } @@ -172,7 +175,7 @@ public class CustomFiltersScoreQueryParser implements QueryParser { } filterFunctions[i] = new FiltersFunctionScoreQuery.FilterFunction(filters.get(i), scoreFunction); } - FiltersFunctionScoreQuery functionScoreQuery = new FiltersFunctionScoreQuery(query, scoreMode, filterFunctions); + FiltersFunctionScoreQuery functionScoreQuery = new FiltersFunctionScoreQuery(query, scoreMode, filterFunctions, maxBoost); functionScoreQuery.setBoost(boost); return functionScoreQuery; }