# How to put a check before inserting data in a index

**URL:** https://discuss.elastic.co/t/how-to-put-a-check-before-inserting-data-in-a-index/9911
**Category:** Elasticsearch
**Created:** [December 1, 2012, 7:37am UTC](https://discuss.elastic.co/t/how-to-put-a-check-before-inserting-data-in-a-index/9911 "2012-12-01T07:37:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![divyanshu\_das](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@divyanshu\_das](https://discuss.elastic.co/u/divyanshu_das)
#### Post date: [December 1, 2012, 7:37am UTC](https://discuss.elastic.co/t/how-to-put-a-check-before-inserting-data-in-a-index/9911/1 "2012-12-01T07:37:55Z")

</div>

I am using java client api to index data.

IndexResponse response = client.prepareIndex("nod",  
"rel").setSource(met.xb).execute().actionGet();

where met.xb contains all the data.

What I want to do here is to put a check on the data being indexed, the  
program should first check if the value that is being inserted is already  
there or not, if it is there, then discard it, if not then update the value.

lets say i add, a custom id field here...like this:-  
string id = curr.getId().toString(); // this comes from database

IndexResponse response = client.prepareIndex("nod", "rel",  
id).setSource(met.xb).execute().actionGet();

id, wont be changing, so now I can identify the json through the id, next  
time I insert the data,

But how do I put a check for update/discard??

Plz help me out here.

--

---

<div class="post-metadata">

### Author: ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)
#### Post date: [December 2, 2012, 7:16am UTC](https://discuss.elastic.co/t/how-to-put-a-check-before-inserting-data-in-a-index/9911/2 "2012-12-02T07:16:04Z")

</div>

Hi,

quick ideas:

- have a look at how to use op\_type with index API. See  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html) (navigate to  
"Operation Type" chapter)

- Also you can use HEAD operation in GET  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/get.html) to check if the  
object already exists (using object \_id).

Regards,  
Lukas

On Sat, Dec 1, 2012 at 8:37 AM, divyanshu das [divyanshu.das@gmail.com](mailto:divyanshu.das@gmail.com)wrote:

> I am using java client api to index data.
> 
> IndexResponse response = client.prepareIndex("nod",  
> "rel").setSource(met.xb).execute().actionGet();
> 
> where met.xb contains all the data.
> 
> What I want to do here is to put a check on the data being indexed, the  
> program should first check if the value that is being inserted is already  
> there or not, if it is there, then discard it, if not then update the value.
> 
> lets say i add, a custom id field here...like this:-  
> string id = curr.getId().toString(); // this comes from database
> 
> IndexResponse response = client.prepareIndex("nod", "rel",  
> id).setSource(met.xb).execute().actionGet();
> 
> id, wont be changing, so now I can identify the json through the id, next  
> time I insert the data,
> 
> But how do I put a check for update/discard??
> 
> Plz help me out here.
> 
> --

--

---

<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, 3:01am UTC](https://discuss.elastic.co/t/how-to-put-a-check-before-inserting-data-in-a-index/9911/3 "2017-07-06T03:01:52Z")

</div>


