Nest Client search empty string does not work

I am trying to implement search for documents where a field exists and is not empty.
The field is mapped as follows:

{
  "mapping": {
    "properties": {
      "standardData": {
        "properties": {
...
          "phone": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
...
        }
      }
    }
  }
}

The following works just fine:

POST local-contacts/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "standardData.phone"
          }
        }
      ],
      "must_not": {
        "match": {
          "standardData.phone.keyword": {
            "query": ""
          }
        }
      }
    }
  }
}

However I need to do this from my C# code, and using Nest Client does not work as expected and is returning documents with empty standardData.phone fields:

            var boolQuery = new BoolQuery
            {
                Must = new[]
                {
                    new QueryContainer(
                        new ExistsQuery
                        {
                            Field = Infer.Field<ContactDocument>(c => c.StandardData.Phone),
                        })
                },
                MustNot = new []
                {
                    new QueryContainer(
                        new TermQuery
                        {
                            Field = Infer.Field<ContactDocument>(c => c.StandardData.Phone.Suffix("keyword")),
                            Value = ""
                        }
                    )
                }
            };
            var ret = _elasticClient.Search<ContactDocument>(new SearchRequest(Indices.Index<ContactDocument>()) { Query = boolQuery, Size = 100});

What am I doing wrong here? Is this a bug in Nest Client?
Nest package version is 7.6.1, Elasticsearch version 7.5

P.S.
I have also tried following options instead of TermQuery in MustNot, but they all return documents with empty standardData.phone fields:

                        new MatchQuery
                        {
                            Field = Infer.Field<ContactDocument>(c => c.StandardData.Phone.Suffix("keyword")),
                            Query = ""
                        }
                        new RegexpQuery
                        {
                            Field = Infer.Field<ContactDocument>(c => c.StandardData.Phone.Suffix("keyword")),
                            Value = "^$"
                        }

Elasticsearch version 7.5 is EOL and no longer supported. Please upgrade ASAP.

(This is an automated response from your friendly Elastic bot. Please report this post if you have any suggestions or concerns :elasticheart: )

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