# Response mapping

**URL:** <https://discuss.elastic.co/t/response-mapping/11089>\
**Category:** Elasticsearch\
**Created:** [March 10, 2013, 9:01pm UTC](https://discuss.elastic.co/t/response-mapping/11089 "2013-03-10T21:01:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mihai\_Cazacu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihai_cazacu/32/2366_2.png) [@Mihai\_Cazacu](https://discuss.elastic.co/u/Mihai_Cazacu)\
**Post date:** [March 10, 2013, 9:01pm UTC](https://discuss.elastic.co/t/response-mapping/11089/1 "2013-03-10T21:01:55Z")

</div>

Hi,

Is there an automatic way to map a search result like [http://www.elasticsearch.org/guide/reference/api/search/fields.html](http://www.elasticsearch.org/guide/reference/api/search/fields.html) to the correspondig object type?  
Let's say that I have stored a "Person" object with firstName, latName and age and I make a query for firstName and age fields. How can I map _automatically_ the response to the Person?

Thanks,  
Mihai

--  
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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 10, 2013, 9:25pm UTC](https://discuss.elastic.co/t/response-mapping/11089/2 "2013-03-10T21:25:41Z")

</div>

I suppose that you are talking about Java Beans, aren't you?  
You have to do it yourself using the same _tools_ you used when indexing.  
For example, if you use Jackson use it when deserializing.

Have a look here: [https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L268](https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L268)

## HTH

David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 10 mars 2013 à 22:01, Mihai Cazacu [cazacugmihai@gmail.com](mailto:cazacugmihai@gmail.com) a écrit :

Hi,

Is there an automatic way to map a search result like [http://www.elasticsearch.org/guide/reference/api/search/fields.html](http://www.elasticsearch.org/guide/reference/api/search/fields.html) to the correspondig object type?  
Let's say that I have stored a "Person" object with firstName, latName and age and I make a query for firstName and age fields. How can I map _automatically_ the response to the Person?

Thanks,  
Mihai

--  
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:** ![Mihai\_Cazacu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihai_cazacu/32/2366_2.png) [@Mihai\_Cazacu](https://discuss.elastic.co/u/Mihai_Cazacu)\
**Post date:** [March 11, 2013, 6:09pm UTC](https://discuss.elastic.co/t/response-mapping/11089/3 "2013-03-11T18:09:41Z")

</div>

Yes, I already did this. It's simple when you have access to "getSourceAsString". But how about mapping a query response like this:

GetResponse response = client.prepareGet(index, type, id).setFields("title","name","postDate").execute().actionGet();

Thanks,  
Mihai

--  
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:** ![Mihai\_Cazacu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihai_cazacu/32/2366_2.png) [@Mihai\_Cazacu](https://discuss.elastic.co/u/Mihai_Cazacu)\
**Post date:** [March 11, 2013, 6:46pm UTC](https://discuss.elastic.co/t/response-mapping/11089/4 "2013-03-11T18:46:02Z")

</div>

Yes, I already did this. It's simple when you have access to  
"getSourceAsString". But how about mapping a  
org.elasticsearch.action.search.SearchResponse?

Thanks,  
Mihai

--  
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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 11, 2013, 6:56pm UTC](https://discuss.elastic.co/t/response-mapping/11089/5 "2013-03-11T18:56:23Z")

</div>

Did you look at the link I gave you?

> <https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L268>

SearchResponse sr = node.client().prepareSearch().setQuery(qb).execute().actionGet();

ObjectMapper mapper = new ObjectMapper(); // create once, reuse

for (SearchHit hit : sr.getHits()) {  
Beer beer = mapper.readValue(hit.getSourceAsString().getBytes(), Beer.class);  
}

It should do what you are expecting? Did I misunderstand what you are after?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 11 mars 2013 à 19:46, Mihai Cazacu [cazacugmihai@gmail.com](mailto:cazacugmihai@gmail.com) a écrit :

> Yes, I already did this. It's simple when you have access to "getSourceAsString". But how about mapping a org.elasticsearch.action.search.SearchResponse?  
> Thanks,  
> Mihai
> 
> --  
> 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:** ![Mihai\_Cazacu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihai_cazacu/32/2366_2.png) [@Mihai\_Cazacu](https://discuss.elastic.co/u/Mihai_Cazacu)\
**Post date:** [March 12, 2013, 7:18am UTC](https://discuss.elastic.co/t/response-mapping/11089/6 "2013-03-12T07:18:14Z")

</div>

Yes, I looked at that link but it is not related to my question.

I have a SearchResponse as a response for a query that request only some  
fields from the object (I mentioned in my initial post the link to the  
query type  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/fields.html)):

SearchRequestBuilder requestBuilder = client.prepareSearch....

> requestBuilder.addFields("field1", "field5.subField2", "field7")

Now, I want to build my objects from the response.

SearchHits hitsInfo = response.getHits()

> SearchHit hits = hitsInfo.hits()  
> for (SearchHit hit : hits) {

```
// here, hit.getSourceAsString() returns null 

Map<String, SearchHitField> fields = hit.fields() 

```

> ```
> MyFullObject obj = decodeMyFullObject(hit)
> 
> ```
> 
> }

So, my MyFullObject must be populated with that fields (that represent a  
part of all MyFullObject fields) returned by the response.

Thanks,  
Mihai

--  
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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 12, 2013, 8:36am UTC](https://discuss.elastic.co/t/response-mapping/11089/7 "2013-03-12T08:36:13Z")

</div>

Ok. I see now. Thanks for the clarification.

I'm afraid you have to build yourself your entity using getters/setters for all individual fields or to create a new JSON with your fields and use Jackson.

One more comment, you can also ask for field \_source in requestBuilder.add().

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 12 mars 2013 à 08:18, Mihai Cazacu [cazacugmihai@gmail.com](mailto:cazacugmihai@gmail.com) a écrit :

Yes, I looked at that link but it is not related to my question.

I have a SearchResponse as a response for a query that request only some fields from the object (I mentioned in my initial post the link to the query type [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/fields.html)):

> SearchRequestBuilder requestBuilder = client.prepareSearch....  
> requestBuilder.addFields("field1", "field5.subField2", "field7")

Now, I want to build my objects from the response.

> SearchHits hitsInfo = response.getHits()  
> SearchHit hits = hitsInfo.hits()  
> for (SearchHit hit : hits) {  
> // here, hit.getSourceAsString() returns null  
> Map\<String, SearchHitField\> fields = hit.fields()  
> MyFullObject obj = decodeMyFullObject(hit)  
> }

So, my MyFullObject must be populated with that fields (that represent a part of all MyFullObject fields) returned by the response.

Thanks,  
Mihai

--  
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:** ![Mihai\_Cazacu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihai_cazacu/32/2366_2.png) [@Mihai\_Cazacu](https://discuss.elastic.co/u/Mihai_Cazacu)\
**Post date:** [March 12, 2013, 8:49am UTC](https://discuss.elastic.co/t/response-mapping/11089/8 "2013-03-12T08:49:26Z")

</div>

Thanks, David. I resorted to this solution but I thought that there is an  
easy way for that mapping.

Best regards,  
Mihai

On Tue, Mar 12, 2013 at 10:36 AM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> Ok. I see now. Thanks for the clarification.
> 
> I'm afraid you have to build yourself your entity using getters/setters  
> for all individual fields or to create a new JSON with your fields and use  
> Jackson.
> 
> One more comment, you can also ask for field \_source in  
> requestBuilder.add().
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 12 mars 2013 à 08:18, Mihai Cazacu [cazacugmihai@gmail.com](mailto:cazacugmihai@gmail.com) a écrit :
> 
> Yes, I looked at that link but it is not related to my question.
> 
> I have a SearchResponse as a response for a query that request only some  
> fields from the object (I mentioned in my initial post the link to the  
> query type [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**guide/reference/api/search/)\*\*  
> fields.html[http://www.elasticsearch.org/guide/reference/api/search/fields.html](http://www.elasticsearch.org/guide/reference/api/search/fields.html)  
> ):
> 
> SearchRequestBuilder requestBuilder = client.prepareSearch....
> 
> > requestBuilder.addFields("field1", "field5.subField2", "field7")
> 
> Now, I want to build my objects from the response.
> 
> SearchHits hitsInfo = response.getHits()
> 
> > SearchHit hits = hitsInfo.hits()  
> > for (SearchHit hit : hits) {
> 
> ```
> // here, hit.getSourceAsString() returns null
> 
> Map<String, SearchHitField> fields = hit.fields()
> 
> ```
> 
> > ```
> > MyFullObject obj = decodeMyFullObject(hit)
> > 
> > ```
> > 
> > }
> 
> So, my MyFullObject must be populated with that fields (that represent a  
> part of all MyFullObject fields) returned by the response.
> 
> Thanks,  
> Mihai
> 
> --  
> 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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/\_a7NW4eZnEE/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/_a7NW4eZnEE/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, 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).

--  
Mihai Cazacu  
Software Engineer  
E-mail: [cazacugmihai@gmail.com](mailto:cazacugmihai@gmail.com)  
Mobile: +40 745 254 657  
Skype: cazacugmihai  
Twitter: cazacugmihai

--  
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:47am UTC](https://discuss.elastic.co/t/response-mapping/11089/9 "2017-07-06T02:47:03Z")

</div>


