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

Add a score_mode to the rescore #3258

Closed
hc opened this Issue Jun 28, 2013 · 15 comments

Comments

Projects
None yet
3 participants
@hc
Copy link
Contributor

hc commented Jun 28, 2013

I started working on adding a score_mode parameter to the rescore query, something like :

{
    "rescore" : {
      "window_size" : 50,
      "query" : {
         "rescore_query" : {
            "match" : {
               "field1" : {
                  "query" : "the quick brown",
                  "type" : "phrase",
                  "slop" : 2
               }
            }
         },
         "query_weight" : 0.7,
         "rescore_query_weight" : 1.2,
         "score_mode" : "multiply"
      }
   }
}

Default is "total", possible values are : avg, max, min, total and multiply.

My use case is for the "multiply", because I want to do something like:

{
   "query" : {
      // lot of complex queries
      // default scoring
   },
   "rescore" : {
      "window_size" : 500,
      "query" : {
         "rescore_query" : {
            "custom_score" : {
              "query" : { "match_all" : { } },
              "script" : "complex_scoring",
              "lang" : "native"
            }
          },
         "query_weight" : 1.0,
         "rescore_query_weight" : 1.2
      }
   }
}

So rather than having to duplicate my complex query in the rescore query, I just multiply the first score with the second one.

What do you think? I will post my code.

@hc

This comment has been minimized.

Copy link
Contributor Author

hc commented Jun 28, 2013

Here my commit: hc@c37a2b7

@ghost ghost assigned s1monw Jul 15, 2013

@s1monw

This comment has been minimized.

Copy link
Contributor

s1monw commented Jul 15, 2013

This looks very useful. I will look into this asap... catching up right now after 3 weeks of absence :)

@s1monw

This comment has been minimized.

Copy link
Contributor

s1monw commented Jul 15, 2013

I added a comment, can you update the PR?

@s1monw

This comment has been minimized.

Copy link
Contributor

s1monw commented Jul 20, 2013

any news on the PR?

@hc

This comment has been minimized.

Copy link
Contributor Author

hc commented Jul 20, 2013

I was also in holidays the past weeks :) I will push it later today.

@s1monw

This comment has been minimized.

Copy link
Contributor

s1monw commented Jul 20, 2013

cool man I will look soon / tomorrow thanks :)

@s1monw

This comment has been minimized.

Copy link
Contributor

s1monw commented Jul 20, 2013

this looks really good. One minor thing, since those functions are idempotent can we only create one instance for them and keep it around and then do ScoreMode.Min.INSTANCE instead of new ScoreMode.Min()?

PS: I hope you had a good vacation :)

@hc

This comment has been minimized.

Copy link
Contributor Author

hc commented Jul 20, 2013

You are definitely right, I added a commit: #3360
I will merge in a single commit once everything is ok.

PS: thanks! You too if that's also the reason of your absence :)

@kimchy

This comment has been minimized.

Copy link
Member

kimchy commented Jul 21, 2013

how about making the score functions simply an enum?

@hc

This comment has been minimized.

Copy link
Contributor Author

hc commented Jul 21, 2013

Do you mean something like:

    private static enum ScoreMode {
        Avg("avg of:"),
        Max("max of:"),
        Min("min of:"),
        Multiply("product of:"),
        Total("sum of:");

        private final String description;

        ScoreMode(String description) {
            this.description = description;
        }
        public float combine(float primary, float secondary) {
            switch (this) {
                case Avg:
                    return (primary + secondary) / 2;
                case Max:
                    return Math.max(primary, secondary);
                case Min:
                    return Math.min(primary, secondary);
                case Multiply:
                    return primary * secondary;
                case Total:
                default:
                    return primary + secondary;
            }
        }
        public String description() {
            return description;
        }
    }
@kimchy

This comment has been minimized.

Copy link
Member

kimchy commented Jul 21, 2013

the combine method can be abstract on the enum, and each actual enum value can implement it, then you won't need the switch statement.

@hc

This comment has been minimized.

Copy link
Contributor Author

hc commented Jul 21, 2013

Oh thanks I didn't know about that. I pushed the modification.

@s1monw

This comment has been minimized.

Copy link
Contributor

s1monw commented Jul 23, 2013

@hc looks good can you squash the commits?

hc added a commit to hc/elasticsearch that referenced this issue Jul 24, 2013

Add a "score_mode" parameter to rescoring query.
Default value is "total", possible values are: "max", "min", "avg",
"multiply" and "total".

- "total": the final score of a document is the sum of the original
  query score with the rescore query score.
- "max": only the highest score count.
- "min": only the lowest score is kept (if the document doesn't match
  the rescore query, the original query score is used).
- "avg": average of both scores
- "multiply": product of both scores

Closes elastic#3258
@hc

This comment has been minimized.

Copy link
Contributor Author

hc commented Jul 24, 2013

I updated the PR

@s1monw s1monw closed this in 5022c46 Jul 26, 2013

s1monw added a commit that referenced this issue Jul 26, 2013

Add a "score_mode" parameter to rescoring query.
Default value is "total", possible values are: "max", "min", "avg",
"multiply" and "total".

- "total": the final score of a document is the sum of the original
  query score with the rescore query score.
- "max": only the highest score count.
- "min": only the lowest score is kept (if the document doesn't match
  the rescore query, the original query score is used).
- "avg": average of both scores
- "multiply": product of both scores

Closes #3258
@s1monw

This comment has been minimized.

Copy link
Contributor

s1monw commented Jul 26, 2013

@hc thanks man! would you mind opening an issue on the doc repo to update the features documentation?

mute pushed a commit to mute/elasticsearch that referenced this issue Jul 29, 2015

Add a "score_mode" parameter to rescoring query.
Default value is "total", possible values are: "max", "min", "avg",
"multiply" and "total".

- "total": the final score of a document is the sum of the original
  query score with the rescore query score.
- "max": only the highest score count.
- "min": only the lowest score is kept (if the document doesn't match
  the rescore query, the original query score is used).
- "avg": average of both scores
- "multiply": product of both scores

Closes elastic#3258
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.