# Support for indexing all Java collections

**URL:** <https://discuss.elastic.co/t/support-for-indexing-all-java-collections/6017>\
**Category:** Elasticsearch\
**Created:** [November 30, 2011, 12:10am UTC](https://discuss.elastic.co/t/support-for-indexing-all-java-collections/6017 "2011-11-30T00:10:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [November 30, 2011, 12:10am UTC](https://discuss.elastic.co/t/support-for-indexing-all-java-collections/6017/1 "2011-11-30T00:10:51Z")

</div>

Started using ElasticSearch on a new (to me) project and came across a  
small issue while indexing sets.

Set set = ...  
builder.field("fieldName", set);

The code will call the overloaded method XContentBuilder#field(String  
name, Object value), since there is no method for the more restrictive  
type Set. There is an overloaded method for  
XContentBuilder#field(String name, List value), so the solution is to  
simply create a list out of the set. However, looking at the latter  
method, there should be no reason why the type should not be  
Collection and not just List.

Ivan

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 30, 2011, 11:09am UTC](https://discuss.elastic.co/t/support-for-indexing-all-java-collections/6017/2 "2011-11-30T11:09:04Z")

</div>

The aim is not to create a full object to json mapping in elasticsearch,  
use jackson for that.

On Wed, Nov 30, 2011 at 2:10 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Started using Elasticsearch on a new (to me) project and came across a  
> small issue while indexing sets.
> 
> Set set = ...  
> builder.field("fieldName", set);
> 
> The code will call the overloaded method XContentBuilder#field(String  
> name, Object value), since there is no method for the more restrictive  
> type Set. There is an overloaded method for  
> XContentBuilder#field(String name, List value), so the solution is to  
> simply create a list out of the set. However, looking at the latter  
> method, there should be no reason why the type should not be  
> Collection and not just List.
> 
> Ivan

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [November 30, 2011, 7:57pm UTC](https://discuss.elastic.co/t/support-for-indexing-all-java-collections/6017/3 "2011-11-30T19:57:21Z")

</div>

The issue has nothing to do with JSON. Indexing any Java collection is  
easy with a change of the method:  
[https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L498](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L498)

Replace List with Collection and you should now be able to index items  
such as Sets and even Vectors. Currently, a Set uses the Object level  
overloaded method:

[https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616)

Since the if-elsif statements does not find set, it simply gets  
converted with toString, but the List method would work perfectly.

The issue is very minor. The workaround is simple, but it would be  
nice to have Collection level support and not just List.

Cheers,

Ivan

On Wed, Nov 30, 2011 at 3:09 AM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> The aim is not to create a full object to json mapping in elasticsearch, use  
> jackson for that.
> 
> On Wed, Nov 30, 2011 at 2:10 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> 
> > Started using Elasticsearch on a new (to me) project and came across a  
> > small issue while indexing sets.
> > 
> > Set set = ...  
> > builder.field("fieldName", set);
> > 
> > The code will call the overloaded method XContentBuilder#field(String  
> > name, Object value), since there is no method for the more restrictive  
> > type Set. There is an overloaded method for  
> > XContentBuilder#field(String name, List value), so the solution is to  
> > simply create a list out of the set. However, looking at the latter  
> > method, there should be no reason why the type should not be  
> > Collection and not just List.
> > 
> > Ivan

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [December 1, 2011, 2:04pm UTC](https://discuss.elastic.co/t/support-for-indexing-all-java-collections/6017/4 "2011-12-01T14:04:26Z")

</div>

It can be fixed (open an issue), but, it is not designed to be a  
replacement to a fully fledged object to json mapper.

On Wed, Nov 30, 2011 at 9:57 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> The issue has nothing to do with JSON. Indexing any Java collection is  
> easy with a change of the method:
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L498](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L498)
> 
> Replace List with Collection and you should now be able to index items  
> such as Sets and even Vectors. Currently, a Set uses the Object level  
> overloaded method:
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616)
> 
> Since the if-elsif statements does not find set, it simply gets  
> converted with toString, but the List method would work perfectly.
> 
> The issue is very minor. The workaround is simple, but it would be  
> nice to have Collection level support and not just List.
> 
> Cheers,
> 
> Ivan
> 
> On Wed, Nov 30, 2011 at 3:09 AM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:
> 
> > The aim is not to create a full object to json mapping in elasticsearch,  
> > use  
> > jackson for that.
> > 
> > On Wed, Nov 30, 2011 at 2:10 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> > 
> > > Started using Elasticsearch on a new (to me) project and came across a  
> > > small issue while indexing sets.
> > > 
> > > Set set = ...  
> > > builder.field("fieldName", set);
> > > 
> > > The code will call the overloaded method XContentBuilder#field(String  
> > > name, Object value), since there is no method for the more restrictive  
> > > type Set. There is an overloaded method for  
> > > XContentBuilder#field(String name, List value), so the solution is to  
> > > simply create a list out of the set. However, looking at the latter  
> > > method, there should be no reason why the type should not be  
> > > Collection and not just List.
> > > 
> > > Ivan

---

<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:** [July 6, 2017, 3:46am UTC](https://discuss.elastic.co/t/support-for-indexing-all-java-collections/6017/5 "2017-07-06T03:46:46Z")

</div>


