There are several ways to set default values for null or missing fields in Elasticsearch depending on how you ingest your data.
In this tutorial, I will show you 2 ways to set default values for missing fields in Elasticsearch:
- Using the
null_value
parameter in index mapping. - Using the ingest pipeline.
Contents
- Set Default Values for Missing Fields in Elasticsearch using the
null_value
Parameter - Set Default Values for Missing Fields in Elasticsearch using Ingest Pipeline
- Conclusion
Set Default Values for Missing Fields in Elasticsearch using the null_value
Parameter
The null_value
parameter allows you to set a default value for missing fields in Elasticsearch. However, please note that it only works if the field value is explicitly set to null
.
For example, if you have the following index mapping:
If you want to set the name
field to Unknown
if it is missing, you can do so by adding the null_value
parameter to the mapping:
Now, if you index a document with the name
field explicitly set to null
, the name
field will be set to Unknown
:
Then, you can get the document and see that the name
field is set to Unknown
:
However, if you index a document with the name
field missing, the name
field will not be set to Unknown
, it will be ignored instead:
Querying the document will return the following result:
If you want to set the name
field to Unknown
if it is missing, I will show you how to use the ingest pipeline instead.
Set Default Values for Missing Fields in Elasticsearch using Ingest Pipeline
The ingest pipeline allows you to perform different operations on your data before indexing it into Elasticsearch.
One of the operations that you can perform is to set a value for a field.
We can take advantage of this operation to set a default value for missing fields in Elasticsearch.
You can create an ingest pipeline using the following request:
The above pipeline will set the name
field to Unknown
if it is missing.
Apply Ingest Pipeline to Indexing Request
Now, if you index a document with the name
field missing, the name
field will be set to Unknown
:
Querying the document will return the following result:
Apply Ingest Pipeline to Logstash
If you are using Logstash to ingest data into Elasticsearch, you can apply the ingest pipeline to Logstash by using the pipeline
option in the Elasticsearch output plugin:
Conclusion
In this tutorial, you have learned how to set default values for missing fields in Elasticsearch.
If you have any questions, please feel free to leave a comment below.
Comments
Be the first to comment!