# Exclude empty-string, null and non-existant

**URL:** https://discuss.elastic.co/t/exclude-empty-string-null-and-non-existant/181140
**Category:** Elasticsearch
**Created:** [May 15, 2019, 9:50am UTC](https://discuss.elastic.co/t/exclude-empty-string-null-and-non-existant/181140 "2019-05-15T09:50:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Pictor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pictor/32/46201_2.png) [@Pictor](https://discuss.elastic.co/u/Pictor)
#### Post date: [May 15, 2019, 9:50am UTC](https://discuss.elastic.co/t/exclude-empty-string-null-and-non-existant/181140/1 "2019-05-15T09:50:38Z")

</div>

How does one query for all documents having a field with non empty value? (aka _mandatory_ value)

I can exclude the `null` values or non existing fields via the `exists` (negated) query, but I also need to ensure that the values is **not an empty string**.  
From what I know an _empty_ string is a non-`null` value, so it will be included in results from `exists` query.

What's the best (smartest & most performant) way to do this filtering?

- could use a script to check for the property `field.length > 0`, but I don't like to rely on scripting languages in my queries (if not strictly needed); also I suppose is not ideal for performances.
- could use a `"wildcard": { "fieldname": "?*" }` together with the `must exists` query. It's the solution I am using at the moment but, still, I believe there must be some better performing way than using regular expressions.
- could _maybe (?)_ use some parameter for the `exists` query so that also empty strings are treated as `null`/non-existing value.

**What I want to achieve is to treat _empty-string_, `null` and _non-existing_ as the same (kind of "invalid value").**

Also, an additional/optional curiosity is if it makes sense (and how) to do a `trim` of the field value, to exclude strings like `" "`.

I was using the `missing` query for the opposite case (empty field).  
Is it completely deprecated? Should I always use the `must_not`-`exists` query instead?

---

<div class="post-metadata">

### Author: ![Pictor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pictor/32/46201_2.png) [@Pictor](https://discuss.elastic.co/u/Pictor)
#### Post date: [May 15, 2019, 7:36pm UTC](https://discuss.elastic.co/t/exclude-empty-string-null-and-non-existant/181140/2 "2019-05-15T19:36:20Z")

</div>

Looks like using `"wildcard": { "fieldname": "?*" }` is the best solution at the moment.  
But I am not sure how it scales when the `wildcard`+`exists` check is performed on more than one field.  
Not sure how much could it slow down the search.

Please, could anybody with solid experience enlighten me?

---

<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: [June 12, 2019, 7:36pm UTC](https://discuss.elastic.co/t/exclude-empty-string-null-and-non-existant/181140/3 "2019-06-12T19:36:23Z")

</div>

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