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

View File

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