# Nest Client search empty string does not work

**URL:** <https://discuss.elastic.co/t/nest-client-search-empty-string-does-not-work/301664>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 5, 2022, 7:02pm UTC](https://discuss.elastic.co/t/nest-client-search-empty-string-does-not-work/301664 "2022-04-05T19:02:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qwe/32/101942_2.png) [@qwe](https://discuss.elastic.co/u/qwe)\
**Post date:** [April 5, 2022, 7:02pm UTC](https://discuss.elastic.co/t/nest-client-search-empty-string-does-not-work/301664/1 "2022-04-05T19:02:07Z")

</div>

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

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

```

The following works just fine:

```auto
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:

```auto
            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:

```auto
                        new MatchQuery
                        {
                            Field = Infer.Field<ContactDocument>(c => c.StandardData.Phone.Suffix("keyword")),
                            Query = ""
                        }

```

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

```

---

<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:** [April 5, 2022, 7:02pm UTC](https://discuss.elastic.co/t/nest-client-search-empty-string-does-not-work/301664/2 "2022-04-05T19:02:07Z")

</div>

Elasticsearch version 7.5 is [EOL](https://www.elastic.co/support/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: )

---

<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:** [May 3, 2022, 7:02pm UTC](https://discuss.elastic.co/t/nest-client-search-empty-string-does-not-work/301664/3 "2022-05-03T19:02:37Z")

</div>

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