# Elasticsearch DSL Query - Field is not null or not empty

**URL:** <https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005>\
**Category:** Elasticsearch\
**Created:** [January 12, 2023, 10:05am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005 "2023-01-12T10:05:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Java2avaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/java2avaj/32/99497_2.png) [@Java2avaj](https://discuss.elastic.co/u/Java2avaj)\
**Post date:** [January 12, 2023, 10:05am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/1 "2023-01-12T10:05:14Z")

</div>

I have an existing DSL query that needs to modify such that the id "12345" should be not null or not empty:

```auto
{
   "bool":{
      "must":[
         {
            "bool":{
               "should":[
                  {
                     "terms":{
                        "users":{
                           "index":"user-codes",
                           "id":"12345",
                           "path":"userCodes"
                        }
                     }
                  },
                  {
                     "terms":{
                        "users":{
                           "index":"user-codes",
                           "id":"56789",
                           "path":"userCodes"
                        }
                     }
                  }
               ]
            }
         }
      ]
   }
}

```

Can somebody help me update the query

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [January 13, 2023, 9:48am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/2 "2023-01-13T09:48:22Z")

</div>

Have you tried using `must_not` and `exists` filters?

> **[Exists query | Elasticsearch Guide \[8.6\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html#find-docs-null-values)**

---

<div class="post-metadata">

**Author:** ![Java2avaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/java2avaj/32/99497_2.png) [@Java2avaj](https://discuss.elastic.co/u/Java2avaj)\
**Post date:** [January 13, 2023, 9:52am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/3 "2023-01-13T09:52:23Z")

</div>

Hi Carly, thanks for your response. Can you please share how to do it using must\_not and exists?

AFAIK, exists parameter is a field whereas, I need to double check that id "12345" is not an empty string.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [January 13, 2023, 10:07am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/4 "2023-01-13T10:07:17Z")

</div>

Sorry your question read to me as you're looking for documents with not null or empty ids. If so the example in the above link should be what you're looking for.

Can you clarify what you mean please by

> id "12345" should not be null or empty

if my assumption isn't quite right?

---

<div class="post-metadata">

**Author:** ![Java2avaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/java2avaj/32/99497_2.png) [@Java2avaj](https://discuss.elastic.co/u/Java2avaj)\
**Post date:** [January 13, 2023, 10:23am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/5 "2023-01-13T10:23:26Z")

</div>

Thanks for your follow up response. We are using a DLS query with a terms lookup as posted below. We want to modify the query such that when ${userId1} must not be null or empty before entering the terms lookup.

> [@Java2avaj](#):
>
> ```auto
> {
> "bool":{
> "must":[
> {
> "bool":{
> "should":[
> {
> "terms":{
> "userAccessCodes":{
> "index":"user-access-codes",
> "id":"${userId1}",
> "path":"userAccessCodes"
> }
> }
> },
> {
> "terms":{
> "userAccessCodes":{
> "index":"user-access-codes",
> "id":"${userId2}",
> "path":"userAccessCodes"
> }
> }
> }
> ]
> }
> }
> ]
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [January 13, 2023, 3:10pm UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/6 "2023-01-13T15:10:02Z")

</div>

So you want a null/ value check on the value being pushed into the terms query itself?

By Elasticsearch DSL do you mean you're using the Python client?

[https://elasticsearch-dsl.readthedocs.io/en/latest/](https://elasticsearch-dsl.readthedocs.io/en/latest/)

---

<div class="post-metadata">

**Author:** ![Java2avaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/java2avaj/32/99497_2.png) [@Java2avaj](https://discuss.elastic.co/u/Java2avaj)\
**Post date:** [January 13, 2023, 5:52pm UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/7 "2023-01-13T17:52:52Z")

</div>

We’re using [document level security (DLS) :](https://docs.search-guard.com/latest/document-level-security) to access control to the indices

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [January 16, 2023, 10:24am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/8 "2023-01-16T10:24:10Z")

</div>

Thanks for the correction. I'm not familiar with DLS, so perhaps someone else might know the answer. However, looking at the [DLS page you sent](https://docs.search-guard.com/latest/document-level-security) I'm assuming the values `${userIdN}` are coming from are dynamic user attributes?

Normally I would expect value checks such as this to be carried out on the conduit that is passing the value rather than in Elasticsearch itself. But perhaps someone else has some experience and can help!

---

<div class="post-metadata">

**Author:** ![Java2avaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/java2avaj/32/99497_2.png) [@Java2avaj](https://discuss.elastic.co/u/Java2avaj)\
**Post date:** [January 16, 2023, 10:49am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/9 "2023-01-16T10:49:18Z")

</div>

Hi Carly

thanks for your response. Yes, the values of ${userIdN} are dynamic. The query I posted works properly. However, when the ${userIdN} is empty string "", we are getting an error. Thus we need to update the query such that we have to check if the ${userIdN} is NOT empty, use the terms lookup else skip. Similar to this SQL statement:

```auto

SELECT * FROM tbl where ( :${userId1} <> '' AND <TERMS_LOOKUP>)

```

---

<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:** [February 13, 2023, 10:49am UTC](https://discuss.elastic.co/t/elasticsearch-dsl-query-field-is-not-null-or-not-empty/323005/10 "2023-02-13T10:49:26Z")

</div>

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