4
votes

I am using a statistical facet (@see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-statistical-facet.html) to perform an aggregation on a few fields across the documents in my Elastic Search index.

I was wondering if anyone knew if the API provided a means to provide a default value if a particular field does not exist. For example, if a field does not exist use 0 (zero) as that fields value. By default it seems to give a null pointer exception when the aggregation is taking place.

My initial thoughts are to utilize a script field to test if the aggregation field is null and perform the default 0 logic there.

1
Or you can reindex and provide a default value upfront in your mapping.javanna

1 Answers

4
votes

As you stated in your question, you could try a script field as defined here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-statistical-facet.html#_script_field_2

For example: "script" : "_source.place == null ? null : 0"

I'll admit that I have not tried this on a statistical facet, but I have used a similar script on a terms stats facet and it worked fine.