# Elasticsearch json response to SearchResponse object

**URL:** <https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394>\
**Category:** Elasticsearch\
**Created:** [March 17, 2018, 9:20am UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394 "2018-03-17T09:20:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Monte\_A](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Monte\_A](https://discuss.elastic.co/u/Monte_A)\
**Post date:** [March 17, 2018, 9:20am UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394/1 "2018-03-17T09:20:06Z")

</div>

Hi There,

Could someone help to get it working?  
So far I got the solution below. This works for simple queries, however it doesn't work for aggregations and I am getting exception ParsingException "Could not parse aggregation keyed as [dashboard]"

JsonXContentParser xContentParser = new JsonXContentParser(NamedXContentRegistry.EMPTY,  
new JsonFactory().createParser(SearchResponse response = SearchResponse.fromXContent(xContentParser);

The response I need to parse is provided below:  
{  
"took": 16,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 2,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "demo",  
"\_type": "demo",  
"\_id": "OSScKmIBKBDaRuGPyr42",  
"\_score": 1,  
"\_source": {  
"demoId": "2",  
"demoName": "j",  
"uniqueDemo": "jab1980xy11b"  
}  
},  
{  
"\_index": "demo",  
"\_type": "demo",  
"\_id": "OCScKmIBKBDaRuGPyr42",  
"\_score": 1,  
"\_source": {  
"demoId": "2",  
"demoName": "j",  
"uniqueDemo": "jmb1970oi611234554321123a"  
}  
}  
]  
},  
"aggregations": {  
"dashboard": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "jab1980xy11b",  
"doc\_count": 1,  
"data": {  
"hits": {  
"total": 1,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "demo",  
"\_type": "demo",  
"\_id": "OSScKmIBKBDaRuGPyr42",  
"\_score": 1,  
"\_source": {  
"demoId": "2",  
"demoName": "j"  
}  
}  
]  
}  
}  
},  
{  
"key": "jmb1970oi611234554321123a",  
"doc\_count": 1,  
"data": {  
"hits": {  
"total": 1,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "demo",  
"\_type": "demo",  
"\_id": "OCScKmIBKBDaRuGPyr42",  
"\_score": 1,  
"\_source": {  
"demoId": "2",  
"demoName": "j"  
}  
}  
]  
}  
}  
}  
]  
}  
}  
}

---

<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 17, 2018, 9:34am UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394/2 "2018-03-17T09:34:34Z")

</div>

Out of curiosity why you are not calling the `search(SearchRequest)` method? It gives you back a `SearchResponse` object (including aggs)

---

<div class="post-metadata">

**Author:** ![Monte\_A](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Monte\_A](https://discuss.elastic.co/u/Monte_A)\
**Post date:** [March 17, 2018, 9:53am UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394/3 "2018-03-17T09:53:06Z")

</div>

We do so in our application. However, I am writing junit test and want to verify that the business logic is correct, hence I want to interrogate the SearchResponse object.  
If there is better way to write unit test I would be more then happy to hear.

---

<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 17, 2018, 11:15am UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394/4 "2018-03-17T11:15:42Z")

</div>

As far as I can read the code

> <https://github.com/elastic/elasticsearch/blob/6.x/server/src/main/java/org/elasticsearch/action/search/SearchResponse.java#L245-L249>

It looks like `innerFromXContent` is also parsing the aggs part.

> <https://github.com/elastic/elasticsearch/blob/6.x/server/src/main/java/org/elasticsearch/action/search/SearchResponse.java#L288-L289>

This is calling:

> <https://github.com/elastic/elasticsearch/blob/6.x/server/src/main/java/org/elasticsearch/search/aggregations/Aggregations.java#L134-L152>

Behind the scene this needs to know what kind of aggregation you asked for, what is the java type to use. The magic is happening here:

> <https://github.com/elastic/elasticsearch/blob/6.x/server/src/main/java/org/elasticsearch/common/xcontent/XContentParserUtils.java#L122-L161>

So you need to add a prefix like `terms#` if you are getting back a terms aggregation.

`terms#foo` as the javadoc example says.

I hope this can help.

---

<div class="post-metadata">

**Author:** ![Monte\_A](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Monte\_A](https://discuss.elastic.co/u/Monte_A)\
**Post date:** [March 17, 2018, 12:19pm UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394/5 "2018-03-17T12:19:09Z")

</div>

Thank you for your help. Behind the scene magic explanation helped me to get working solution.

Will post it after a little bit more testing.

---

<div class="post-metadata">

**Author:** ![Monte\_A](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Monte\_A](https://discuss.elastic.co/u/Monte_A)\
**Post date:** [March 18, 2018, 3:14pm UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394/6 "2018-03-18T15:14:43Z")

</div>

Working solution for someone that might have the same issue.

**Elasticsearch response with aggregation example:**

> <https://gist.github.com/anonymous/cd2de1ccdedf072f40b69b9094afa05e>

**Helper method to create NamedXContentRegistry**

```
public static List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {
    Map<String, ContextParser<Object, ? extends Aggregation>> map = new HashMap<>();
    map.put(TopHitsAggregationBuilder.NAME, (p, c) -> ParsedTopHits.fromXContent(p, (String) c));
    map.put(StringTerms.NAME, (p, c) -> ParsedStringTerms.fromXContent(p, (String) c));
    List<NamedXContentRegistry.Entry> entries = map.entrySet().stream()
            .map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))
            .collect(Collectors.toList());
  return entries;
}

```

**Method to get SearchResponse from String/Json**

```
public static SearchResponse getSearchResponseFromJson(String jsonResponse){
    try {
        NamedXContentRegistry registry = new NamedXContentRegistry(DashboardCuke.getDefaultNamedXContents());
        XContentParser parser = JsonXContent.jsonXContent.createParser(registry, jsonResponse);
        return SearchResponse.fromXContent(parser);
    } catch (IOException e) {
        System.out.println("exception " + e);
    }catch (Exception e){
        System.out.println("exception " + e);
    }
    return new SearchResponse();
}
```

---

<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:** [April 15, 2018, 3:14pm UTC](https://discuss.elastic.co/t/elasticsearch-json-response-to-searchresponse-object/124394/7 "2018-04-15T15:14:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
