Elasticsearch DSL Query - Field is not null or not empty

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

{
   "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

Have you tried using must_not and exists filters?

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.

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?

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.

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/

We’re using document level security (DLS) : to access control to the indices

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 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!

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:


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

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