Painless Script: How to Check for Null Values

Minh Vu

By Minh Vu

Updated Jan 05, 2024

Figure: Painless Script: How to Check for Null Values

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

In this tutorial, I will show you how to check for null values in the Painless script in Elasticsearch.

I will show you both two cases whether you want to check for null values in the document field or the script parameter.

The usage for those cases is also different when you are searching documents or updating documents.

Contents

Check for Null Values while Searching Documents in Painless Script

Check for Null Values in Document Fields using doc Variable

While searching for documents, you can access the document fields in the doc variable.

To check for null values of a document field in the Painless script while searching documents, you can use the if (doc['some_field'].size() == 0) statement.

{ "query": { "script": { "script": { "lang": "painless", "source": """ if (doc['some_field'].size() == 0) { // do something } else { // do something } """ } } } }

For the nested fields, you can check like this: if (doc['some_field']['some_nested_field'].size() == 0).

Check for Null Values in Script Parameters using params Variable

You can also specify the parameters in the params section to add additional parameters to the script.

So, to check if a script parameter is null or not in the Painless script while updating documents, you can use the if (params.some_param == null) statement.

{ "query": { "script": { "script": { "lang": "painless", "source": """ if (params.some_param == null) { emit(true); } else { return false; } """, "params": { "some_param": "some_value" } } } } }

Check for Null Values while Updating Documents in Painless Script

Check for Null Values in a Document while Updating using ctx._source Variable

To check if a document field is null or not in the Painless script while updating documents, you can use the if (ctx._source.some_field == null) statement.

{ "script": { "lang": "painless", "source": """ if (ctx._source.some_field == null) { return true; } else { return false; } """ } }

For the nested fields, you can check like this: if (ctx._source.some_field?.some_nested_field == null).

Check for Null Values in Script Parameters while Updating using params Variable

Similarly, you can also check for null values in the script parameters while searching documents.

{ "script": { "lang": "painless", "source": """ if (params.some_param == null) { return true; } else { return false; } """, "params": { "some_param": "some_value" } } }

Conclusion

In this tutorial, I have shown you how to check for null values in the Painless script in Elasticsearch.

Leave a comment below if you have any questions or suggestions.

Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

adams

Feb 28, 2024

cool guide, worked like a charm

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.