# Detection of object arrays

**URL:** <https://discuss.elastic.co/t/detection-of-object-arrays/13239>\
**Category:** Elasticsearch\
**Created:** [August 16, 2013, 10:06am UTC](https://discuss.elastic.co/t/detection-of-object-arrays/13239 "2013-08-16T10:06:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel\_koller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_koller/32/2149_2.png) [@daniel\_koller](https://discuss.elastic.co/u/daniel_koller)\
**Post date:** [August 16, 2013, 10:06am UTC](https://discuss.elastic.co/t/detection-of-object-arrays/13239/1 "2013-08-16T10:06:55Z")

</div>

Hi all,

did not found anything about this topic  
it seems to me, that arrays of objects are not detected automatically:

(java insert)  
startObject()  
.startObject("someThing")  
.field("id", 42)  
.field("fieldName", "fieldValue")  
.endObject()  
.startObject("someThing")  
.field("id", 43)  
.field("fieldName", "other fieldValue")  
.endObject()  
.endObject()

results in something like:

{  
"someThing":{  
"id":42,  
"fieldName":"fieldValue"  
},  
"someThing":{  
"id":43,  
"fieldName":"other fieldValue"  
}  
}

i am expecting something like

{  
"someThing":[  
{  
"id":42,  
"fieldName":"fieldValue"  
},  
{  
"id":43,  
"fieldName":"other fieldValue"  
}  
]  
}

for me it is not possible to insert something in this way (we do not have  
the all the "someThing"s at one time):  
startArray("someThing")  
.startObject()  
.field("id", 42)  
.field("fieldName", "fieldValue")  
.endObject()  
.startObject()  
.field("id", 43)  
.field("fieldName", "other fieldValue")  
.endObject()  
.endArray()

searching works also for e.g. "other fieldValue" but when I parse the  
source of the searchHit (java again), i get not a list for "someThing",  
only a normal Map is found.

Is there some error in my considerations/coding or is this a bug?

Best regards,  
Daniel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [August 16, 2013, 11:16am UTC](https://discuss.elastic.co/t/detection-of-object-arrays/13239/2 "2013-08-16T11:16:57Z")

</div>

In the Elasticsearch object model, which maps to Lucene fields, the two  
alternatives are equivalent. Although not correct JSON because two  
object names occur, ES accepts double JSON keys while parsing.

Jörg

Am 16.08.2013 12:06, schrieb daniel koller:

> Hi all,
> 
> did not found anything about this topic  
> it seems to me, that arrays of objects are not detected automatically:
> 
> (java insert)  
> |  
> startObject()  
> .startObject("someThing")  
> .field("id",42)  
> .field("fieldName","fieldValue")  
> .endObject()  
> .startObject("someThing")  
> .field("id",43)  
> .field("fieldName","other fieldValue")  
> .endObject()  
> .endObject()  
> |
> 
> results in something like:
> 
> |  
> {  
> "someThing":{  
> "id":42,  
> "fieldName":"fieldValue"  
> },  
> "someThing":{  
> "id":43,  
> "fieldName":"other fieldValue"  
> }  
> }  
> |
> 
> i am expecting something like
> 
> |  
> {  
> "someThing":[  
> {  
> "id":42,  
> "fieldName":"fieldValue"  
> },  
> {  
> "id":43,  
> "fieldName":"other fieldValue"  
> }  
> ]  
> }  
> |
> 
> for me it is not possible to insert something in this way (we do not  
> have the all the "someThing"s at one time):  
> |  
> startArray("someThing")  
> .startObject()  
> .field("id",42)  
> .field("fieldName","fieldValue")  
> .endObject()  
> .startObject()  
> .field("id",43)  
> .field("fieldName","other fieldValue")  
> .endObject()  
> .endArray()  
> |
> 
> searching works also for e.g. "other fieldValue" but when I parse the  
> source of the searchHit (java again), i get not a list for  
> "someThing", only a normal Map is found.
> 
> Is there some error in my considerations/coding or is this a bug?
> 
> ## Best regards, Daniel
> 
> You received this message because you are subscribed to the Google  
> Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send  
> an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![daniel\_koller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_koller/32/2149_2.png) [@daniel\_koller](https://discuss.elastic.co/u/daniel_koller)\
**Post date:** [August 16, 2013, 11:34am UTC](https://discuss.elastic.co/t/detection-of-object-arrays/13239/3 "2013-08-16T11:34:20Z")

</div>

Hi Jörg,

since the first object can not be retrieved from a searchHit, it might be  
better to build an array when adding the second object instead of adding a  
second object with the same name.  
or collection all object from with the same name and adding them to the  
source Map of the search hit (since the first object is present in the  
searchResponse but not in the searchHit source).

Maybe I am just too stupid to find it, but is there a way to add multiple  
object to an array after adding the single object to a XContentBuilder?  
For only one single object, this can be done with  
xContentBuilder.rawField(String name, ByteReference byteReference).

I thought of something like:

builder.startArray("someThing");  
for(ByteReference ref: references){  
builder.raw(ref); //not existing  
}  
builder.endArray()

where the references Iterable contains the bytes which where build and  
collected before.

Best regards,  
Daniel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [August 16, 2013, 1:33pm UTC](https://discuss.elastic.co/t/detection-of-object-arrays/13239/4 "2013-08-16T13:33:03Z")

</div>

I agree, this is one thing I also miss in the XContentBuilder API.

Maybe XContentBuilder can be improved, so for example, it could also  
understand the nuances of JSONML mixed object/value arrays.

Jörg

Am 16.08.2013 13:34, schrieb daniel koller:

> Maybe I am just too stupid to find it, but is there a way to add  
> multiple object to an array after adding the single object to a  
> XContentBuilder?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![daniel\_koller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_koller/32/2149_2.png) [@daniel\_koller](https://discuss.elastic.co/u/daniel_koller)\
**Post date:** [August 16, 2013, 1:43pm UTC](https://discuss.elastic.co/t/detection-of-object-arrays/13239/5 "2013-08-16T13:43:45Z")

</div>

Hi Jörg,

i worked it out by just passing the "parent"-xContentBuilder arround and  
calling a "startArray"-Function before and an "endArray"-Function after  
adding the array-objects.

Thank you very much,  
best regards from switzerland,  
Daniel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:20am UTC](https://discuss.elastic.co/t/detection-of-object-arrays/13239/6 "2017-07-06T02:20:52Z")

</div>


