Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 773
Skrudox Avatar asked Jun 27 '18 18:06

Skrudox


1 Answers

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.

like image 77
Polynomial Proton Avatar answered Nov 09 '22 19:11

Polynomial Proton