# Proposed way to use Elasticsearch Java API Client to generate requests

**URL:** https://discuss.elastic.co/t/proposed-way-to-use-elasticsearch-java-api-client-to-generate-requests/317239
**Category:** Elasticsearch
**Created:** [October 22, 2022, 5:37am UTC](https://discuss.elastic.co/t/proposed-way-to-use-elasticsearch-java-api-client-to-generate-requests/317239 "2022-10-22T05:37:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Michael\_Gattinger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_gattinger/32/99690_2.png) [@Michael\_Gattinger](https://discuss.elastic.co/u/Michael_Gattinger)
#### Post date: [October 22, 2022, 5:37am UTC](https://discuss.elastic.co/t/proposed-way-to-use-elasticsearch-java-api-client-to-generate-requests/317239/1 "2022-10-22T05:37:00Z")

</div>

Hello,

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

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

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

```

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

> **[Elastic Search PUT Mapping Example - Java Developer Zone](https://javadeveloperzone.com/elastic-search/elastic-search-put-mapping-example/)**
>
> In this article, we wil discuss how to create index mapping , get mapping, get field mapping using elastic search transport client java api with an example

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?**

B)  
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)](https://javadoc.io/static/org.elasticsearch/elasticsearch-x-content/8.1.0/org/elasticsearch/xcontent/XContentBuilder.html)

example for 3

```auto
		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);
		System.out.println(pma.toString());

```

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

---

<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: [November 19, 2022, 5:37am UTC](https://discuss.elastic.co/t/proposed-way-to-use-elasticsearch-java-api-client-to-generate-requests/317239/2 "2022-11-19T05:37:37Z")

</div>

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