Response mapping

Hi,

Is there an automatic way to map a search result like 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.
For more options, visit https://groups.google.com/groups/opt_out.

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

HTH

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

Le 10 mars 2013 à 22:01, Mihai Cazacu 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 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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.

Did you look at the link I gave you?

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
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 11 mars 2013 à 19:46, Mihai Cazacu 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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

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
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.
For more options, visit https://groups.google.com/groups/opt_out.

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 :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 12 mars 2013 à 08:18, Mihai Cazacu 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 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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

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 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 :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 12 mars 2013 à 08:18, Mihai Cazacu 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 http://www.elasticsearch.org/guide/reference/api/search/
fields.htmlhttp://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.
For more options, visit 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
.
To unsubscribe from this group and all its topics, send an email to
elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
Mihai Cazacu
Software Engineer
E-mail: 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.
For more options, visit https://groups.google.com/groups/opt_out.