# Term query with empty string and case\_insensitive setting become invalid

**URL:** <https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961>\
**Category:** Elasticsearch\
**Created:** [May 22, 2024, 6:17am UTC](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961 "2024-05-22T06:17:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DannyDuo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dannyduo/32/126980_2.png) [@DannyDuo](https://discuss.elastic.co/u/DannyDuo)\
**Post date:** [May 22, 2024, 6:17am UTC](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961/1 "2024-05-22T06:17:55Z")

</div>

Hello, everyone

Here is my search request and response, I filter oceanchannelid to empty value, but I still get oceanchannelid with empty value.

```auto
{
  "bool" : {
    "must_not" : [
      {
        "term" : {
          "oceanchannelid" : {
            "value" : "",
            "boost" : 1.0,
            "case_insensitive" : true
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

```

```auto
"hits": [
      {
        "_index": "20240522",
        "_id": "acc73ed550bc489987b6c937df5ffa60",
        "_score": 8.986232,
        "_source": {
          "stime": "2024-05-22 11:42:04",
          "timestamp": "2024-05-22 11:42:01",
          "oceanchannelid": "",
          "os": "windows"
        }
      }
]

```

If I remove the case\_insensitive setting, it works.

```auto
{
  "bool" : {
    "must_not" : [
      {
        "term" : {
          "oceanchannelid" : {
            "value" : "",
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

```

The type of oceanchannelid is keyword.

```auto
"oceanchannelid": {
    "type": "keyword",
    "store": true
  },

```

So if I want to filter empty string, I cannot set case\_insensitive to true?

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 22, 2024, 3:20pm UTC](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961/2 "2024-05-22T15:20:02Z")

</div>

Hi DannyDuo,

Yep, this looks like a weird side effect of a `term` query with `case_insensitive: true`.

As a workaround, I suggest either setting up a [lowercase normalizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-normalizers.html) on the `oceanchannelid` field to implicitly support searching case insensitively, or using a different matching type, for example [regexp](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html):

```auto
"bool": {
  "must": [
    {
      "regexp": {
        "oceanchannelid" : {
          "value": ".+", // 1+ characters
          "boost" : 1.0,
          "case_insensitive": true
        }
      }
    }
  ],
...

```

---

<div class="post-metadata">

**Author:** ![DannyDuo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dannyduo/32/126980_2.png) [@DannyDuo](https://discuss.elastic.co/u/DannyDuo)\
**Post date:** [May 23, 2024, 2:11am UTC](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961/3 "2024-05-23T02:11:13Z")

</div>

Hi demjened,

Thanks for your reply!

The filter will become invalid **only when I filter empty value and set case\_insensitive to true** , the filter works when the value is not empty.

So I just set **case\_insensitive: false** when I filter empty value.

I wonder if it will be fixed in next version?

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 23, 2024, 3:32pm UTC](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961/4 "2024-05-23T15:32:11Z")

</div>

I reported this as a bug; I'm afraid I don't know if it will be fixed by the next release.

---

<div class="post-metadata">

**Author:** ![DannyDuo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dannyduo/32/126980_2.png) [@DannyDuo](https://discuss.elastic.co/u/DannyDuo)\
**Post date:** [May 24, 2024, 3:10am UTC](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961/5 "2024-05-24T03:10:12Z")

</div>

That's enough. Could you please share the issue link? I would set the link as solution.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 24, 2024, 1:51pm UTC](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961/6 "2024-05-24T13:51:50Z")

</div>

Sure, here you go:

> <https://github.com/elastic/elasticsearch/issues/108968>
>
> \### Elasticsearch Version
> 
> 8.15.0
> 
> \### Installed Plugins
> 
> \_No response\_
> 
> \### Jav…a Version
> 
> \_bundled\_
> 
> \### OS Version
> 
> Elastic Cloud
> 
> \### Problem Description
> 
> Context in \[Discuss forum post\](https://discuss.elastic.co/t/term-query-with-empty-string-and-case-insensitive-setting-become-invalid/359961). 
> 
> The \`term\` query on a keyword field with an empty value ("") behaves differently depending on the \`case\_insensitive\` flag:
> \- If \`case\_insensitive==false\` (default), the query matches on "",
> \- If \`case\_insensitive==true\`, the query does not match on "".
> 
> We should make this consistent.
> 
> \### Steps to Reproduce
> 
> Tested on an Elastic Cloud deployment with version 8.15.0-SNAPSHOT.
> 
> \`\`\`
> PUT term-match-test
> {
> "mappings": {
> "properties": {
> "text": {
> "type": "keyword"
> }
> }
> }
> }
> \`\`\`
> 
> \`\`\`
> PUT term-match-test/\_doc/1
> {
> "text": ""
> }
> \`\`\`
> 
> Case sensitive search returns 1 hit:
> \`\`\`
> POST term-match-test/\_search
> {
> "query": {
> "term": {
> "text": {
> "value": ""
> }
> }
> }
> }
> \`\`\`
> 
> Case insensitive search returns 0 hits:
> \`\`\`
> POST term-match-test/\_search
> {
> "query": {
> "term": {
> "text": {
> "value": "",
> "case\_insensitive": true
> }
> }
> }
> }
> \`\`\`
> 
> 
> \### Logs (if relevant)
> 
> \_No response\_
