Karthikeyan
Karthikeyan

Reputation: 2011

Elastic Search wildcard query not working with case insensitive ( for lower case)

I am trying to fetch records from elasticsearch using wildcard queries.

Please find the below query

get my_index12/_search
{ 
  "query": { 
  "wildcard": { 
  "code.keyword": { 
          "value": "*ARG*" 
        } 
    } 
  } 
}

It's working and giving expected results for the above query., but it is not working for the lower case value.

get my_index12/_search
{ 
  "query": { 
  "wildcard": { 
  "code.keyword": { 
          "value": "*Arg*" 
        } 
    } 
  } 
}

Upvotes: 2

Views: 3974

Answers (2)

Richa
Richa

Reputation: 7649

Try Following:

Mapping:

PUT my_index12
{
"settings": {
"analysis": {
  "analyzer": {
    "custom_analyzer": {
      "type": "custom",
      "tokenizer": "whitespace",
      "char_filter": [
        "html_strip"
      ],
      "filter": [
        "lowercase",
        "asciifolding"
      ]
    }
   }
  }
},
"mappings": {
"doc": {
  "properties": {
    "code": {
      "type": "text",
       "analyzer": "custom_analyzer"
      }
    }
  }
 }
}

Then Run Query String Query

GET my_index12/_search
{
"query": {
"query_string": {
  "default_field": "code",
  "query": "AB\\-7000*"
    }
  }
}

It will also work for ab-7000*

Let me know if it works for you.

Upvotes: 1

SouXin
SouXin

Reputation: 1564

You have to normalize your keyword field:

ElasticSearch normalizer

Something like (from documentation):

PUT index
 {
  "settings": {
    "analysis": {
    "normalizer": {
       "my_normalizer": {
       "type": "custom",
       "char_filter": [],
       "filter": ["lowercase", "asciifolding"]
     }
   }
 }
},
"mappings": {
  "_doc": {
    "properties": {
      "foo": {
        "type": "keyword",
        "normalizer": "my_normalizer"
      }
    }
   }
  }
}

UPDATE Some additional info:

Only parts of the analysis chain that operate at the character level are applied. So for instance, if the analyzer performs both lowercasing and stemming, only the lowercasing will be applied: it would be wrong to perform stemming on a word that is missing some of its letters.

By setting analyze_wildcard to true, queries that end with a * will be analyzed and a boolean query will be built out of the different tokens, by ensuring exact matches on the first N-1 tokens, and prefix match on the last token.

Upvotes: 0

Related Questions