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

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).

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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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.
For more options, visit https://groups.google.com/groups/opt_out.