Proposed way to use Elasticsearch Java API Client to generate requests


as i found for my self there are a bunch of ways how to use Elasticsearch Java API Client to generate requests.

  1. write JSON (in file or programm code) and push it over the low level Rest Client instead of the ESJAC
  2. write JSON (in file or programm code) and use a JsonMapper to parse it and then send it
  3. Use the Builder()-architecture to build each object by myself.

example for 1

String myJsonString = "{\n" +
			"  \"query\": {\n" +
			"    \"match_all\": {}\n" +
			"  }\n" +

		Request request = new Request("GET", "/products2/_search");
		Response response = restClient.performRequest(request);

example for 2
actually i was not able to rebuild it in elasticsearch 8 but this page:

See 2.2 PUT Mapping Nested Data Type
It tells that we could put the Json-Code directly in the Java-Code and then parse it with the .source(String, XContentType.JSON)-method

Question 1:
I was not able to programm that. Now .source()-Method wants to have a "SourceField"-parameter. How to use that plain JSON-text-parsing?

See 2.3 PUT Mapping XContentBuilder
It shows how to use XContentBuilder to build JSON in an objectoriented way.

Question 2:
But i was not able to recreate this because .source()-Method now wants to have a "SourceField"-parameter. How to use that XContentBuilder today?

XContentBuilder seems to still exist in Elasticsearch 8: XContentBuilder (elasticsearch-x-content 8.1.0 API)

example for 3

		Map<String, Property> msp = new HashMap<String, Property>();
		msp.put("Rechnungsnummer", new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build());
		msp.put("Rechnungsbetrag", new Property.Builder().float_(f -> f).build());
		PutMappingRequest pmr = new PutMappingRequest.Builder().index("rechnungen3").properties(msp).build();
		PutMappingResponse pma = client.indices().putMapping(pmr);

I have this code programmed by myself. It works, but on the first impression it looks little messy. Question 3:
Is this how it is proposed to be used? Or is there a smarter way?
Question 4:
Are there even more ways than the 3 i wrote down here?

Thanks - Enomine

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