Returning _type and _version in source?


(James Cook-3) #1

I am using the Java API and get the result of a search:

final GetResponse response =
_esClient.prepareGet(_index, _type, (String) key)
.execute().actionGet();

Now, I can get the source as a Map or as a String using:

    Map<String, Object> result = response.getSource();
    String result = response.sourceAsString();

But, I want to "inject" some additional response properties into my source
(i.e. _type, _version, _index) which I will return as a String. Getting the
Map back then injecting the properties is trivial, but there isn't a
convenient way (that I could find) to convert the map back to a JSON
string.

Is there a config property to inject these additional fields into source
already? Or some technique to accomplish what I would like?


(Igor Motov) #2

XContentFactory.jsonBuilder().map(responseAsMap).string()

On Thursday, April 19, 2012 5:59:00 PM UTC-4, James Cook wrote:

I am using the Java API and get the result of a search:

final GetResponse response =
_esClient.prepareGet(_index, _type, (String) key)
.execute().actionGet();

Now, I can get the source as a Map or as a String using:

    Map<String, Object> result = response.getSource();
    String result = response.sourceAsString();

But, I want to "inject" some additional response properties into my source
(i.e. _type, _version, _index) which I will return as a String. Getting the
Map back then injecting the properties is trivial, but there isn't a
convenient way (that I could find) to convert the map back to a JSON
string.

Is there a config property to inject these additional fields into source
already? Or some technique to accomplish what I would like?


(James Cook-3) #3

Thanks, I'll give that a try.

On Thu, Apr 19, 2012 at 7:43 PM, Igor Motov imotov@gmail.com wrote:

XContentFactory.jsonBuilder().map(responseAsMap).string()

On Thursday, April 19, 2012 5:59:00 PM UTC-4, James Cook wrote:

I am using the Java API and get the result of a search:

final GetResponse response =
_esClient.prepareGet(_index, _type, (String) key)
.execute().actionGet();

Now, I can get the source as a Map or as a String using:

    Map<String, Object> result = response.getSource();
    String result = response.sourceAsString();

But, I want to "inject" some additional response properties into my
source (i.e. _type, _version, _index) which I will return as a String.
Getting the Map back then injecting the properties is trivial, but there
isn't a convenient way (that I could find) to convert the map back to a
JSON string.

Is there a config property to inject these additional fields into source
already? Or some technique to accomplish what I would like?


(system) #4