Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function score and optional weight : avg score is wrong #8992

Closed
ghiron opened this issue Dec 17, 2014 · 5 comments
Closed

Function score and optional weight : avg score is wrong #8992

ghiron opened this issue Dec 17, 2014 · 5 comments

Comments

@ghiron
Copy link
Contributor

ghiron commented Dec 17, 2014

related to #7137 #6955

Score calculation is wrong when score mode is set to average :
Let's say we have :

  • function A returns 25 and weight of 10
  • function B returns 10 and weight of 5

returned value is (25 *10 +10 * 5 )/ 2 = 150
returned value should be (25 *10 +10 * 5 )/ (10 +5) = 20

I think problem is located in org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery:302

double totalFactor = 0.0f;
int count = 0;
for (int i = 0; i < filterFunctions.length; i++) {
   if (docSets[i].get(docId)) {
      totalFactor += filterFunctions[i].function.score(docId, subQueryScore);
      count++;
   }
}
if (count != 0) {
   factor = totalFactor;
   if (scoreMode == ScoreMode.Avg) {
      factor /= count;
   }
}

should be something like

double totalFactor = 0.0f;
float count = 0;
for (int i = 0; i < filterFunctions.length; i++) {
   if (docSets[i].get(docId)) {
      totalFactor += filterFunctions[i].function.score(docId, subQueryScore);
      if (filterFunctions[i].function instanceof WeightFactorFunction){
         count+= ((WeightFactorFunction)filterFunctions[i].function).getWeight();
      }else{
         count++;
      }
   }
}
if (count != 0) {
   factor = totalFactor;
   if (scoreMode == ScoreMode.Avg) {
      factor /= count;
   }
}
@rjernst
Copy link
Member

rjernst commented Dec 17, 2014

Thanks @ghiron! Do you want to create a pull request with your change?

@ghiron
Copy link
Contributor Author

ghiron commented Dec 18, 2014

I'm on it

@ghiron
Copy link
Contributor Author

ghiron commented Dec 18, 2014

@rjernst : I'm updating tests and have negative weight in some tests. Does it really make sense to have negative weight ?

@rjernst
Copy link
Member

rjernst commented Dec 18, 2014

@ghiron Which tests use negative weights?

@ghiron
Copy link
Contributor Author

ghiron commented Dec 18, 2014

@rjernst in FunctionScoreTests createRandomWeights method returns negative value half of the time

An other comment about this test class : getRandomScoreMode method returns random score mode value but does not guarantee all values will be tested.
simpleWeightedFunctionsTestWithRandomWeightsAndRandomCombineMode test is the only place I found avg score mode test so basically avg computation is tested once over 5 execution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants