# HOWTO streaming search response data into servlet response?

**URL:** <https://discuss.elastic.co/t/howto-streaming-search-response-data-into-servlet-response/4382>\
**Category:** Elasticsearch\
**Created:** [May 11, 2011, 2:28pm UTC](https://discuss.elastic.co/t/howto-streaming-search-response-data-into-servlet-response/4382 "2011-05-11T14:28:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [May 11, 2011, 2:28pm UTC](https://discuss.elastic.co/t/howto-streaming-search-response-data-into-servlet-response/4382/1 "2011-05-11T14:28:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 11, 2011, 5:27pm UTC](https://discuss.elastic.co/t/howto-streaming-search-response-data-into-servlet-response/4382/2 "2011-05-11T17:27:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [May 11, 2011, 10:06pm UTC](https://discuss.elastic.co/t/howto-streaming-search-response-data-into-servlet-response/4382/3 "2011-05-11T22:06:04Z")

</div>

Yep, this works, thanks!

On Wed, May 11, 2011 at 7:27 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> 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

---

<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, 4:06am UTC](https://discuss.elastic.co/t/howto-streaming-search-response-data-into-servlet-response/4382/4 "2017-07-06T04:06:24Z")

</div>


