Support for indexing all Java collections

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Support for indexing all Java collections

Ivan Brusic
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
Reply | Threaded
Open this post in threaded view
|

Re: Support for indexing all Java collections

kimchy
Administrator
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 <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Support for indexing all Java collections

Ivan Brusic
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

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

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 <[hidden email]> 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 <[hidden email]> 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
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Support for indexing all Java collections

kimchy
Administrator
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 <[hidden email]> 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

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

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 <[hidden email]> 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 <[hidden email]> 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
>
>