Create search request from available json unit test

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?

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.

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

{
  "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

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

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]

Hi,

I fixed the searchresponse part.

What was the error

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

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;
}