# Indexing arrays

**URL:** <https://discuss.elastic.co/t/indexing-arrays/3584>\
**Category:** Elasticsearch\
**Created:** [November 17, 2010, 11:01pm UTC](https://discuss.elastic.co/t/indexing-arrays/3584 "2010-11-17T23:01:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanBrusic](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IvanBrusic](https://discuss.elastic.co/u/IvanBrusic)\
**Post date:** [November 17, 2010, 11:01pm UTC](https://discuss.elastic.co/t/indexing-arrays/3584/1 "2010-11-17T23:01:54Z")

</div>

It seems like I am getting stuck on the simple tasks. How can I index  
an array via the Java API?

Some simple Java code:

val builder = jsonBuilder.startObject  
...  
ArrayList[String] list = new java.util.ArrayListString  
list.add(.....)  
builder.field("somefield", list)

returns:  
java.io.IOException: Type not allowed [class java.util.ArrayList]  
at  
org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:  
607)  
at  
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:  
406)

builder.field("somefield", list.toArray())  
=\> java.io.IOException: Type not allowed [class [Ljava.lang.Object;]

builder.field("somefield", list.toArray(new Array[String]  
(list.size())))  
=\> java.io.IOException: Type not allowed [class [Ljava.lang.String;]

Not sure why my JVM is not calling "public XContentBuilder  
field(String name, Object value) throws IOException" (line 493) like  
my IDE thinks it should.

---

<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 17, 2010, 11:44pm UTC](https://discuss.elastic.co/t/indexing-arrays/3584/2 "2010-11-17T23:44:40Z")

</div>

The basic construct the create an array is something list this:

builder.startObject().startArray("field\_name").value("1").value("2").endArray().endObject()

There are helper methods to simplify it, specifically: builder#field(String,  
Object...), and builder#array(String, String...) which uses the above API.

Let me add the same for Iterable, should help with Collections.

-shay.banon

On Thu, Nov 18, 2010 at 1:01 AM, Ivan Brusic [ivan\_brusic@yahoo.com](mailto:ivan_brusic@yahoo.com) wrote:

> It seems like I am getting stuck on the simple tasks. How can I index  
> an array via the Java API?
> 
> Some simple Java code:
> 
> val builder = jsonBuilder.startObject  
> ...  
> ArrayList[String] list = new java.util.ArrayListString  
> list.add(.....)  
> builder.field("somefield", list)
> 
> returns:  
> java.io.IOException: Type not allowed [class java.util.ArrayList]  
> at
> 
> org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:  
> 607)  
> at
> 
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:  
> 406)
> 
> builder.field("somefield", list.toArray())  
> =\> java.io.IOException: Type not allowed [class [Ljava.lang.Object;]
> 
> builder.field("somefield", list.toArray(new Array[String]  
> (list.size())))  
> =\> java.io.IOException: Type not allowed [class [Ljava.lang.String;]
> 
> Not sure why my JVM is not calling "public XContentBuilder  
> field(String name, Object value) throws IOException" (line 493) like  
> my IDE thinks it should.

---

<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 17, 2010, 11:52pm UTC](https://discuss.elastic.co/t/indexing-arrays/3584/3 "2010-11-17T23:52:44Z")

</div>

Sorry, thats strange, there is a method for field(String fieldName, List  
values) ....

On Thu, Nov 18, 2010 at 1:44 AM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> The basic construct the create an array is something list this:
> 
> builder.startObject().startArray("field\_name").value("1").value("2").endArray().endObject()
> 
> There are helper methods to simplify it, specifically:  
> builder#field(String, Object...), and builder#array(String, String...) which  
> uses the above API.
> 
> Let me add the same for Iterable, should help with Collections.
> 
> -shay.banon
> 
> On Thu, Nov 18, 2010 at 1:01 AM, Ivan Brusic [ivan\_brusic@yahoo.com](mailto:ivan_brusic@yahoo.com)wrote:
> 
> > It seems like I am getting stuck on the simple tasks. How can I index  
> > an array via the Java API?
> > 
> > Some simple Java code:
> > 
> > val builder = jsonBuilder.startObject  
> > ...  
> > ArrayList[String] list = new java.util.ArrayListString  
> > list.add(.....)  
> > builder.field("somefield", list)
> > 
> > returns:  
> > java.io.IOException: Type not allowed [class java.util.ArrayList]  
> > at
> > 
> > org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:  
> > 607)  
> > at
> > 
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:  
> > 406)
> > 
> > builder.field("somefield", list.toArray())  
> > =\> java.io.IOException: Type not allowed [class [Ljava.lang.Object;]
> > 
> > builder.field("somefield", list.toArray(new Array[String]  
> > (list.size())))  
> > =\> java.io.IOException: Type not allowed [class [Ljava.lang.String;]
> > 
> > Not sure why my JVM is not calling "public XContentBuilder  
> > field(String name, Object value) throws IOException" (line 493) like  
> > my IDE thinks it should.

---

<div class="post-metadata">

**Author:** ![IvanBrusic](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IvanBrusic](https://discuss.elastic.co/u/IvanBrusic)\
**Post date:** [November 18, 2010, 12:18am UTC](https://discuss.elastic.co/t/indexing-arrays/3584/4 "2010-11-18T00:18:06Z")

</div>

That was the point that I made at the bottom of my post.

The method:  
public XContentBuilder field(XContentBuilderString name, List  
value)  
should be getting called

Instead the method:  
public XContentBuilder field(String name, Object... value)  
is getting called, where the value of "value" is something like a  
vararg of one. Bizarre.

I am using scala, so perhaps the byte code that is generated is out-of-  
sync. Would have seen problems long ago. Perhaps I have never called  
an overloaded method that accepts either a List or a vararg of  
Object. Will ask a Scala list about it.

But as always, the solution was under my nose. I completely glossed  
over "startArray". I was focusing on field(...) and other possible  
builders. startArray works like a charm.

Thanks again,

Ivan

On Nov 17, 6:52 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Sorry, thats strange, there is a method for field(String fieldName, List  
> values) ....
> 
> On Thu, Nov 18, 2010 at 1:44 AM, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)wrote:
> 
> > The basic construct the create an array is something list this:
> 
> > builder.startObject().startArray("field\_name").value("1").value("2").endArr ay().endObject()
> 
> > There are helper methods to simplify it, specifically:  
> > builder#field(String, Object...), and builder#array(String, String...) which  
> > uses the above API.
> 
> > Let me add the same for Iterable, should help with Collections.
> 
> > -shay.banon
> 
> > On Thu, Nov 18, 2010 at 1:01 AM, Ivan Brusic [ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com)wrote:
> 
> > > It seems like I am getting stuck on the simple tasks. How can I index  
> > > an array via the Java API?
> 
> > > Some simple Java code:
> 
> > > val builder = jsonBuilder.startObject  
> > > ...  
> > > ArrayList[String] list = new java.util.ArrayListString  
> > > list.add(.....)  
> > > builder.field("somefield", list)
> 
> > > returns:  
> > > java.io.IOException: Type not allowed [class java.util.ArrayList]  
> > > at
> 
> > > org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.jav a:  
> > > 607)  
> > > at
> 
> > > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.jav a:  
> > > 406)
> 
> > > builder.field("somefield", list.toArray())  
> > > =\> java.io.IOException: Type not allowed [class [Ljava.lang.Object;]
> 
> > > builder.field("somefield", list.toArray(new Array[String]  
> > > (list.size())))  
> > > =\> java.io.IOException: Type not allowed [class [Ljava.lang.String;]
> 
> > > Not sure why my JVM is not calling "public XContentBuilder  
> > > field(String name, Object value) throws IOException" (line 493) like  
> > > my IDE thinks it should.

---

<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 18, 2010, 12:26am UTC](https://discuss.elastic.co/t/indexing-arrays/3584/5 "2010-11-18T00:26:43Z")

</div>

Yea, varargs are a pain and there used to be bugs in earlier JVM versions in  
handling it correctly... . Not sure how scala does it... .

Just added a test for it, which seems to work fine (in java):

```
@Test public void testOverloadedList() throws Exception {
    XContentBuilder builder =

```

XContentFactory.contentBuilder(XContentType.JSON);  
builder.startObject().field("test", Lists.newArrayList("1",  
"2")).endObject();  
assertThat(builder.string(), equalTo("{"test":["1","2"]}"));  
}

As as a side note, would love for a Scala client built on top of the Java  
client with a more scala feel for it.

-shay.banon

On Thu, Nov 18, 2010 at 2:18 AM, Ivan Brusic [ivan\_brusic@yahoo.com](mailto:ivan_brusic@yahoo.com) wrote:

> That was the point that I made at the bottom of my post.
> 
> The method:  
> public XContentBuilder field(XContentBuilderString name, List  
> value)  
> should be getting called
> 
> Instead the method:  
> public XContentBuilder field(String name, Object... value)  
> is getting called, where the value of "value" is something like a  
> vararg of one. Bizarre.
> 
> I am using scala, so perhaps the byte code that is generated is out-of-  
> sync. Would have seen problems long ago. Perhaps I have never called  
> an overloaded method that accepts either a List or a vararg of  
> Object. Will ask a Scala list about it.
> 
> But as always, the solution was under my nose. I completely glossed  
> over "startArray". I was focusing on field(...) and other possible  
> builders. startArray works like a charm.
> 
> Thanks again,
> 
> Ivan
> 
> On Nov 17, 6:52 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Sorry, thats strange, there is a method for field(String fieldName, List  
> > values) ....
> > 
> > On Thu, Nov 18, 2010 at 1:44 AM, Shay Banon \<  
> > [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)\>wrote:
> > 
> > > The basic construct the create an array is something list this:
> > 
> > >
> 
> builder.startObject().startArray("field\_name").value("1").value("2").endArr  
> ay().endObject()
> 
> > > There are helper methods to simplify it, specifically:  
> > > builder#field(String, Object...), and builder#array(String, String...)  
> > > which  
> > > uses the above API.
> > 
> > > Let me add the same for Iterable, should help with Collections.
> > 
> > > -shay.banon
> > 
> > > On Thu, Nov 18, 2010 at 1:01 AM, Ivan Brusic \<[ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com)  
> > > wrote:
> > 
> > > > It seems like I am getting stuck on the simple tasks. How can I index  
> > > > an array via the Java API?
> > 
> > > > Some simple Java code:
> > 
> > > > val builder = jsonBuilder.startObject  
> > > > ...  
> > > > ArrayList[String] list = new java.util.ArrayListString  
> > > > list.add(.....)  
> > > > builder.field("somefield", list)
> > 
> > > > returns:  
> > > > java.io.IOException: Type not allowed [class java.util.ArrayList]  
> > > > at
> > 
> > > >
> 
> org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.jav  
> a:
> 
> > > > 1. at
> > 
> > > >
> 
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.jav  
> a:
> 
> > > > 1.
> > 
> > > > builder.field("somefield", list.toArray())  
> > > > =\> java.io.IOException: Type not allowed [class [Ljava.lang.Object;]
> > 
> > > > builder.field("somefield", list.toArray(new Array[String]  
> > > > (list.size())))  
> > > > =\> java.io.IOException: Type not allowed [class [Ljava.lang.String;]
> > 
> > > > Not sure why my JVM is not calling "public XContentBuilder  
> > > > field(String name, Object value) throws IOException" (line 493) like  
> > > > my IDE thinks it should.

---

<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, 4:16am UTC](https://discuss.elastic.co/t/indexing-arrays/3584/6 "2017-07-06T04:16:20Z")

</div>


