Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch wildcard case-sensitive

How to make wildcard case-insensitive?

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

like image 234
Skrudox Avatar asked Jun 29 '18 18:06

Skrudox


2 Answers

Since version 7.10 the wildcard query supports special parameter case_insensitive (boolean). Example of case-insensitive search:

GET /_search
{
  "query": {
    "wildcard": {
      "my_field": {
        "value": "ki*y",
        "case_insensitive": true
      }
    }
  }
}
like image 84
Stalinko Avatar answered Sep 19 '22 17:09

Stalinko


Wildcards are not_analyzed. It depends on what analyzers you've provided for the field you're searching. But if you're using the default analyzers then a wildcard query will return case-insensitive results.

Example: Post two names in a sample index one is "Sid" and other "sid".

POST sample/sample
{
  "name" : "sid"
}

POST sample/sample
{
  "name" : "Sid"
}

Then perform a wildcard query:

GET sample/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "s*"
      }
    }
  }
}

This will return me both the documents:

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "sample",
        "_type": "sample",
        "_id": "AWRPM87Wb6oopELrnEKE",
        "_score": 1,
        "_source": {
          "name": "Sid"
        }
      },
      {
        "_index": "sample",
        "_type": "sample",
        "_id": "AWRPM9tpb6oopELrnEKF",
        "_score": 1,
        "_source": {
          "name": "sid"
        }
      }
    ]
  }
}

But if you perform a wildcard query on "S*" it will return nothing. Because the default token filter stores the terms in lowercase and the term "Sid" is stored as "sid" in the inverted index.

like image 28
TechnocratSid Avatar answered Sep 18 '22 17:09

TechnocratSid