Term query with empty string and case_insensitive setting become invalid

Hello, everyone

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

{
  "bool" : {
    "must_not" : [
      {
        "term" : {
          "oceanchannelid" : {
            "value" : "",
            "boost" : 1.0,
            "case_insensitive" : true
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
"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.

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

The type of oceanchannelid is keyword.

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

So if I want to filter empty string, I cannot set case_insensitive to true?

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 on the oceanchannelid field to implicitly support searching case insensitively, or using a different matching type, for example regexp:

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

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?

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

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

Thanks for your help!

Sure, here you go: