# ValuesSourceType in ValuesSourceAggregationBuilder constructors

**URL:** <https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208>\
**Category:** Elasticsearch\
**Created:** [September 12, 2019, 8:58am UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208 "2019-09-12T08:58:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deb/32/46162_2.png) [@Deb](https://discuss.elastic.co/u/Deb)\
**Post date:** [September 12, 2019, 8:58am UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/1 "2019-09-12T08:58:23Z")

</div>

Can someone explain me what does `ValuesSourceType` fields denote in `ValuesSourceAggreationBuilder` constructors?

- `ValuesSourceAggregationBuilder(StreamInput in, ValuesSourceType valuesSourceType, ValueType targetValueType)`
- `ValuesSourceAggregationBuilder(StreamInput in, ValuesSourceType valuesSourceType)`

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [September 12, 2019, 8:01pm UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/2 "2019-09-12T20:01:20Z")

</div>

`ValuesSourceType` is roughly the type of data that the aggregator expects to work with. For example, `MaxAggregatorBuilder` uses `ValuesSourceType.NUMERIC` because it only works with numeric fields. `ValuesSourceType.ANY` is sort of a wildcard that means the aggregator can accept type of field. The `missing` aggregator is a good example: it doesn't really care what the data type is, it's just looking to see if the doc is missing a value or not.

That's a rough approximation, it's a little more complicated because the VS Type is used in a few different places. The main location is [`ValuesSourceConfig#resolve()`](https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfig.java#L64), and helps resolve what kind of field data implementation we should load. Scripting, `missing`, and `value_type` parameters on the agg affect this decision too.

And to head off an obvious followup question, `ValueType` sorta maps the VSType, formatter and field data implementation to a specific field type. E.g. a `long` and `double` field have different `ValueType`s, but both of those ValueTypes map to a ValuesSourceType.Numeric.

It's all a bit confusing and an area we're [actively trying to refactor](https://github.com/elastic/elasticsearch/pull/46196) 🙂

---

<div class="post-metadata">

**Author:** ![Deb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deb/32/46162_2.png) [@Deb](https://discuss.elastic.co/u/Deb)\
**Post date:** [September 13, 2019, 12:30pm UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/3 "2019-09-13T12:30:02Z")

</div>

Thanks @polyfractal for the explanation.

Can you also explain me the use of `ValuesSourceAggregationBuilder(StreamInput in, ValuesSourceType valuesSourceType)` in which no `targetValueType` is specified ?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [September 13, 2019, 8:40pm UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/4 "2019-09-13T20:40:45Z")

</div>

Sure. The comments for that stream constructor are helpful in this case:

> /\*\*  
> \* Read an aggregation from a stream that serializes its targetValueType. This should only be used by subclasses that override  
> \* {@link #serializeTargetValueType(Version)} to return true.  
> \*/

Aggregations like the terms agg accept `ANY` as their values source, which means the actual type isn't known at compile time via a generic like the other aggs (`max` for example). So these aggregations need to serialize/deserialize their targetValueType explicitly from the stream, rather than getting it as a constructor parameter.

I think the only aggs that use `ValuesSourceAggregationBuilder(StreamInput in, ValuesSourceType valuesSourceType) ` are the aggs that specify `ANY` as their VSType. Everything else uses the other constructor.

Out of curiosity, what're you building? 🙂 If it's private/not able to share that's fine, I'm just interested in what aggs community members are making 🙂

---

<div class="post-metadata">

**Author:** ![Deb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deb/32/46162_2.png) [@Deb](https://discuss.elastic.co/u/Deb)\
**Post date:** [September 14, 2019, 8:12am UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/5 "2019-09-14T08:12:47Z")

</div>

Thanks @polyfractal again.

Yes for now it is private. I am trying to write a custom dedup aggregation plugin for our dataset.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [September 16, 2019, 1:07pm UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/6 "2019-09-16T13:07:43Z")

</div>

👍 Sounds interesting! Let me know if you have any more questions 🙂

Just as a side note: the internal API doesn't change too often, but we don't provide any backwards compat guarantees either. So it can be challenging sometimes to maintain a custom plugin since we may accidentally break your code with an internal refactor, etc.

As the plugin API grows (and we remove more instances of Guice injection) we're hoping to have more consistent BWC guarantees, but it's not as rigorous as say the REST API.

---

<div class="post-metadata">

**Author:** ![Deb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deb/32/46162_2.png) [@Deb](https://discuss.elastic.co/u/Deb)\
**Post date:** [September 17, 2019, 3:29am UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/7 "2019-09-17T03:29:07Z")

</div>

Yeah sure @polyfractal ... I will try to reach you when I have more doubts. Thanks again 🙂

---

<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:** [October 15, 2019, 3:29am UTC](https://discuss.elastic.co/t/valuessourcetype-in-valuessourceaggregationbuilder-constructors/199208/8 "2019-10-15T03:29:07Z")

</div>

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