Adding a Document in Java


(Deepak Goel) #1

Hello

I would like to add a document to a collection in ElasticSearch using Java. Is there a sample program with explanation available?

Thank You


(David Pilato) #2

This: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html


(Deepak Goel) #3

I hope this is right:

//Add documents

	Map<String, Object> jsonMap = new HashMap<>();
	jsonMap.put("user", "kimchy");
	jsonMap.put("postDate", new Date());
	jsonMap.put("message", "trying out Elasticsearch");
	IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
	        .source(jsonMap);
	
	IndexResponse indexResponse = (IndexResponse) client.index(indexRequest);
	System.out.println(indexResponse.toString());

Also what about the 'Rest Client' code? Would it be the same?

Thank You


(David Pilato) #4

Thé link I pasted is for the rest Client.


(system) #5

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