# How to extract the value of complex fields in script field specific search using the java api

**URL:** <https://discuss.elastic.co/t/how-to-extract-the-value-of-complex-fields-in-script-field-specific-search-using-the-java-api/3523>\
**Category:** Elasticsearch\
**Created:** [November 4, 2010, 8:34pm UTC](https://discuss.elastic.co/t/how-to-extract-the-value-of-complex-fields-in-script-field-specific-search-using-the-java-api/3523 "2010-11-04T20:34:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diptamay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diptamay/32/2805_2.png) [@Diptamay](https://discuss.elastic.co/u/Diptamay)\
**Post date:** [November 4, 2010, 8:34pm UTC](https://discuss.elastic.co/t/how-to-extract-the-value-of-complex-fields-in-script-field-specific-search-using-the-java-api/3523/1 "2010-11-04T20:34:14Z")

</div>

Hi

I have sample documents like this at [https://gist.github.com/663112](https://gist.github.com/663112),  
and I am running a setting up specific fields to fetch using script  
field like this for e.g. (through the java api)  
{  
"query" :{  
"bool" : {  
"should" : {"term" : {"\_id" : 10990239 }},  
"minimum\_number\_should\_match" : 1  
}  
},  
"script\_fields" : {  
"\_id" : {  
"script" : "\_source.\_id"  
},  
"tags" : {  
"script" : "\_source.tags"  
}  
},  
"size" : 5  
}

Now after I perform the search I need to build my domain objects from  
the returned data. I am doing something like this

for ( Map.Entry\<String, SearchHitField\> entry :  
hit.getFields().entrySet() ) {  
String fieldName = entry.getKey();  
if ( fieldName.equals("tags") ) {  
//do something with tags  
LOG.debug("TAG VALUE = " +  
entry.getValue().getValue());  
} else {  
//do something  
}  
}

Now the problem is entry.getValue().getValue() returns an Object, and  
I have no way of manipulating that data. If I do a toString() on the  
object value, I get the toString() version as

{  
subject=[  
{  
value=MLBCOM\_FEATURE,  
displayName=MLBCOM\_FEATURE  
}  
],  
player\_id=[  
{  
value=121347,  
displayName=Alex Rodriguez  
}  
],  
team\_id=[  
{  
value=147,  
displayName=New York Yankees  
}  
]  
}

This is not a valid json response either, so I can no way parse this  
using a json parser.

Is there anyway, I can get the actual json as string or byte[] from  
entry.getValue().getValue(), so that I can parse it?

Thanks  
Diptamay

---

<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 4, 2010, 9:58pm UTC](https://discuss.elastic.co/t/how-to-extract-the-value-of-complex-fields-in-script-field-specific-search-using-the-java-api/3523/2 "2010-11-04T21:58:28Z")

</div>

What you get back (in Java), is the "Map of Maps" representation of the json  
(if you know jackson, then its the result of parsing the json into a map).  
Its similar result as when calling sourceAsMap on a hit. You can use the  
XContentFactory to get a jsonBuilder and write that map to get the json  
representation of it.

-shay.banon

On Thu, Nov 4, 2010 at 10:34 PM, diptamay [diptamay@gmail.com](mailto:diptamay@gmail.com) wrote:

> Hi
> 
> I have sample documents like this at [mock json · GitHub](https://gist.github.com/663112),  
> and I am running a setting up specific fields to fetch using script  
> field like this for e.g. (through the java api)  
> {  
> "query" :{  
> "bool" : {  
> "should" : {"term" : {"\_id" : 10990239 }},  
> "minimum\_number\_should\_match" : 1  
> }  
> },  
> "script\_fields" : {  
> "\_id" : {  
> "script" : "\_source.\_id"  
> },  
> "tags" : {  
> "script" : "\_source.tags"  
> }  
> },  
> "size" : 5  
> }
> 
> Now after I perform the search I need to build my domain objects from  
> the returned data. I am doing something like this
> 
> for ( Map.Entry\<String, SearchHitField\> entry :  
> hit.getFields().entrySet() ) {  
> String fieldName = entry.getKey();  
> if ( fieldName.equals("tags") ) {  
> //do something with tags  
> LOG.debug("TAG VALUE = " +  
> entry.getValue().getValue());  
> } else {  
> //do something  
> }  
> }
> 
> Now the problem is entry.getValue().getValue() returns an Object, and  
> I have no way of manipulating that data. If I do a toString() on the  
> object value, I get the toString() version as
> 
> {  
> subject=[  
> {  
> value=MLBCOM\_FEATURE,  
> displayName=MLBCOM\_FEATURE  
> }  
> ],  
> player\_id=[  
> {  
> value=121347,  
> displayName=Alex Rodriguez  
> }  
> ],  
> team\_id=[  
> {  
> value=147,  
> displayName=New York Yankees  
> }  
> ]  
> }
> 
> This is not a valid json response either, so I can no way parse this  
> using a json parser.
> 
> Is there anyway, I can get the actual json as string or byte from  
> entry.getValue().getValue(), so that I can parse it?
> 
> Thanks  
> Diptamay

---

<div class="post-metadata">

**Author:** ![Diptamay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diptamay/32/2805_2.png) [@Diptamay](https://discuss.elastic.co/u/Diptamay)\
**Post date:** [November 4, 2010, 10:38pm UTC](https://discuss.elastic.co/t/how-to-extract-the-value-of-complex-fields-in-script-field-specific-search-using-the-java-api/3523/3 "2010-11-04T22:38:18Z")

</div>

Thanks for the pointer.

Did this,

```
    XContentBuilder builder = jsonBuilder();
    builder.startObject();
    for ( Map.Entry<String, SearchHitField> entry :

```

hit.getFields().entrySet() ) {  
builder.field(entry.getKey(),  
entry.getValue().getValue());  
}  
builder.endObject();

Since, the map of maps accepts only Map\<String,Object\> while the  
fields are Map\<String,SearchHitField\>.

-Diptamay

On Nov 4, 5:58 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> What you get back (in Java), is the "Map of Maps" representation of the json  
> (if you know jackson, then its the result of parsing the json into a map).  
> Its similar result as when calling sourceAsMap on a hit. You can use the  
> XContentFactory to get a jsonBuilder and write that map to get the json  
> representation of it.
> 
> -shay.banon
> 
> On Thu, Nov 4, 2010 at 10:34 PM, diptamay [dipta...@gmail.com](mailto:dipta...@gmail.com) wrote:
> 
> > Hi
> 
> > I have sample documents like this athttps://gist.github.com/663112,  
> > and I am running a setting up specific fields to fetch using script  
> > field like this for e.g. (through the java api)  
> > {  
> > "query" :{  
> > "bool" : {  
> > "should" : {"term" : {"\_id" : 10990239 }},  
> > "minimum\_number\_should\_match" : 1  
> > }  
> > },  
> > "script\_fields" : {  
> > "\_id" : {  
> > "script" : "\_source.\_id"  
> > },  
> > "tags" : {  
> > "script" : "\_source.tags"  
> > }  
> > },  
> > "size" : 5  
> > }
> 
> > Now after I perform the search I need to build my domain objects from  
> > the returned data. I am doing something like this
> 
> > for ( Map.Entry\<String, SearchHitField\> entry :  
> > hit.getFields().entrySet() ) {  
> > String fieldName = entry.getKey();  
> > if ( fieldName.equals("tags") ) {  
> > //do something with tags  
> > LOG.debug("TAG VALUE = " +  
> > entry.getValue().getValue());  
> > } else {  
> > //do something  
> > }  
> > }
> 
> > Now the problem is entry.getValue().getValue() returns an Object, and  
> > I have no way of manipulating that data. If I do a toString() on the  
> > object value, I get the toString() version as
> 
> > {  
> > subject=[  
> > {  
> > value=MLBCOM\_FEATURE,  
> > displayName=MLBCOM\_FEATURE  
> > }  
> > ],  
> > player\_id=[  
> > {  
> > value=121347,  
> > displayName=Alex Rodriguez  
> > }  
> > ],  
> > team\_id=[  
> > {  
> > value=147,  
> > displayName=New York Yankees  
> > }  
> > ]  
> > }
> 
> > This is not a valid json response either, so I can no way parse this  
> > using a json parser.
> 
> > Is there anyway, I can get the actual json as string or byte from  
> > entry.getValue().getValue(), so that I can parse it?
> 
> > Thanks  
> > Diptamay

---

<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 4, 2010, 11:07pm UTC](https://discuss.elastic.co/t/how-to-extract-the-value-of-complex-fields-in-script-field-specific-search-using-the-java-api/3523/4 "2010-11-04T23:07:25Z")

</div>

Yes, just make sure that this is the format of the json you want (you create  
a json for all hits).

-shay.banon

On Fri, Nov 5, 2010 at 12:38 AM, diptamay [diptamay@gmail.com](mailto:diptamay@gmail.com) wrote:

> Thanks for the pointer.
> 
> Did this,
> 
> ```
> XContentBuilder builder = jsonBuilder();
> builder.startObject();
> for ( Map.Entry<String, SearchHitField> entry :
> 
> ```
> 
> hit.getFields().entrySet() ) {  
> builder.field(entry.getKey(),  
> entry.getValue().getValue());  
> }  
> builder.endObject();
> 
> Since, the map of maps accepts only Map\<String,Object\> while the  
> fields are Map\<String,SearchHitField\>.
> 
> -Diptamay
> 
> On Nov 4, 5:58 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > What you get back (in Java), is the "Map of Maps" representation of the  
> > json  
> > (if you know jackson, then its the result of parsing the json into a  
> > map).  
> > Its similar result as when calling sourceAsMap on a hit. You can use the  
> > XContentFactory to get a jsonBuilder and write that map to get the json  
> > representation of it.
> > 
> > -shay.banon
> > 
> > On Thu, Nov 4, 2010 at 10:34 PM, diptamay [dipta...@gmail.com](mailto:dipta...@gmail.com) wrote:
> > 
> > > Hi
> > 
> > > I have sample documents like this athttps://gist.github.com/663112,  
> > > and I am running a setting up specific fields to fetch using script  
> > > field like this for e.g. (through the java api)  
> > > {  
> > > "query" :{  
> > > "bool" : {  
> > > "should" : {"term" : {"\_id" : 10990239 }},  
> > > "minimum\_number\_should\_match" : 1  
> > > }  
> > > },  
> > > "script\_fields" : {  
> > > "\_id" : {  
> > > "script" : "\_source.\_id"  
> > > },  
> > > "tags" : {  
> > > "script" : "\_source.tags"  
> > > }  
> > > },  
> > > "size" : 5  
> > > }
> > 
> > > Now after I perform the search I need to build my domain objects from  
> > > the returned data. I am doing something like this
> > 
> > > for ( Map.Entry\<String, SearchHitField\> entry :  
> > > hit.getFields().entrySet() ) {  
> > > String fieldName = entry.getKey();  
> > > if ( fieldName.equals("tags") ) {  
> > > //do something with tags  
> > > LOG.debug("TAG VALUE = " +  
> > > entry.getValue().getValue());  
> > > } else {  
> > > //do something  
> > > }  
> > > }
> > 
> > > Now the problem is entry.getValue().getValue() returns an Object, and  
> > > I have no way of manipulating that data. If I do a toString() on the  
> > > object value, I get the toString() version as
> > 
> > > {  
> > > subject=[  
> > > {  
> > > value=MLBCOM\_FEATURE,  
> > > displayName=MLBCOM\_FEATURE  
> > > }  
> > > ],  
> > > player\_id=[  
> > > {  
> > > value=121347,  
> > > displayName=Alex Rodriguez  
> > > }  
> > > ],  
> > > team\_id=[  
> > > {  
> > > value=147,  
> > > displayName=New York Yankees  
> > > }  
> > > ]  
> > > }
> > 
> > > This is not a valid json response either, so I can no way parse this  
> > > using a json parser.
> > 
> > > Is there anyway, I can get the actual json as string or byte from  
> > > entry.getValue().getValue(), so that I can parse it?
> > 
> > > Thanks  
> > > Diptamay

---

<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:17am UTC](https://discuss.elastic.co/t/how-to-extract-the-value-of-complex-fields-in-script-field-specific-search-using-the-java-api/3523/5 "2017-07-06T04:17:07Z")

</div>


