Skrudox
Skrudox

Reputation: 949

Elasticsearch wildcard case-sensitive

How to make wildcard case-insensitive?

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

Upvotes: 11

Views: 19964

Answers (4)

Stalinko
Stalinko

Reputation: 3656

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
      }
    }
  }
}

Upvotes: 16

Adiii
Adiii

Reputation: 60104

I was looking for the same option for nodejs client, so came across this question, so posting as an answer might help someone else.

I have to convert the term to lowercase and its worked for me *${term.toLowerCase()}* Here is the complete function

searchUsers(term, from, limit) {
    let users = await EsClient.search({
        index: 'users',
        type: 'users',
        body: {
            from,
            size: limit,
            query: {
                bool: {
                    should: [
                        {
                            wildcard: {
                                email: {
                                    value: `*${term.toLowerCase()}*`
                                }
                            }
                        },
                        {
                            wildcard: {
                                "name.keyword": {
                                    value: `*${term.toLowerCase()}*`
                                }
                            }
                        }
                      ],
                    must_not: {
                        terms: {_id: blacklist}
                    }
                }
            }
        }
    });
}

Upvotes: 2

Mariusz Pala
Mariusz Pala

Reputation: 1071

In my case this is not true, it is case sensitive by default - I am using ES 7.2. In you sample the type of the field is "text" not "keyword"

Upvotes: 2

TechnocratSid
TechnocratSid

Reputation: 2415

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.

Upvotes: 6

Related Questions