1

I have a very simple query :

match: {
  field => {
    boost: 4,
    query: term,
    fuzziness: 'AUTO',
  }
}

Composed with several (about 10) others queries most of them using constant_score. The problem is that on specific terms, my query has a too big score that cancel all others queries results.

Here is a part of the explain :

"details" => [
[0] {
      "value" => 63.656006,
"description" => "sum of:",
    "details" => [
    [0] {
              "value" => 63.656006,
        "description" => "weight(title.de:kandinsky in 1694239) [PerFieldSimilarity], result of:",
            "details" => [
            [0] {
                      "value" => 63.656006,
                "description" => "score(doc=1694239,freq=1.0 = termFreq=1.0\n), product of:",
                    "details" => [
                    [0] {
                              "value" => 4.0,
                        "description" => "boost",
                            "details" => []
                    },
                    [1] {
                              "value" => 11.3820715,
                        "description" => "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
[...]

Has you can see, I have a score of 11.38 due to the IDF. My others queries (with scores between 1 and 3) are totally useless.

My question is :

How can I set a max possible score for a query ?

Or, even better, can I set a range of score for my query ?

I would like to avoid a constant_score query for this field, I need a few TF/IDF and score notion for this field but not that strong.

I tried this :

function_score: {
  query: { match: {
    field => term,
  }},
  score_mode: :avg,
  script_score: {
    script: {
      inline: "4 * (1 + Math.log(2 + _score))",
    }
  },
}

It's better but it can still perform a very high score on certain cases.

Julien TASSIN
  • 5,004
  • 1
  • 25
  • 40
  • Hello, did you find an answer to this? I am trying to figure out what I have to do to solve this problem since I want my scores to be between 0 and 1. Thank you! – christouandr7 Apr 01 '19 at 10:17
  • @christouandr7, I used a function score with a script score using a `1 - (1/x)` `script_score`. I will post the full answer below – Julien TASSIN Apr 03 '19 at 18:46
  • Thank you for the update! I have another question. Since I get some scores to be less than 1, this creates a problem using this scoring function. With the function you 've just written, if the score is less than 1 then the final score will be negative! Do you have any idea how to handle this? – christouandr7 Apr 04 '19 at 08:07
  • @christouandr7 You can adapt the function this way : `1 - (1/( 1 +x))` – Julien TASSIN Apr 05 '19 at 17:39
  • I have adapted the function to 1 - (1 / (10x)). It is not very sensitive but my work is done! thank you for your answer too! – christouandr7 Apr 06 '19 at 07:10

2 Answers2

4

Finaly I used a function score with a script score using a 1 - (1/x) function in script_score

GET _search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "postgresql.log.message": "alter"
        }
      },
      "script_score" : {
                "script" : {
                    "params": {
                        "max_score": 5
                    },
                    "source": "params.max_score * (1 - 1 / _score)" 
                }
            }
    }
  }
}

This way, I will have a score between 0 and nearly 5 (max_score).

You can try it here with the word alter (score 3.9150627) or alter table pgbench_branches add primary key (bid) (score 4.8539715).

You can adapt the 1 - (1/x) function to approach the asymptote faster.

Julien TASSIN
  • 5,004
  • 1
  • 25
  • 40
  • Your max score will not always be near to 5 for smaller values of score. Like suppose actual max score for a specific query is 2 then you will get max score equal to 2.5 not 5. – Syed SaeedulHassan Ali Jan 22 '20 at 14:03
1

have u tried using Function score query ? Here is the link for the same https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

  • Yes, I already tried (I just updated the OP). It does not permit me to create a score range, just to limit a few the score value. – Julien TASSIN Jul 07 '17 at 15:10
  • Finaly , I used a function score with a script score using a 1 - (1/x) script_score. See the full answer below – Julien TASSIN Apr 03 '19 at 18:51