# Wrong term search result with case\_insensitive=true

**URL:** <https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916>\
**Category:** Elasticsearch\
**Created:** [May 30, 2022, 8:46am UTC](https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916 "2022-05-30T08:46:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikeSER](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@MikeSER](https://discuss.elastic.co/u/MikeSER)\
**Post date:** [May 30, 2022, 8:46am UTC](https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916/1 "2022-05-30T08:46:22Z")

</div>

I have a problem with a specific search when using a term query and case\_insensitive=true.

To demonstrate that I've created a shell script.  
It deletes then creates index "a" with an attribute "my\_kw" of type "keyword".  
One document is stored with a value of "Gamma--MÖma" for "my\_kw".

Then a term search is executed with value "Gamma--MÖma", it finds the document (as expected).  
Then the search is executed with "gamma--mÖma" and case\_insensitive=true, it finds the document (as expected).  
But then a search is executed with "gamma--möma" and case\_insensitive=true. Here it finds nothing, which comes unexpected.

Any idea why it is so?

The script:

```auto
# Setup:
#CONNECTION="localhost:9200"
CONNECTION="--cacert http_ca.crt -u elastic:xbsv-AdsDWOcefIGzyzJ https://localhost:19200"
INDEX="a"
set -vx

# Delete old index
deleteIndex() {
  curl -XDELETE $CONNECTION/$INDEX
}

# Set up index with mappings
createIndex() {
curl -XPUT $CONNECTION/$INDEX -H 'Content-Type: application/json' -d '{
  "settings": {
    "analysis": {
      "analyzer" : {
        "automatic_analyzer" : {
          "filter" : [
            "lowercase",
            "asciifolding"
          ],
          "tokenizer" : "standard"
        }
      }
    },
    "number_of_replicas": "0",
    "number_of_shards": "1"
  },
  "mappings": {
    "properties": {
      "my_kw": {
        "type": "keyword",
        "store": true
      }
    }
  }
}'
}

# $1 - doc id
# $2 - keyword value
function insert() {
        ( echo -n '{"my_kw": "'$2'"}' ) | curl -XPOST $CONNECTION/$INDEX/_doc/$1 -H 'Content-Type: application/json' -d "@-"
}

# $1 - search term
# $2 - case_insensitive false/true
function search() {
echo
echo "===== Search ======"
curl -XPOST $CONNECTION/$INDEX/_search -H 'Content-Type: application/json' -d '
{
  "query": {
    "term" : {
        "my_kw": {
          "value": "'$1'",
          "case_insensitive": '$2',
          "boost": 1
        }
    }
  }
}
' | tr , \\012
}
 
deleteIndex
createIndex

insert 1 "Gamma--MÖma"
 
sleep 2

# Searching with correct case and case_insensitive false => expect and get one hit
search "Gamma--MÖma" false 
# Searching with wrong case, but umlaut in correct case and case_insensitive true => expect and get one hit
search "gamma--mÖma" true 

# Searching with wrong case (umlaut also) and case_insensitive true => expect one hit, but get no hit
search "gamma--möma" true

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 30, 2022, 1:24pm UTC](https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916/2 "2022-05-30T13:24:29Z")

</div>

Hi!!

Read this [post](https://discuss.elastic.co/t/how-to-use-case-insensitive-option-in-dsl-terms-query/267371/2).

---

<div class="post-metadata">

**Author:** ![MikeSER](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@MikeSER](https://discuss.elastic.co/u/MikeSER)\
**Post date:** [May 30, 2022, 1:41pm UTC](https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916/3 "2022-05-30T13:41:23Z")

</div>

Thanks for the hint, but in this case a terms query is **not** used, but a **term** query!

The term query supports the case\_insensitive flag.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 30, 2022, 3:41pm UTC](https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916/4 "2022-05-30T15:41:05Z")

</div>

Sorry for the misunderstanding.

In fact, this Ö character doesn't seem to be converted using case\_insensitive.

Anyway, using the normalizer as the one in the post above said, you will be able to solve it..

---

<div class="post-metadata">

**Author:** ![MikeSER](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@MikeSER](https://discuss.elastic.co/u/MikeSER)\
**Post date:** [May 31, 2022, 5:02am UTC](https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916/5 "2022-05-31T05:02:05Z")

</div>

Sorry from me too. I have overread the note on case\_insensitive in [Term query | Elasticsearch Guide [8.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html#term-field-params).  
"Allows **ASCII** case insensitive matching ..."  
That was so unexpected to me ...

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 28, 2022, 5:02am UTC](https://discuss.elastic.co/t/wrong-term-search-result-with-case-insensitive-true/305916/6 "2022-06-28T05:02:24Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
