HOWTO streaming search response data into servlet response?


(Lukáš Vlček) #1

Hi,

what would be the best way to streaming data from SearchResponse to servlet
response?

I can imagine something like the following would work but is definitely not
very efficient:

@Override protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws IOException, ServletException {
response.setContentType("application/json;charset=UTF-8");
String json = searchClient.getClient()
.search( ... )
.actionGet()
.toString();
response.getWriter().write(json);
}

Is there a better (faster) way how to push the response data directly into
response using output stream?

ServletOutputStream out = response.getOutputStream();
out.write( /* can search response give me something that goes here? */);
out.close();

If the code snippets are not readable let me know I will gist it.

Regards,
Lukas


(Shay Banon) #2

You can use XContentFactory#jsonBuilder(OutputStream), and then do:

XContentBuilder builder = XContetFactory.jsonBuilder(os);
builder.startObject();
searchResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
builder.close();
On Wednesday, May 11, 2011 at 5:28 PM, Lukáš Vlček wrote:

Hi,

what would be the best way to streaming data from SearchResponse to servlet response?

I can imagine something like the following would work but is definitely not very efficient:

@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("application/json;charset=UTF-8");
String json = searchClient.getClient()
.search( ... )
.actionGet()
.toString();
response.getWriter().write(json);
}

Is there a better (faster) way how to push the response data directly into response using output stream?

ServletOutputStream out = response.getOutputStream();
out.write( /* can search response give me something that goes here? */);
out.close();

If the code snippets are not readable let me know I will gist it.

Regards,
Lukas


(Lukáš Vlček) #3

Yep, this works, thanks!

On Wed, May 11, 2011 at 7:27 PM, Shay Banon shay.banon@elasticsearch.comwrote:

You can use XContentFactory#jsonBuilder(OutputStream), and then do:

XContentBuilder builder = XContetFactory.jsonBuilder(os);
builder.startObject();
searchResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
builder.close();

On Wednesday, May 11, 2011 at 5:28 PM, Lukáš Vlček wrote:

Hi,

what would be the best way to streaming data from SearchResponse to servlet
response?

I can imagine something like the following would work but is definitely not
very efficient:

@Override protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws IOException, ServletException {
response.setContentType("application/json;charset=UTF-8");
String json = searchClient.getClient()
.search( ... )
.actionGet()
.toString();
response.getWriter().write(json);
}

Is there a better (faster) way how to push the response data directly into
response using output stream?

ServletOutputStream out = response.getOutputStream();
out.write( /* can search response give me something that goes here? */);
out.close();

If the code snippets are not readable let me know I will gist it.

Regards,
Lukas


(system) #4