# NEST: Avoid storing default values false, 0 etc

**URL:** <https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457>\
**Category:** Elasticsearch\
**Created:** [July 18, 2018, 6:42am UTC](https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457 "2018-07-18T06:42:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henr1k](https://avatars.discourse-cdn.com/v4/letter/h/f475e1/32.png) [@Henr1k](https://discuss.elastic.co/u/Henr1k)\
**Post date:** [July 18, 2018, 6:42am UTC](https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457/1 "2018-07-18T06:42:50Z")

</div>

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

I know [Json.NET](http://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?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [July 18, 2018, 7:52am UTC](https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457/2 "2018-07-18T07:52:41Z")

</div>

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](https://www.nuget.org/packages/NEST.JsonNetSerializer/), hook up [`JsonNetSerializer`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/custom-serialization.html#_jsonnetserializer) as the serializer to use for `_source` documents, and attribute the properties with the [Json.NET](http://Json.NET) `JsonProperty` attribute.

---

<div class="post-metadata">

**Author:** ![Henr1k](https://avatars.discourse-cdn.com/v4/letter/h/f475e1/32.png) [@Henr1k](https://discuss.elastic.co/u/Henr1k)\
**Post date:** [July 18, 2018, 8:12am UTC](https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457/3 "2018-07-18T08:12:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [July 18, 2018, 8:36am UTC](https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457/4 "2018-07-18T08:36:22Z")

</div>

> [@Henr1k](#):
>
> Unset or false/0 could be found with the negated query, not true or not postive, if that is desired, right?

You would need [a `bool` query with an `exists` query in `must_not` clause](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html#_literal_missing_literal_query) 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.

> [@Henr1k](#):
>
> Will the JsonNetSerializer work with the mapping done in Nest or do I have to redo mapping for JsonNet?

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.

---

<div class="post-metadata">

**Author:** ![Henr1k](https://avatars.discourse-cdn.com/v4/letter/h/f475e1/32.png) [@Henr1k](https://discuss.elastic.co/u/Henr1k)\
**Post date:** [July 18, 2018, 8:59am UTC](https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457/5 "2018-07-18T08:59:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 15, 2018, 8:59am UTC](https://discuss.elastic.co/t/nest-avoid-storing-default-values-false-0-etc/140457/6 "2018-08-15T08:59:24Z")

</div>

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