Is there a way to make elasticsearch case-insensitive without altering the existing documents?

Skrudox picture Skrudox · Jun 27, 2018 · Viewed 16.7k times · Source

Does Elasticsearch allow us to query documents case-insensitive? Or should I save them as case-insensitive before querying? Or is there some setting that I should set for the whole index to make it case-insensitive?

Can you clarify this moment please?

Answer

Polynomial Proton picture Polynomial Proton · Jun 28, 2018

By Default, the fields are case-insensitive because of the mapping elastic applied.

Try below:

PUT myindex/doc/1
{
  "name":"TEST"
}

GET myindex/_mapping

It should return :

{
  "myindex": {
    "mappings": {
      "doc": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }          
        }
      }
    }
  }
}

Now if you query with below, it will return a match (notice the mapping[text and keyword]):

POST myindex/_search
{
  "query": {
    "match": {
      "name2": "test"
    }
  }
}

Now, if you explicitly specify to index the field as keyword, then it will be case-sensitive search. Try below and see; it will not return any results.

PUT myindex/_mapping/doc
{
  "properties": {
    "name2": {
      "type": "keyword"
    }
  }
}

PUT myindex/doc/1
{
  "name2":"TEST"
}


POST myindex/_search
{
  "query": {
    "match": {
      "name2": "test"
    }
  }
}

TLDR; Use default mapping or text type- if you specify the field to index only keyword type, it will be case-sensitive.