# Create search request from available json unit test

**URL:** <https://discuss.elastic.co/t/create-search-request-from-available-json-unit-test/264952>\
**Category:** Elasticsearch\
**Created:** [February 20, 2021, 2:54pm UTC](https://discuss.elastic.co/t/create-search-request-from-available-json-unit-test/264952 "2021-02-20T14:54:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohit\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_sharma/32/80029_2.png) [@Rohit\_Sharma](https://discuss.elastic.co/u/Rohit_Sharma)\
**Post date:** [February 20, 2021, 2:54pm UTC](https://discuss.elastic.co/t/create-search-request-from-available-json-unit-test/264952/1 "2021-02-20T14:54:39Z")

</div>

Hi,

I'm new to elastic search.

I need to write unit test where I need to create search request object and search response object.

I have search response and search request available in JSON.

My search request contains aggregation part too.

How to do this in Java without using Rest High Level Client.

Can you help?

---

<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:** [February 20, 2021, 4:35pm UTC](https://discuss.elastic.co/t/create-search-request-from-available-json-unit-test/264952/2 "2021-02-20T16:35:17Z")

</div>

> [@Rohit\_Sharma](#):
>
> How to do this in Java without using Rest High Level Client.

Why?

If you want to have just json text, you can use the low level client only. If you want objects representing the json content, use the high level client.

---

<div class="post-metadata">

**Author:** ![Rohit\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_sharma/32/80029_2.png) [@Rohit\_Sharma](https://discuss.elastic.co/u/Rohit_Sharma)\
**Post date:** [February 22, 2021, 5:03am UTC](https://discuss.elastic.co/t/create-search-request-from-available-json-unit-test/264952/3 "2021-02-22T05:03:21Z")

</div>

Hi,

Thanks for replying. I can't use client as the code is building on jenkins server which doesn't have elasticsearch and subsequently the build fails.

# My JSON foir searchsourcebuilder

```auto
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "dimensions.attributeValue": {
              "query": "Order123",
              "slop": 0,
              "zero_terms_query": "NONE",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "aggregations": {
    "agg1": {
      "terms": {
        "field": "dimensions.attributeValue.keyword",
        "size": 100,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      },
      "aggregations": {
        "aggUpdatedAt": {
          "date_histogram": {
            "field": "aggUpdatedAt",
            "calendar_interval": "1w",
            "offset": 0,
            "order": {
              "_key": "asc"
            },
            "keyed": false,
            "min_doc_count": 1
          }
        }
      }
    }
  }
}

```

# What i have tried till now

I used my JSON to build searchsourcebuilder from following code

```auto
JsonXContentParser xContentParser = new JsonXContentParser(NamedXContentRegistry.EMPTY, null, new JsonFactory().createParser(requestJSON));  
	        
SearchSourceBuilder searchsourcebuilder=SearchSourceBuilder.fromXContent(xContentParser);

```

Where i get this exception =\> ParsingException[no [query] registered for [bool]]

# Similarly for search response I'm writing the code as follows

```auto
try {
        NamedXContentRegistry registry = new NamedXContentRegistry(getDefaultNamedXContents());
        XContentParser parser = JsonXContent.jsonXContent.createParser(registry, null, responseJSON);
        return SearchResponse.fromXContent(parser);
    } catch (IOException e) {
        System.out.println("exception " + e);
    }catch (Exception e){
        System.out.println("exception " + e);
    }
    return null;

```

}

where i get this exception =\> [ParsedStringTerms] failed to parse field [buckets]

---

<div class="post-metadata">

**Author:** ![Rohit\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_sharma/32/80029_2.png) [@Rohit\_Sharma](https://discuss.elastic.co/u/Rohit_Sharma)\
**Post date:** [February 22, 2021, 6:58am UTC](https://discuss.elastic.co/t/create-search-request-from-available-json-unit-test/264952/4 "2021-02-22T06:58:27Z")

</div>

Hi,

I fixed the searchresponse part.

# What was the error

I was not mapping datehistogram at the time of making registry.

```auto
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));
//This line was missing
    map.put(DateHistogramAggregationBuilder.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;
}

```

---

<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:** [March 22, 2021, 6:59am UTC](https://discuss.elastic.co/t/create-search-request-from-available-json-unit-test/264952/5 "2021-03-22T06:59:27Z")

</div>

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