NEST: Avoid storing default values false, 0 etc


(Henrik Gedionsen) #1

Hi, is it possible to avoid serializing default values for int, boolean etc.
E.g.:
"onlyCheckingForPositiveValue" : 0,
"onlyCheckingForTrueValue" : false,

I know Json.NET has this attribute:
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
but it is ignored and the value is stored and indexed.

Is there a attribute that does the same in NEST?


(Russ Cam) #2

No there isn't an attribute to do this. I think having one would be confusing; the default values for value types are only relevant to NEST when deserializing to a POCO instance. If a value isn't serialized as part of the document, then no value will exist in _source, which is what you want to achieve I think, but also

  • no value will exist in the inverted index to query on
  • no value will exist in doc values to aggregate or sort on

If these are both things that you'd also like to achieve, then the closest you can come with the least amount of effort would be to make the property a Nullable<T> e.g. bool -> bool?, and not set a value. as null values are not serialized by default.

If you really did want to have DefaultValueHandling.Ignore behaviour, then you can reference Nest.JsonNetSerializer nuget package, hook up JsonNetSerializer as the serializer to use for _source documents, and attribute the properties with the Json.NET JsonProperty attribute.


(Henrik Gedionsen) #3

That is exactly what I want to archive.
I am aware of the side effects and it should be used with caution.
I do only want to filter on positive int or true, no sorting.
Unset or false/0 could be found with the negated query, not true or not postive, if that is desired, right?

Will the JsonNetSerializer work with the mapping done in Nest or do I have to redo mapping for JsonNet?


(Russ Cam) #4

You would need a bool query with an exists query in must_not clause to find documents that are now missing a value. The implication here is that missing and default value for a value type in .NET are treated the same on the Elasticsearch, which may be confusing.

It should work exactly the same if you're running with the defaults.

I'm curious to know why you might want to not serialize these values? With compression, the storage overhead for them is generally going to be small.


(Henrik Gedionsen) #5

I was also thinking of the overhead sending them over the line and serializing/deserializing them.
But I guess it is not to big a overhead, I was just used to do that when working with solr and could not find the feature in Nest.

I am considering requesting the feature and contributing it. Haven't looked into how it works yet, though.


(system) #6

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