Indexing arrays


(IvanBrusic) #1

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.


(Shay Banon) #2

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 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.


(Shay Banon) #3

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.comwrote:

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.comwrote:

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.


(IvanBrusic) #4

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 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.comwrote:

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.comwrote:

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.


(Shay Banon) #5

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 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 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>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
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:

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.


(system) #6