Query DSL: custom_filters_score allow score_mode for "min" and "multiply", closes #1560

This commit is contained in:
jayson.minard 2011-12-22 00:19:38 -08:00 committed by Shay Banon
parent fe4ba2ad55
commit 61b2562623
2 changed files with 32 additions and 2 deletions

View File

@ -69,7 +69,7 @@ public class FiltersFunctionScoreQuery extends Query {
}
}
public static enum ScoreMode {First, Avg, Max, Total}
public static enum ScoreMode {First, Avg, Max, Total, Min, Multiply}
Query subQuery;
final FilterFunction[] filterFunctions;
@ -178,7 +178,9 @@ public class FiltersFunctionScoreQuery extends Query {
} else {
int count = 0;
float total = 0;
float multiply = 1;
float max = Float.NEGATIVE_INFINITY;
float min = Float.POSITIVE_INFINITY;
ArrayList<Explanation> filtersExplanations = new ArrayList<Explanation>();
for (FilterFunction filterFunction : filterFunctions) {
DocSet docSet = DocSets.convert(reader, filterFunction.filter.getDocIdSet(reader));
@ -188,7 +190,9 @@ public class FiltersFunctionScoreQuery extends Query {
float sc = functionExplanation.getValue();
count++;
total += sc;
multiply *= sc;
max = Math.max(sc, max);
min = Math.min(sc, min);
Explanation res = new ComplexExplanation(true, sc, "custom score, product of:");
res.addDetail(new Explanation(1.0f, "match filter: " + filterFunction.filter.toString()));
res.addDetail(functionExplanation);
@ -205,9 +209,15 @@ public class FiltersFunctionScoreQuery extends Query {
case Max:
sc = max;
break;
case Min:
sc = min;
break;
case Total:
sc = total;
break;
case Multiply:
sc = multiply;
break;
}
sc *= getValue();
Explanation res = new ComplexExplanation(true, sc, "custom score, score mode [" + scoreMode.toString().toLowerCase() + "]");
@ -279,12 +289,25 @@ public class FiltersFunctionScoreQuery extends Query {
if (maxScore != Float.NEGATIVE_INFINITY) {
score = maxScore;
}
} else if (scoreMode == ScoreMode.Min) {
float minScore = Float.POSITIVE_INFINITY;
for (int i = 0; i < filterFunctions.length; i++) {
if (docSets[i].get(docId)) {
minScore = Math.min(filterFunctions[i].function.score(docId, score), minScore);
}
}
if (minScore != Float.POSITIVE_INFINITY) {
score = minScore;
}
} else { // Avg / Total
float totalScore = 0.0f;
float multiplicativeScore = 1.0f;
int count = 0;
for (int i = 0; i < filterFunctions.length; i++) {
if (docSets[i].get(docId)) {
totalScore += filterFunctions[i].function.score(docId, score);
float tempScore = filterFunctions[i].function.score(docId, score);
totalScore += tempScore;
multiplicativeScore *= tempScore;
count++;
}
}
@ -293,6 +316,9 @@ public class FiltersFunctionScoreQuery extends Query {
if (scoreMode == ScoreMode.Avg) {
score /= count;
}
else if (scoreMode == ScoreMode.Multiply) {
score = multiplicativeScore;
}
}
}
return subQueryWeight * score;

View File

@ -120,8 +120,12 @@ public class CustomFiltersScoreQueryParser implements QueryParser {
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Avg;
} else if ("max".equals(sScoreMode)) {
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Max;
} else if ("min".equals(sScoreMode)) {
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Min;
} else if ("total".equals(sScoreMode)) {
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Total;
} else if ("multiply".equals(sScoreMode)) {
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Multiply;
} else if ("first".equals(sScoreMode)) {
scoreMode = FiltersFunctionScoreQuery.ScoreMode.First;
} else {