Query DSL: Add max_boost to custom_filters_score to cap boosting, closes #1973.

This commit is contained in:
Shay Banon 2012-05-23 23:31:42 +02:00
parent 9f706c6f6e
commit a64fcf77ee
2 changed files with 17 additions and 4 deletions

View File

@ -72,13 +72,15 @@ public class FiltersFunctionScoreQuery extends Query {
Query subQuery; Query subQuery;
final FilterFunction[] filterFunctions; final FilterFunction[] filterFunctions;
final ScoreMode scoreMode; final ScoreMode scoreMode;
final float maxBoost;
DocSet[] docSets; DocSet[] docSets;
public FiltersFunctionScoreQuery(Query subQuery, ScoreMode scoreMode, FilterFunction[] filterFunctions) { public FiltersFunctionScoreQuery(Query subQuery, ScoreMode scoreMode, FilterFunction[] filterFunctions, float maxBoost) {
this.subQuery = subQuery; this.subQuery = subQuery;
this.scoreMode = scoreMode; this.scoreMode = scoreMode;
this.filterFunctions = filterFunctions; this.filterFunctions = filterFunctions;
this.docSets = new DocSet[filterFunctions.length]; this.docSets = new DocSet[filterFunctions.length];
this.maxBoost = maxBoost;
} }
public Query getSubQuery() { public Query getSubQuery() {
@ -149,7 +151,7 @@ public class FiltersFunctionScoreQuery extends Query {
filterFunction.function.setNextReader(reader); filterFunction.function.setNextReader(reader);
docSets[i] = DocSets.convert(reader, filterFunction.filter.getDocIdSet(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 @Override
@ -218,6 +220,9 @@ public class FiltersFunctionScoreQuery extends Query {
break; break;
} }
if (factor > maxBoost) {
factor = maxBoost;
}
float sc = factor * subQueryExpl.getValue() * getValue(); float sc = factor * subQueryExpl.getValue() * getValue();
Explanation res = new ComplexExplanation(true, sc, "custom score, score mode [" + scoreMode.toString().toLowerCase() + "]"); Explanation res = new ComplexExplanation(true, sc, "custom score, score mode [" + scoreMode.toString().toLowerCase() + "]");
res.addDetail(subQueryExpl); res.addDetail(subQueryExpl);
@ -242,15 +247,17 @@ public class FiltersFunctionScoreQuery extends Query {
private final Scorer scorer; private final Scorer scorer;
private final FilterFunction[] filterFunctions; private final FilterFunction[] filterFunctions;
private final ScoreMode scoreMode; private final ScoreMode scoreMode;
private final float maxBoost;
private final DocSet[] docSets; private final DocSet[] docSets;
private CustomBoostFactorScorer(Similarity similarity, CustomBoostFactorWeight w, Scorer scorer, 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); super(similarity);
this.subQueryWeight = w.getValue(); this.subQueryWeight = w.getValue();
this.scorer = scorer; this.scorer = scorer;
this.scoreMode = scoreMode; this.scoreMode = scoreMode;
this.filterFunctions = filterFunctions; this.filterFunctions = filterFunctions;
this.maxBoost = maxBoost;
this.docSets = docSets; this.docSets = docSets;
} }
@ -322,6 +329,9 @@ public class FiltersFunctionScoreQuery extends Query {
} }
} }
} }
if (factor > maxBoost) {
factor = maxBoost;
}
float score = scorer.score(); float score = scorer.score();
return subQueryWeight * score * factor; return subQueryWeight * score * factor;
} }

View File

@ -65,6 +65,7 @@ public class CustomFiltersScoreQueryParser implements QueryParser {
boolean filtersFound = false; boolean filtersFound = false;
ArrayList<String> scripts = new ArrayList<String>(); ArrayList<String> scripts = new ArrayList<String>();
TFloatArrayList boosts = new TFloatArrayList(); TFloatArrayList boosts = new TFloatArrayList();
float maxBoost = Float.MAX_VALUE;
String currentFieldName = null; String currentFieldName = null;
XContentParser.Token token; XContentParser.Token token;
@ -141,6 +142,8 @@ public class CustomFiltersScoreQueryParser implements QueryParser {
} else { } else {
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] illegal score_mode [" + sScoreMode + "]"); 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 { } else {
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] query does not support [" + currentFieldName + "]"); 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); 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); functionScoreQuery.setBoost(boost);
return functionScoreQuery; return functionScoreQuery;
} }