Raghu Chahar
Raghu Chahar

Reputation: 1773

How to implement fuzzy search in multi fields

I was implementing fuzzy search in my existing elasticsearch where I can't change mappings, I was hoping if there is any way I can convert the following query in fuzzy one i.e add fuzzy search on fields lower_name and album

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user": "userId"
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "lower_name": ["search", "Text"]
                }
              },
              {
                "terms": {
                  "album": ["search","Text"]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

I tried this :

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user": "userId"
          }
        },
        {
          "bool": {
            "should": [
              {
                "fuzzy": {
                  "lower_name": ["search","Text"]
                }
              },
              {
                "fuzzy": {
                  "album": ["search","Text"]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

But this is giving error: [fuzzy] query doesn't support multiple fields Please help! Using Elasticsearch 6.3

Upvotes: 0

Views: 557

Answers (2)

Bhavya
Bhavya

Reputation: 16192

You can use a multi_match query with fuzziness. Try out the below query

Index Data:

{
  "user": "ben",
  "lower_name": "def",
  "album": "Brenda"
}
{
  "user": "ben",
  "lower_name": "abc",
  "album": "Brenda"
},
{
  "user": "ben",
  "lower_name": "fgh",
  "album": "honda"
}

Search Query:

     {
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user": "ben"
          }
        },
        {
          "bool": {
            "should": [
              {
                "multi_match": {
                  "query": "abc dey",
                  "fields": [
                    "lower_name"
                  ],
                  "fuzziness": "auto"
                }
              },
              {
                "multi_match": {
                  "query": "brenda",
                  "fields": [
                    "album"
                  ],
                  "fuzziness": "auto"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "66311552",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.7497801,
        "_source": {
          "user": "ben",
          "lower_name": "def",
          "album": "Brenda"
        }
      },
      {
        "_index": "66311552",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.7497801,
        "_source": {
          "user": "ben",
          "lower_name": "abc",
          "album": "Brenda"
        }
      }
    ]

Upvotes: 1

Amit
Amit

Reputation: 32386

You can easily use the "fuzziness": "AUTO". param in your search query. Refer fuzziness in match query official example

Upvotes: 1

Related Questions