Search code examples
elasticsearchelasticsearch-5

add fuzziness to elasticsearch query


I have a query for an autocomplete/suggestions index that looks like this:

{
    "size": 10,
    "query": {
        "multi_match": {
            "query": "'"+search_text+"'",
            "type": "bool_prefix",
            "fields": [
                "company_name",
                "company_name._2gram",
                "company_name._3gram"  
            ]
        }       
      }
  }

This query works exactly as I want it to. However I want to add fuzziness:"AUTO" to this query. I read the documentation and tried adding it like this:

{
    "size": 10,
    "query": {
        "multi_match": {
            "query": {
                "fuzzy": {
                    "value": "'"+search_text+"'",
                    "fuzziness": "AUTO"
                }
            },
            "type": "bool_prefix",
            "fields": [
                "company_name",
                "company_name._2gram",
                "company_name._3gram"
            ]
        }
    }
}

But I get a this error

```
"type": "parsing_exception",
"reason": "[multi_match] unknown token [START_OBJECT] after [query]",```

This is causing my query not to work.


Solution

  • There is no need to add a fuzzy query. To add fuzziness to a multi-match query you need to add the fuzziness property as described here :

    Since you are using bool_prefix as the type of multi-match query, so it creates a match_bool_prefix on each field that analyzes its input and constructs a bool query from the terms. Each term except the last is used in a term query. The last term is used in a prefix query.

    Adding a working example with index data, mapping, search query, and search result

    Index Mapping:

    {
      "mappings": {
        "properties": {
          "company_name": {
            "type": "search_as_you_type",
            "max_shingle_size": 3
          },
          "serviceTitle": {
            "type": "search_as_you_type",
            "max_shingle_size": 3
          },
          "services": {
            "type": "search_as_you_type",
            "max_shingle_size": 3
          }
        }
      }
    }
    

    Index Data:

    {
        "company_name":"sequencing how shingles are actually used"
    }
    

    Search Query:

       {
        "size": 10,
        "query": {
            "multi_match": {
                "query": "sequensing how shingles",
                "type": "bool_prefix",
                "fields": [
                    "company_name",
                    "company_name._2gram",
                    "company_name._3gram"  
                ],
                "fuzziness":"auto"
            }       
          }
      }
    

    Search Result:

    "hits": [
          {
            "_index": "65153201",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.5465959,
            "_source": {
              "company_name": "sequencing how shingles are actually used"
            }
          }
        ]
    

    If you want to query sequensing, and get the above document, then you need to change the type of multi-match from bool_prefix to another type according to your use case.