Elasticsearch fuzzy query too many hits bad score

Viewed 59

How can I add a script that calculates the percentage of match from fuzzy query? And showed the matched value

Example patient has 5 symptoms and fist the possible (A)diagnosis has 5 possible symptoms and the second diagnosis (B) has 6 symptoms and last(C) had 4 symptoms

Given some of the patience symptoms are similar, he actually matches 5/4 for C, 4/6 for B and 5/5 for A ...

The question is, how do you add such a script that calculates and sorts the matches, how do you put the value from match example for C?

1 Answers

You can use function_score for this requirement, like the one mentioned below.

Assuming symptoms as the field name where the symptom(s) are stored.

GET <index-of-symptoms>/_search?pretty
{
  "size": 20,
  "query": {
    "function_score": {
      "query": {
        "match_all": {}  #Write a master query to respond with all possible symptoms 
      },
      "functions": [
        {
          "filter": {
            "term": {
              "symptoms": "headache"
            }
          },
          "weight": 2    #Add weight to the score of the filter, this will be multiplied with the previous score each time
        },
        {
          "filter": {
            "term": {
              "tags.keyword": "nausea"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "term": {
              "tags.keyword": "fever"
            }
          },
          "weight": 2
        }
      ]
    }
  }
}

This will give you a score on the basis of how many matches you qualify from the functions section.

Related