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

Hi

I have sample documents like this at 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

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

Hi

I have sample documents like this at mock json · GitHub,
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

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

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