Mayak
Mayak

Reputation: 553

Finding matches for two connected fields with fuzziness

I'm trying to search a specific person by his given name and surname. I think the best option to search within two fields simultaneously is a bool query:

{
    "query":{
        "bool":{
            "must":[
                {"match": {"name":"Martin"}},
                {"match": {"surname":"Mcfly"}}
            ]
        }
}
}

But bool queries don't seem to support fuzziness. So what could I do to find the person "Marty Mcfly" since this match isn't found by the above query. I also would like to be ably to find someone like "Marty J. Mcfly" if it's possible.

Upvotes: 0

Views: 38

Answers (1)

Evaldas Buinauskas
Evaldas Buinauskas

Reputation: 14097

bool is just a wrapper to join AND/OR/NOT/FILTER operations.

In your case it would make sense to use multi_match query:

{
  "query":{
    "bool":{
      "must":[
        {
          "multi_match":{
            "query":"Marty J. Mcfly",
            "operator": "and",
            "fields":[
              "name",
              "surname"
            ]
          }
        }
      ]
    }
  }
}

This will search data in both name and surname fields and ensure that all terms must match in both of your fields.

Updated

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": {
              "query": "Martin",
              "operator": "and",
              "fuzziness": 1
            }
          }
        },
        {
          "match": {
            "surname": {
              "query": "Mcfly",
              "operator": "and",
              "fuzziness": 1
            }
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions