From 60215155679db40203a7cea8c5893e6379b13121 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Sun, 25 Nov 2012 09:06:30 -0500 Subject: [PATCH] The relevancy score in explanation should match the actual score in custom_filters_query Fixes #2441 --- .../function/FiltersFunctionScoreQuery.java | 6 ++- .../customscore/CustomScoreSearchTests.java | 47 ++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) 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 b312ab2518f..2d3a62a75e6 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 @@ -163,7 +163,11 @@ public class FiltersFunctionScoreQuery extends Query { if (docSet.get(doc)) { filterFunction.function.setNextReader(context); Explanation functionExplanation = filterFunction.function.explainFactor(doc); - float sc = getBoost() * subQueryExpl.getValue() * functionExplanation.getValue(); + float factor = functionExplanation.getValue(); + if (factor > maxBoost) { + factor = maxBoost; + } + float sc = getBoost() * factor; Explanation filterExplanation = new ComplexExplanation(true, sc, "custom score, product of:"); filterExplanation.addDetail(new Explanation(1.0f, "match filter: " + filterFunction.filter.toString())); filterExplanation.addDetail(functionExplanation); diff --git a/src/test/java/org/elasticsearch/test/integration/search/customscore/CustomScoreSearchTests.java b/src/test/java/org/elasticsearch/test/integration/search/customscore/CustomScoreSearchTests.java index 5d78fe9680f..e32f93d0295 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/customscore/CustomScoreSearchTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/customscore/CustomScoreSearchTests.java @@ -84,7 +84,7 @@ public class CustomScoreSearchTests extends AbstractNodesTests { .setQuery(customFiltersScoreQuery(matchAllQuery()) .add(termFilter("field", "value4"), "2") .add(termFilter("field", "value2"), "3") - .scoreMode("first")) + .scoreMode("first")) .setExplain(true) .execute().actionGet(); @@ -114,7 +114,7 @@ public class CustomScoreSearchTests extends AbstractNodesTests { .add(termFilter("field", "value4"), "2") .add(termFilter("field", "value2"), "3") .boost(2) - .scoreMode("first")) + .scoreMode("first")) .setExplain(true) .execute().actionGet(); @@ -364,5 +364,48 @@ public class CustomScoreSearchTests extends AbstractNodesTests { assertThat(searchResponse.hits().getAt(2).score(), equalTo(2.0f)); assertThat(searchResponse.hits().getAt(3).id(), equalTo("2")); assertThat(searchResponse.hits().getAt(3).score(), equalTo(1.0f)); + + searchResponse = client.prepareSearch("test") + .setQuery(customFiltersScoreQuery(termsQuery("field", "value1", "value2", "value3", "value4")).scoreMode("first") + .add(termFilter("field", "value4"), 2) + .add(termFilter("field", "value3"), 3) + .add(termFilter("field", "value2"), 4)) + .setExplain(true) + .execute().actionGet(); + + assertThat(Arrays.toString(searchResponse.shardFailures()), searchResponse.failedShards(), equalTo(0)); + assertThat(searchResponse.hits().totalHits(), equalTo(4l)); + assertThat(searchResponse.hits().getAt(0).id(), equalTo("2")); + assertThat(searchResponse.hits().getAt(0).score(), equalTo(searchResponse.hits().getAt(0).explanation().getValue())); + logger.info("--> Hit[0] {} Explanation {}", searchResponse.hits().getAt(0).id(), searchResponse.hits().getAt(0).explanation()); + assertThat(searchResponse.hits().getAt(1).id(), equalTo("3")); + assertThat(searchResponse.hits().getAt(1).score(), equalTo(searchResponse.hits().getAt(1).explanation().getValue())); + assertThat(searchResponse.hits().getAt(2).id(), equalTo("4")); + assertThat(searchResponse.hits().getAt(2).score(), equalTo(searchResponse.hits().getAt(2).explanation().getValue())); + assertThat(searchResponse.hits().getAt(3).id(), equalTo("1")); + assertThat(searchResponse.hits().getAt(3).score(), equalTo(searchResponse.hits().getAt(3).explanation().getValue())); + + + searchResponse = client.prepareSearch("test") + .setQuery(customFiltersScoreQuery(termsQuery("field", "value1", "value2", "value3", "value4")).scoreMode("multiply") + .add(termFilter("field", "value4"), 2) + .add(termFilter("field", "value1"), 3) + .add(termFilter("color", "red"), 5)) + .setExplain(true) + .execute().actionGet(); + + assertThat(Arrays.toString(searchResponse.shardFailures()), searchResponse.failedShards(), equalTo(0)); + assertThat(searchResponse.hits().totalHits(), equalTo(4l)); + assertThat(searchResponse.hits().getAt(0).id(), equalTo("1")); + assertThat(searchResponse.hits().getAt(0).score(), equalTo(searchResponse.hits().getAt(0).explanation().getValue())); + logger.info("--> Hit[0] {} Explanation {}", searchResponse.hits().getAt(0).id(), searchResponse.hits().getAt(0).explanation()); + assertThat(searchResponse.hits().getAt(1).id(), equalTo("3")); + assertThat(searchResponse.hits().getAt(1).score(), equalTo(searchResponse.hits().getAt(1).explanation().getValue())); + assertThat(searchResponse.hits().getAt(2).id(), equalTo("4")); + assertThat(searchResponse.hits().getAt(2).score(), equalTo(searchResponse.hits().getAt(2).explanation().getValue())); + assertThat(searchResponse.hits().getAt(3).id(), equalTo("2")); + assertThat(searchResponse.hits().getAt(3).score(), equalTo(searchResponse.hits().getAt(3).explanation().getValue())); + + } } \ No newline at end of file