Indexing data into ElasticSearch 6.2 using java API


(Phani Kumar Yadavilli) #1

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.


(David Pilato) #2

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.


(Phani Kumar Yadavilli) #4

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();

}

}


(David Pilato) #5

It looks good to me.

What is the error?


(Phani Kumar Yadavilli) #6

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


(Phani Kumar Yadavilli) #7

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


(Phani Kumar Yadavilli) #8

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)

(Magnus Kessler) #9

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.


(David Pilato) #10

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.


(system) #11

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