Search code examples
elasticsearchfuzzy-search

Some weird problem with fuzzy query in elasticsearch


I have one doc in es

        "_type": "_doc",
        "_id": "109487",
        "_score": null,
        "_source": {
          "id": "109487",
          "title": "Interstellar",
          "year": 2014,
          "genre": [
            "Sci-Fi",
            "IMAX"
          ]
        },
        "sort": [
          "Interstellar"
        ]
      }

I am searching with a fuzzy query like

{
    "query": {
        "fuzzy": {
            "title": {"value": "intersteller", "fuzziness": 1}
        }
    }
}

But the weird thing is if i am searching with small i in intersteller then i am getting the desired record with title as Interstellar but if i am searching with Capital I ie if my query is

    "query": {
        "fuzzy": {
            "title": {"value": "Intersteller", "fuzziness": 1}
        }
    }
}

then am not getting and docs from db .. just wanted to understand what is happening behind the scenes


Solution

  • The fuzzy query does not analyze the text. Mostly fuzzy query acts like a term query itself.

    In your case "title" field must be using standard analyzer. So "Intersteller" is indexed as "intersteller". Now when you are performing a fuzzy query on "intersteller", you will get the result but not with "Intersteller"

    To know more about fuzzy query refer to this elasticsearch blog


    It is better to use a match query along with the fuzziness parameter

    {
      "query": {
        "match": {
          "title": {
            "query": "Intersteller",
            "fuzziness": "auto"
          }
        }
      }
    }
    

    If you want use fuzzy query, then you need to increase the fuzziness parameter, to get your document to match

    {
      "query": {
        "fuzzy": {
          "title": {
            "value": "Intersteller",
            "fuzziness": 3
          }
        }
      }
    }