Elasticsearch script breaking change query Accessing missing document value

Hi all,

We are using elastic version 7.10.2 and it is migrated from 6.8.6.
I want to compare two fields from a same document using Scripting .Below query was working in elastic version 6.8.6 now after upgrade to 7.10 its not working

Here is my query : find records where nickname is equal to firstname.

{
  "query": {
    "bool": {
      "must": [{
        "script": {
          "script": {
            "inline": "doc['nickname.keyword'].value == doc['firstname.keyword'].value",
            "lang": "painless",
            
          }
        } 
      }]
    }
  }
}

It gives error like script_exception
Reason: all shards failed and
"caused by":{
"type":"illegal_state_exception",
"reason":"A document doesnt have value for field! Use doc[].size()==0 to check if document is missing field"
}

This issue specified in breaking changes in 7.0 here. It is because the field we have used in query is may be null for some records/document.

Breaking changes in 7.0 | Elasticsearch Reference [7.11] | Elastic

To resolve this issue I have added condition like doc[].size!=0 now error is gone but it did not give any records in response (empty response in hits:) but in elastic I have records where nickname == firstname. Kindly help

New Query

{
  "query": {
    "bool": {
      "must": [{
        "script": {
          "script": {
            "inline": "doc['nickname.keyword'].size()!=0 &&  doc['firstname.keyword'].size()!=0 &&doc['nickname.keyword'].value == doc['firstname.keyword'].value",
            "lang": "painless",
            
          }
        } 
      }]
    }
  }
}

I also tried put same script in filter instead of must but that also not worked.
and using doc['nickname.keyword'].size()>0 instead ofdoc['nickname.keyword'].size()!=0 also not worked. Kindly help

Hi,
Kindly use this query

{
  "query": {
    "bool": {
      "must": [{
        "script": {
          "script": {
            "inline": "doc['nickname.keyword'].size()!=0 &&  doc['firstname.keyword'].size()!=0 &&doc['nickname.keyword'].value.equalsIgnoreCase(doc['firstname.keyword'].value)",
            "lang": "painless",
            
          }
        } 
      }]
    }
  }
}

"==" operator compares object references while equalsIgnoreCase() compares both strings ignoring case.