# Returning Search Results as JSON

**URL:** <https://discuss.elastic.co/t/returning-search-results-as-json/6082>\
**Category:** Elasticsearch\
**Created:** [December 6, 2011, 10:04pm UTC](https://discuss.elastic.co/t/returning-search-results-as-json/6082 "2011-12-06T22:04:30Z")\
**Posts on this page:** 6\
**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:** [December 6, 2011, 10:04pm UTC](https://discuss.elastic.co/t/returning-search-results-as-json/6082/1 "2011-12-06T22:04:30Z")

</div>

I work in SSJS, so JS access to elasticsearch is pretty much second nature:

var searchResponse = getClient().search(request).actionGet();  
var searchHits = searchResponse.hits().hits();

return searchHits.map(function(hit) {  
return JSON.parse(hit.sourceAsString());  
});

You might notice that I am iterating over search hits only here. Now, I  
want access to facets and other information at a higher level than just  
hits. How might I take a SearchResult object and convert it to a JSON  
string value? I am assuming the REST interface does this, but I didn't find  
where the magic happens.

---

<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:** [December 7, 2011, 8:42am UTC](https://discuss.elastic.co/t/returning-search-results-as-json/6082/2 "2011-12-07T08:42:59Z")

</div>

Hi James,

I am working in a little different scenario, no SSJS (assuming this stands for Server Side JS). In my case I return JSON form servlet proxy and consume it on the client side (web browser). And I do it like this, may be this will help:

```
        ServletOutputStream os = (HttpServletResponse) response.getOutputStream();

        … do the search and get search response ...

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

        os.close();  

```

--  
Regards,  
Lukas

On Tuesday, December 6, 2011 at 11:04 PM, James Cook wrote:

> I work in SSJS, so JS access to elasticsearch is pretty much second nature:
> 
> var searchResponse = getClient().search(request).actionGet();  
> var searchHits = searchResponse.hits().hits();
> 
> return searchHits.map(function(hit) {  
> return JSON.parse(hit.sourceAsString());  
> });
> 
> You might notice that I am iterating over search hits only here. Now, I want access to facets and other information at a higher level than just hits. How might I take a SearchResult object and convert it to a JSON string value? I am assuming the REST interface does this, but I didn't find where the magic happens.

---

<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:** [December 7, 2011, 2:54pm UTC](https://discuss.elastic.co/t/returning-search-results-as-json/6082/3 "2011-12-07T14:54:03Z")

</div>

Thanks Lucas. I'll give it a shot. I haven't played with XContentBuilders  
yet, so I guess it is time. Seems like a convenience method ( toJSON() )  
would be welcome.

---

<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:** [December 7, 2011, 4:04pm UTC](https://discuss.elastic.co/t/returning-search-results-as-json/6082/4 "2011-12-07T16:04:07Z")

</div>

SearchResponse.#toString will return you a json string, though you probably  
don't want to do that since its the less efficient way to stream it back to  
the client.

On Wed, Dec 7, 2011 at 4:54 PM, James Cook [jcook@pykl.com](mailto:jcook@pykl.com) wrote:

> Thanks Lucas. I'll give it a shot. I haven't played with XContentBuilders  
> yet, so I guess it is time. Seems like a convenience method ( toJSON() )  
> would be welcome.

---

<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:** [December 7, 2011, 4:18pm UTC](https://discuss.elastic.co/t/returning-search-results-as-json/6082/5 "2011-12-07T16:18:07Z")

</div>

I didn't know that. That is even easier. There is no streaming to client  
involved because this is an embedded ES instance. Thanks.

---

<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:46am UTC](https://discuss.elastic.co/t/returning-search-results-as-json/6082/6 "2017-07-06T03:46:09Z")

</div>


