# Returning \_type and \_version in source?

**URL:** <https://discuss.elastic.co/t/returning--type-and--version-in-source/7406>\
**Category:** Elasticsearch\
**Created:** [April 19, 2012, 9:59pm UTC](https://discuss.elastic.co/t/returning--type-and--version-in-source/7406 "2012-04-19T21:59:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Cook\_3](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@James\_Cook\_3](https://discuss.elastic.co/u/James_Cook_3)\
**Post date:** [April 19, 2012, 9:59pm UTC](https://discuss.elastic.co/t/returning--type-and--version-in-source/7406/1 "2012-04-19T21:59:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 19, 2012, 11:43pm UTC](https://discuss.elastic.co/t/returning--type-and--version-in-source/7406/2 "2012-04-19T23:43:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![James\_Cook\_3](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@James\_Cook\_3](https://discuss.elastic.co/u/James_Cook_3)\
**Post date:** [April 20, 2012, 12:18am UTC](https://discuss.elastic.co/t/returning--type-and--version-in-source/7406/3 "2012-04-20T00:18:24Z")

</div>

Thanks, I'll give that a try.

On Thu, Apr 19, 2012 at 7:43 PM, Igor Motov [imotov@gmail.com](mailto: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?

---

<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, 3:31am UTC](https://discuss.elastic.co/t/returning--type-and--version-in-source/7406/4 "2017-07-06T03:31:46Z")

</div>

Closed on July 6, 2017, 3:31am UTC


