Janko
Janko

Reputation: 9335

How to tolerate typos with Elasticsearch?

I'm using query_string, and I want to be able for my search to tolerate typos. In the query below I typed The Gren Mile, but it didn't return any results:

curl -XPOST 127.0.0.1:9200/test
curl -XPOST 127.0.0.1:9200/test/movies -d '{"title": "The Green Mile"}'
curl -XPOST 127.0.0.1:9200/test/_refresh
curl -XPOST 127.0.0.1:9200/test/movies/_search -d '{
  "query": {
    "query_string": {
      "query": "The Gren Mile",
      "default_operator": "AND"
    }
  }
}'

It says in the Elasticsearch docs that fuzziness is by default AUTO (which tolerates bigger typos as the word gets bigger), so I don't know why it doesn't work. I tried manually setting fuzziness: 2, but it didn't work either. Does this option do something else than I think it does?

Upvotes: 2

Views: 3422

Answers (1)

Olly Cruickshank
Olly Cruickshank

Reputation: 6180

I don't have an answer as to why the fuzziness parameter isn't working - it didn't work for me either, possibly this is a bug?

However putting the fuzzy operator ~ directly in the string works:

curl -XPOST 127.0.0.1:9200/test/movies/_search?pretty -d '{
  "query": {
    "query_string": {
      "query": "The Gren~ Mile",
      "default_operator": "AND"
    }
  }
}'

returns the record:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.263573,
    "hits" : [ {
      "_index" : "test",
      "_type" : "movies",
      "_id" : "AUxq8KE1EKExB5CrkB_W",
      "_score" : 0.263573,
      "_source":{"title": "The Green Mile"}
    } ]
  }
}

Match query

Using fuzziness with a match query works. Either combine the query_string with a match query (to form a single query) or do a match query if the user's original search returns no results.

  "query": {
    "match": {
      "title": {
        "query": "The Gren Mile",
        "operator" : "and",
        "fuzziness": 2
      }
    }
  }

Upvotes: 1

Related Questions