# Checking if value exists in array (before adding-it)

**URL:** <https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452>\
**Category:** Elasticsearch\
**Created:** [April 4, 2013, 2:54pm UTC](https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452 "2013-04-04T14:54:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fernando\_Javier\_Mart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fernando_javier_mart/32/2358_2.png) [@Fernando\_Javier\_Mart](https://discuss.elastic.co/u/Fernando_Javier_Mart)\
**Post date:** [April 4, 2013, 2:54pm UTC](https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452/1 "2013-04-04T14:54:05Z")

</div>

Hi there.

I have a "user" document which has an array of friends.  
I need to add/delete friends from that array.

I'm currently being able to add or remove, but problem is that if a user ID  
already exists gets added anyway.  
I need to perform a check before actually adding it (desiderable in one  
operation "if (not\_exists) add it" ).

This is my user object and mappings (and scripts).

> <https://gist.github.com/fernandojmartin/5311012>

Thanks in advance.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Nabloom](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@Nabloom](https://discuss.elastic.co/u/Nabloom)\
**Post date:** [April 4, 2013, 4:34pm UTC](https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452/2 "2013-04-04T16:34:14Z")

</div>

Hi,

You can play with the 'ctx.op' field, so in your case, you can perform the  
following update :  
ctx.\_source.friends.contains(friend) ? ctx.\_source.friends += friend :  
ctx.op = "none"

Nabloom

On Thursday, April 4, 2013 4:54:05 PM UTC+2, Fernando Javier Martin wrote:

> Hi there.
> 
> I have a "user" document which has an array of friends.  
> I need to add/delete friends from that array.
> 
> I'm currently being able to add or remove, but problem is that if a user  
> ID already exists gets added anyway.  
> I need to perform a check before actually adding it (desiderable in one  
> operation "if (not\_exists) add it" ).
> 
> This is my user object and mappings (and scripts).  
> [The mapping and the user object · GitHub](https://gist.github.com/fernandojmartin/5311012)
> 
> Thanks in advance.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Fernando\_Javier\_Mart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fernando_javier_mart/32/2358_2.png) [@Fernando\_Javier\_Mart](https://discuss.elastic.co/u/Fernando_Javier_Mart)\
**Post date:** [April 4, 2013, 6:35pm UTC](https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452/3 "2013-04-04T18:35:24Z")

</div>

Thx Nabloom.

I've tried your suggestion with neither error or success.  
This is the actual code:

{  
"script" : "ctx.\_source.friends.contains(friend) ? ctx.op = 'none' :  
ctx.\_source.friends += friend; ctx.\_source.friends\_qty =  
ctx.\_source.friends.size()",  
"params" : { "friend" : "88" }  
}

Am I missing anything?  
I've tried even removing the friend\_qty update operation, just in case the  
semicolon was causing something unwanted (is that the right way to put  
multiple statements?).  
Version number gets incremented on every POST but no changes at all to the  
document.

El jueves, 4 de abril de 2013 11:54:05 UTC-3, Fernando Javier Martin  
escribió:

> Hi there.
> 
> I have a "user" document which has an array of friends.  
> I need to add/delete friends from that array.
> 
> I'm currently being able to add or remove, but problem is that if a user  
> ID already exists gets added anyway.  
> I need to perform a check before actually adding it (desiderable in one  
> operation "if (not\_exists) add it" ).
> 
> This is my user object and mappings (and scripts).  
> [The mapping and the user object · GitHub](https://gist.github.com/fernandojmartin/5311012)
> 
> Thanks in advance.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Nabloom](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@Nabloom](https://discuss.elastic.co/u/Nabloom)\
**Post date:** [April 4, 2013, 11:02pm UTC](https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452/4 "2013-04-04T23:02:41Z")

</div>

Have you tried with a simple if-else:

{  
"script" : "if(ctx.\_source.friends.contains(friend)) {ctx.op = 'none'}  
else { ctx.\_source.friends += friend; ctx.\_source.friends\_qty =  
ctx.\_source.friends.size()}",  
"params" : { "friend" : "88" }  
}

it can by the way avoid to reindex the doc when there is a noop.

Nabloom

On Thursday, April 4, 2013 8:35:24 PM UTC+2, Fernando Javier Martin wrote:

> Thx Nabloom.
> 
> I've tried your suggestion with neither error or success.  
> This is the actual code:
> 
> {  
> "script" : "ctx.\_source.friends.contains(friend) ? ctx.op = 'none' :  
> ctx.\_source.friends += friend; ctx.\_source.friends\_qty =  
> ctx.\_source.friends.size()",  
> "params" : { "friend" : "88" }  
> }
> 
> Am I missing anything?  
> I've tried even removing the friend\_qty update operation, just in case the  
> semicolon was causing something unwanted (is that the right way to put  
> multiple statements?).  
> Version number gets incremented on every POST but no changes at all to the  
> document.
> 
> El jueves, 4 de abril de 2013 11:54:05 UTC-3, Fernando Javier Martin  
> escribió:
> 
> > Hi there.
> > 
> > I have a "user" document which has an array of friends.  
> > I need to add/delete friends from that array.
> > 
> > I'm currently being able to add or remove, but problem is that if a user  
> > ID already exists gets added anyway.  
> > I need to perform a check before actually adding it (desiderable in one  
> > operation "if (not\_exists) add it" ).
> > 
> > This is my user object and mappings (and scripts).  
> > [The mapping and the user object · GitHub](https://gist.github.com/fernandojmartin/5311012)
> > 
> > Thanks in advance.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Fernando\_Javier\_Mart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fernando_javier_mart/32/2358_2.png) [@Fernando\_Javier\_Mart](https://discuss.elastic.co/u/Fernando_Javier_Mart)\
**Post date:** [April 5, 2013, 6:09pm UTC](https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452/5 "2013-04-05T18:09:12Z")

</div>

Whooa!

It worked perfect!  
Thanks Nabloom!

BTW: Do(es) you(anyone) have a clue why the ternary operator version hadn't  
worked?

I'm completely new to ES (hence to mvel)  
he  
El jueves, 4 de abril de 2013 11:54:05 UTC-3, Fernando Javier Martin  
escribió:

> Hi there.
> 
> I have a "user" document which has an array of friends.  
> I need to add/delete friends from that array.
> 
> I'm currently being able to add or remove, but problem is that if a user  
> ID already exists gets added anyway.  
> I need to perform a check before actually adding it (desiderable in one  
> operation "if (not\_exists) add it" ).
> 
> This is my user object and mappings (and scripts).  
> [The mapping and the user object · GitHub](https://gist.github.com/fernandojmartin/5311012)
> 
> Thanks in advance.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:42am UTC](https://discuss.elastic.co/t/checking-if-value-exists-in-array-before-adding-it/11452/6 "2017-07-06T02:42:33Z")

</div>


