# Indexing data into ElasticSearch 6.2 using java API

**URL:** https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988
**Category:** Elasticsearch
**Created:** [February 8, 2018, 7:37am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988 "2018-02-08T07:37:41Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![wandermonk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wandermonk/32/27338_2.png) [@wandermonk](https://discuss.elastic.co/u/wandermonk)
#### Post date: [February 8, 2018, 7:37am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/1 "2018-02-08T07:37:41Z")

</div>

As we know that the elasticsearch has deprecated types in 6.2.0 release how do we index data to an elastic search index.

```
public class ElasticSearchWriter {

	private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchWriter.class);
	private static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(
			"https://search-ces.amazonaws.com/", 9200, "https")));;

	public static void writeToElasticSearch(String jsonString) throws JsonProcessingException, IOException {
		ObjectMapper om = new ObjectMapper();
		JsonNode actualObject = om.readTree(jsonString);
		String documentId = actualObject.get("id").textValue();
		IndexRequest request = new IndexRequest("testdata", "data", documentId);
		request.source(jsonString, XContentType.JSON);
		client.indexAsync(request, new ActionListener<IndexResponse>() {
			@Override
			public void onResponse(IndexResponse indexResponse) {
				LOGGER.info("The Index ID is :: " + indexResponse.getId());
				LOGGER.info("The Index is :: " + indexResponse.getIndex());
				LOGGER.info("The type is :: " + indexResponse.getType());
			}

			@Override
			public void onFailure(Exception e) {

			}
		});

	}

}

```

How do I instantiate the IndexRequest Object without using the type argument?

IndexRequest request = new IndexRequest("testdata", "data", documentId);

When I go through the constructors I did not find any constructor with IndexRequest(index,documentid);

Please 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 8, 2018, 8:03am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/2 "2018-02-08T08:03:39Z")

</div>

Types are deprecated. Not removed.

This is a long process to get them removed.  
For now, I'd recommend just using `"doc"` as your type name.

---

<div class="post-metadata">

### Author: ![wandermonk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wandermonk/32/27338_2.png) [@wandermonk](https://discuss.elastic.co/u/wandermonk)
#### Post date: [February 9, 2018, 10:23am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/4 "2018-02-09T10:23:31Z")

</div>

Hi David,

I am trying to insert a record under the index /testdata/doc. But, the code is not working. I would like to know if I am using the API in a right way. Please help.

public class ElasticSearchWriter {

```
//private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchWriter.class);

private static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(
		"localhost", 9200, "http")));

public static void writeToElasticSearch(String jsonString) throws JsonProcessingException, IOException {
	ObjectMapper om = new ObjectMapper();
	JsonNode actualObject = om.readTree(jsonString);
	String documentId = actualObject.get("id").textValue();
	IndexRequest request = new IndexRequest("testdata", "doc",documentId);
	request.source(jsonString, XContentType.JSON);
	client.indexAsync(request, new ActionListener<IndexResponse>() {
		public void onResponse(IndexResponse indexResponse) {				
			System.out.println("The Index ID is :: " + indexResponse.getId());
			System.out.println("The Index is :: " + indexResponse.getIndex());
			System.out.println("The type is :: " + indexResponse.getType());
		}

		public void onFailure(Exception e) {
			e.printStackTrace();
		}
	});
	
	client.close();

}

```

}

---

<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 9, 2018, 10:36am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/5 "2018-02-09T10:36:15Z")

</div>

It looks good to me.

What is the error?

---

<div class="post-metadata">

### Author: ![wandermonk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wandermonk/32/27338_2.png) [@wandermonk](https://discuss.elastic.co/u/wandermonk)
#### Post date: [February 9, 2018, 10:54am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/6 "2018-02-09T10:54:23Z")

</div>

It is not throwing any error and It neither inserts the record.

---

<div class="post-metadata">

### Author: ![wandermonk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wandermonk/32/27338_2.png) [@wandermonk](https://discuss.elastic.co/u/wandermonk)
#### Post date: [February 9, 2018, 11:30am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/7 "2018-02-09T11:30:48Z")

</div>

Could you please point me to any working code which creates an index with mapping and inserts document to the ElasticSearch.

---

<div class="post-metadata">

### Author: ![wandermonk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wandermonk/32/27338_2.png) [@wandermonk](https://discuss.elastic.co/u/wandermonk)
#### Post date: [February 9, 2018, 11:32am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/8 "2018-02-09T11:32:05Z")

</div>

public class ElasticSearchIndexCreator {

```
private static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
		new HttpHost("search-cdxsdses-ztxv3lvq23ptq4sdr6d3fsjat4.us-east-2.es.amazonaws.com", 9200, "https")));

public static void createIndex() {
	IndexRequest request = new IndexRequest("posts", "doc", "1").source("user", "kimchy",
            "postDate", new Date(),
            "message", "trying out Elasticsearch");
	client.indexAsync(request, new ActionListener<IndexResponse>() {
	    public void onResponse(IndexResponse indexResponse) {
	    	
	    	System.out.println("The index is :: "+indexResponse.getIndex());
	        
	    }

	    public void onFailure(Exception e) {
	    	
	    	e.printStackTrace();
	        
	    }
	});
}

```

}

When I am trying to create an index it is throwing an exception

```
Exception in thread "main" java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW
	at org.elasticsearch.common.xcontent.json.JsonXContent.<clinit>(JsonXContent.java:57)
	at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:121)
	at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:366)
	at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:347)
	at com.cisco.sdp.microservices.ElasticSearchMS.ElasticSearchIndexCreator.createIndex(ElasticSearchIndexCreator.java:19)
	at com.cisco.sdp.microservices.ElasticSearchMS.MainApp.main(MainApp.java:21)
```

---

<div class="post-metadata">

### Author: ![Magnus\_Kessler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnus_kessler/32/42001_2.png) [@Magnus\_Kessler](https://discuss.elastic.co/u/Magnus_Kessler)
#### Post date: [February 9, 2018, 11:38am UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/9 "2018-02-09T11:38:55Z")

</div>

Elasticsearch 6.x does not longer support _multiple_ document types per index, but still requires you to specify a document type. You should be able to continue using the code as before.

However, if you try to index a document under a different document type than the first one used on an index, the request will be rejected.

---

<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 9, 2018, 1:29pm UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/10 "2018-02-09T13:29:06Z")

</div>

How do you know? How do you test that?  
I'm sorry but I don't have the full picture here so it's hard to tell.

---

<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 9, 2018, 1:29pm UTC](https://discuss.elastic.co/t/indexing-data-into-elasticsearch-6-2-using-java-api/118988/11 "2018-03-09T13:29:18Z")

</div>

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