1
votes

I have a requirement where there needs to be custom scoring on name. To keep it simple lets say, if I search for 'Smith' against names in the index, the logic should be:

if input = exact 'Smith' then score = 100%
else
 if input = phonetic match then
   score = <depending upon fuzziness match of input with name>% 
 end if
end if;

I'm able to search documents with a fuzziness of 1 but I don't know how to give it custom score depending upon how fuzzy it is. Thanks!

Update: I went through a post that had the same requirement as mine and it was mentioned that the person solved it by using native scripts. My question still remains, how to actually get the score based on the similarity distance such that it can be used in the native scripts:

The post for reference: https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116

The text to look for in the post: "For future readers I solved this issue by creating a custom score query and writing a (native) script to handle the scoring."

1

1 Answers

0
votes

You can implement this search logic using the rescore function query (docs here).

Here there is a possible example:

    {
    "query": {
        "function_score": {
          "query": { "match": {
            "input": "Smith"
          } },
          "boost": "5", 
          "functions": [
              {
                  "filter": { "match": { "input.keyword": "Smith" } },
                  "random_score": {}, 
                  "weight": 23
              }
          ]
        }
      }
   }

In this example we have a mapping with the input field indexed both as text and keyword (input.keyword is for exact match). We re-score the documents that match exactly the term "Smith" with an higher score respect to the all documents matched by the first query (in the example is a match, but in your case will be the query with fuzziness).

You can control the re-score effect tuning the weight parameter.