# Adding a Document in Java

**URL:** <https://discuss.elastic.co/t/adding-a-document-in-java/116332>\
**Category:** Elasticsearch\
**Created:** [January 20, 2018, 9:26am UTC](https://discuss.elastic.co/t/adding-a-document-in-java/116332 "2018-01-20T09:26:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_Goel](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@Deepak\_Goel](https://discuss.elastic.co/u/Deepak_Goel)\
**Post date:** [January 20, 2018, 9:26am UTC](https://discuss.elastic.co/t/adding-a-document-in-java/116332/1 "2018-01-20T09:26:38Z")

</div>

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

---

<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:** [January 20, 2018, 10:56am UTC](https://discuss.elastic.co/t/adding-a-document-in-java/116332/2 "2018-01-20T10:56:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Deepak\_Goel](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@Deepak\_Goel](https://discuss.elastic.co/u/Deepak_Goel)\
**Post date:** [January 20, 2018, 4:10pm UTC](https://discuss.elastic.co/t/adding-a-document-in-java/116332/3 "2018-01-20T16:10:13Z")

</div>

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

---

<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:** [January 20, 2018, 5:02pm UTC](https://discuss.elastic.co/t/adding-a-document-in-java/116332/4 "2018-01-20T17:02:29Z")

</div>

Thé link I pasted is for the rest Client.

---

<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:** [February 17, 2018, 5:02pm UTC](https://discuss.elastic.co/t/adding-a-document-in-java/116332/5 "2018-02-17T17:02:49Z")

</div>

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