# Use java Api to set a document's field as \_id

**URL:** https://discuss.elastic.co/t/use-java-api-to-set-a-documents-field-as--id/18912
**Category:** Elasticsearch
**Created:** [July 25, 2014, 11:22pm UTC](https://discuss.elastic.co/t/use-java-api-to-set-a-documents-field-as--id/18912 "2014-07-25T23:22:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Chia\_Eng\_Chang](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Chia\_Eng\_Chang](https://discuss.elastic.co/u/Chia_Eng_Chang)
#### Post date: [July 25, 2014, 11:22pm UTC](https://discuss.elastic.co/t/use-java-api-to-set-a-documents-field-as--id/18912/1 "2014-07-25T23:22:41Z")

</div>

I want to ask if the unique field \_id be assigned by certain field within document. I see with Rest, it can achieve by "path":

```
{
    "tweet" : {
        "_id" : {
           "path" : "post_id"
       }
    }
}

```

But if I want to do it with java API, is there any way to achieve it?

```
Map<String, Object> MapA= new HashMap<String, Object>();
MapA=MapProcessor(MapA);

client.prepareIndex("index","type").setSource(MapA).execute().actionGet();

```

How could I modify my code to assign certain field in Map to become \_id of  
this type?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3f9fe5df-cd96-4083-b21b-610dbe265e38%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3f9fe5df-cd96-4083-b21b-610dbe265e38%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Kireet\_Reddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kireet_reddy/32/1071_2.png) [@Kireet\_Reddy](https://discuss.elastic.co/u/Kireet_Reddy)
#### Post date: [July 27, 2014, 6:09pm UTC](https://discuss.elastic.co/t/use-java-api-to-set-a-documents-field-as--id/18912/2 "2014-07-27T18:09:00Z")

</div>

You should check out the IndexRequestBuilder class. It helps simplify  
creating indexing requests and has a setId() method.

On Friday, July 25, 2014 4:22:42 PM UTC-7, Chia-Eng Chang wrote:

> I want to ask if the unique field \_id be assigned by certain field within document. I see with Rest, it can achieve by "path":
> 
> ```
> {
> "tweet" : {
> "_id" : {
> "path" : "post_id"
> }
> }
> }
> 
> ```
> 
> But if I want to do it with java API, is there any way to achieve it?
> 
> ```
> Map<String, Object> MapA= new HashMap<String, Object>();
> MapA=MapProcessor(MapA);
> 
> client.prepareIndex("index","type").setSource(MapA).execute().actionGet();
> 
> ```
> 
> How could I modify my code to assign certain field in Map to become \_id of  
> this type?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/66966b06-71dc-4ff9-a863-4371bf9ca368%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/66966b06-71dc-4ff9-a863-4371bf9ca368%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [July 28, 2014, 4:21pm UTC](https://discuss.elastic.co/t/use-java-api-to-set-a-documents-field-as--id/18912/3 "2014-07-28T16:21:09Z")

</div>

The behavior is applied at the creation of the index within the mapping,  
not during the prepareIndex call. The example you provided is part of the  
mapping and not part of the document that gets indexed. If you want to  
override which field will be used as the \_id field, you would need to  
change your mapping.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Cheers,

Ivan

On Sun, Jul 27, 2014 at 11:09 AM, Kireet Reddy [kireet@feedly.com](mailto:kireet@feedly.com) wrote:

> You should check out the IndexRequestBuilder class. It helps simplify  
> creating indexing requests and has a setId() method.
> 
> On Friday, July 25, 2014 4:22:42 PM UTC-7, Chia-Eng Chang wrote:
> 
> > I want to ask if the unique field \_id be assigned by certain field within document. I see with Rest, it can achieve by "path":
> > 
> > ```
> > {
> > "tweet" : {
> > "_id" : {
> > "path" : "post_id"
> > }
> > }
> > }
> > 
> > ```
> > 
> > But if I want to do it with java API, is there any way to achieve it?
> > 
> > ```
> > Map<String, Object> MapA= new HashMap<String, Object>();
> > MapA=MapProcessor(MapA);
> > 
> > client.prepareIndex("index","type").setSource(MapA).execute().actionGet();
> > 
> > ```
> > 
> > How could I modify my code to assign certain field in Map to become \_id  
> > of this type?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/66966b06-71dc-4ff9-a863-4371bf9ca368%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/66966b06-71dc-4ff9-a863-4371bf9ca368%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/66966b06-71dc-4ff9-a863-4371bf9ca368%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/66966b06-71dc-4ff9-a863-4371bf9ca368%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAnTcE9BArRFLox7iSuE3nt49dG3y0rC1pB%3DLNqecVtbQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAnTcE9BArRFLox7iSuE3nt49dG3y0rC1pB%3DLNqecVtbQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [July 6, 2017, 1:12am UTC](https://discuss.elastic.co/t/use-java-api-to-set-a-documents-field-as--id/18912/4 "2017-07-06T01:12:45Z")

</div>


