Home > Blockchain >  Elasticsearch - Partial Search with Fuzziness on multiple fields?
Elasticsearch - Partial Search with Fuzziness on multiple fields?

Time:12-22

I have an index which contains users with real names along with facilities with names that have numbers. Sample data show below.

dim_employee = [
{"full_name": "Jon Doe", "email_address": "[email protected]"}, 
{"full_name": "RIG 876 B&X", "email_address": "[email protected]"}]

I wanted to be able to support fuzziness search for typos as well as partial word search.

Initially I went with the below fuzzy query:

GET dim_employee/_search
{
  "query": {
    "multi_match": {
      "fields":  [ "full_name", "email_address" ],
      "query":     "BX876",
      "fuzziness": "AUTO",
       "type": "best_fields"
    }
  }
}

This query does not return the record with the BX867 document. However if I just query for 876 I get the intended result. Searching for user Jon Doe works perfect here. I can mistype it and works fine. Partial search does not return the result as expected here.

I then tried to do a phrase_prefix query as below:

GET dim_employee/_search
{
  "query": {
    "multi_match": {
      "fields":  [ "full_name", "email_address" ],
      "query":     "BX867 ",
       "type": "phrase_prefix"
    }
  }
}

This does get the BX867 document as intended but no longer supports fuzzy matching. If I mistype user Jon Doe I would not get any results back. Is there a way to have fuzzy matching and partial phrase matching while searching multiple fields?

CodePudding user response:

Why not you combine two clausules?

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "fields": [
              "full_name",
              "email_address"
            ],
            "query": "BX8676",
            "type": "phrase_prefix"
          }
        },
        {
          "multi_match": {
            "fields": [
              "full_name",
              "email_address"
            ],
            "query": "BX876",
            "fuzziness": "AUTO",
            "type": "best_fields"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}
  • Related