# Adding / Removing entries from an array

**URL:** https://discuss.elastic.co/t/adding-removing-entries-from-an-array/11002
**Category:** Elasticsearch
**Created:** [March 4, 2013, 6:20pm UTC](https://discuss.elastic.co/t/adding-removing-entries-from-an-array/11002 "2013-03-04T18:20:21Z")
**Posts on this page:** 4
**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: [March 4, 2013, 6:20pm UTC](https://discuss.elastic.co/t/adding-removing-entries-from-an-array/11002/1 "2013-03-04T18:20:21Z")

</div>

Hi there.  
It's my first time with ES so sorry if I make a dumb question.

\*Question: \*  
I'm trying to remove an object from an array.

_Why?:_  
We plan to use ES like some sort of DB for fast retrieval, instead of  
hitting MySQL.  
Our RDBMS we'll keep being our datastore.

_Background:_  
The project I'm in is a social network so I'm planning to use ES to store  
via API "user" objects for fast store and retrieval (today MySQL is nor  
performing very well)  
We'll be doing around 350 GET reqs/sec and around 150 POST()insert/updates)  
reqs/sec.

The proposed user object structure is like this:  
{

- "\_index":"session",
- "\_type":"user",
- "\_id":"320292",
- "\_version":4,
- "exists":true,
- "\_source":{
  - "iduser":320292,
  - "username":"FAKEPROFILE",
  - "avatar":"userfiles/uf214/320292/avatar",
  - "friends":[
    - 43,
    - 51,
    - 54,
    - 55,
    - 66,
    - 69,
    - 285,
    - 893978,
    - 1003406,
    - 666  
],

  - "friends\_qty":9  
}

}

I've successfully added USER ID 666 with the syntax at Docs page:  
{  
"script" : "ctx.\_source.friends += friend",  
"params" : {  
"friend" : 666  
}  
}

Problem is that I tried "script" : "ctx.\_source.friends _-=_ friend" without  
success.

I'm trying to update record in place since some users have a very long list  
of friends that could become about 16Mb-length string.  
I plan to populate once, then keep updated.

If any of you have also any input/advice on any other topic of the proposed  
idea don't hesitate to add it.

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: ![Paco\_Guzman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paco_guzman/32/2460_2.png) [@Paco\_Guzman](https://discuss.elastic.co/u/Paco_Guzman)
#### Post date: [March 4, 2013, 6:50pm UTC](https://discuss.elastic.co/t/adding-removing-entries-from-an-array/11002/2 "2013-03-04T18:50:45Z")

</div>

Hi,

I think you have to use some of the [1]Projects and Folds of MVEL (the  
default scripting language). I did some time ago but I did not remember  
exactly, sorry

[1] - [http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds](http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds)

On Mon, Mar 4, 2013 at 7:20 PM, Fernando Javier Martin \<  
[fernandojmartin@gmail.com](mailto:fernandojmartin@gmail.com)\> wrote:

> Hi there.  
> It's my first time with ES so sorry if I make a dumb question.
> 
> \*Question: \*  
> I'm trying to remove an object from an array.
> 
> _Why?:_  
> We plan to use ES like some sort of DB for fast retrieval, instead of  
> hitting MySQL.  
> Our RDBMS we'll keep being our datastore.
> 
> _Background:_  
> The project I'm in is a social network so I'm planning to use ES to store  
> via API "user" objects for fast store and retrieval (today MySQL is nor  
> performing very well)  
> We'll be doing around 350 GET reqs/sec and around 150  
> POST()insert/updates) reqs/sec.
> 
> The proposed user object structure is like this:  
> {
> 
> - "\_index":"session",
> - "\_type":"user",
> - "\_id":"320292",
> - "\_version":4,
> - "exists":true,
> - "\_source":{
> - "iduser":320292,
> - "username":"FAKEPROFILE",
> - "avatar":"userfiles/uf214/320292/avatar",
> - "friends":[
> - 43,
> - 51,
> - 54,
> - 55,
> - 66,
> - 69,
> - 285,
> - 893978,
> - 1003406,
> - 666  
> ],
> 
> - "friends\_qty":9  
> }
> 
> }
> 
> I've successfully added USER ID 666 with the syntax at Docs page:  
> {  
> "script" : "ctx.\_source.friends += friend",  
> "params" : {  
> "friend" : 666  
> }  
> }
> 
> Problem is that I tried "script" : "ctx.\_source.friends _-=_ friend" without  
> success.
> 
> I'm trying to update record in place since some users have a very long  
> list of friends that could become about 16Mb-length string.  
> I plan to populate once, then keep updated.
> 
> If any of you have also any input/advice on any other topic of the  
> proposed idea don't hesitate to add it.
> 
> 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).

--  
Paco Guzmán  
e-mail: [pacoguzmanp@gmail.com](mailto:pacoguzmanp@gmail.com)  
blog: [pacoguzman.lacoctelera.net/](http://pacoguzman.lacoctelera.net/)

--  
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: [March 5, 2013, 6:56pm UTC](https://discuss.elastic.co/t/adding-removing-entries-from-an-array/11002/3 "2013-03-05T18:56:26Z")

</div>

Thanks Paco!

I've dived into MVEL docs and found this syntax to work:

// Adding  
{  
"script" : "ctx.\_source.friends += friend; ctx.\_source.friends\_qty += 1",  
"params" : { "friend" : "299" }  
}

// Removing  
{  
"script" : "ctx.\_source.friends.remove("199"); ctx.\_source.friends\_qty -=  
1"  
}

However decreasing the counter in this way doesn't works (this may be my  
next question).  
I was unable to get the property size() too.  
Anyway, I've managed to add/remove elements from a List.

Thanks!

El lunes, 4 de marzo de 2013 15:50:45 UTC-3, Paco Guzmán escribió:

> Hi,
> 
> I think you have to use some of the [1]Projects and Folds of MVEL (the  
> default scripting language). I did some time ago but I did not remember  
> exactly, sorry
> 
> [1] - [http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds](http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds)
> 
> On Mon, Mar 4, 2013 at 7:20 PM, Fernando Javier Martin \<  
> [fernand...@gmail.com](mailto:fernand...@gmail.com) \<javascript:\>\> wrote:
> 
> > Hi there.  
> > It's my first time with ES so sorry if I make a dumb question.
> > 
> > \*Question: \*  
> > I'm trying to remove an object from an array.
> > 
> > _Why?:_  
> > We plan to use ES like some sort of DB for fast retrieval, instead of  
> > hitting MySQL.  
> > Our RDBMS we'll keep being our datastore.
> > 
> > _Background:_  
> > The project I'm in is a social network so I'm planning to use ES to store  
> > via API "user" objects for fast store and retrieval (today MySQL is nor  
> > performing very well)  
> > We'll be doing around 350 GET reqs/sec and around 150  
> > POST()insert/updates) reqs/sec.
> > 
> > The proposed user object structure is like this:  
> > {
> > 
> > - "\_index":"session",
> > - "\_type":"user",
> > - "\_id":"320292",
> > - "\_version":4,
> > - "exists":true,
> > - "\_source":{
> > - "iduser":320292,
> > - "username":"FAKEPROFILE",
> > - "avatar":"userfiles/uf214/320292/avatar",
> > - "friends":[
> > - 43,
> > - 51,
> > - 54,
> > - 55,
> > - 66,
> > - 69,
> > - 285,
> > - 893978,
> > - 1003406,
> > - 666  
> > ],
> > 
> > - "friends\_qty":9  
> > }
> > 
> > }
> > 
> > I've successfully added USER ID 666 with the syntax at Docs page:  
> > {  
> > "script" : "ctx.\_source.friends += friend",  
> > "params" : {  
> > "friend" : 666  
> > }  
> > }
> > 
> > Problem is that I tried "script" : "ctx.\_source.friends _-=_ friend" without  
> > success.
> > 
> > I'm trying to update record in place since some users have a very long  
> > list of friends that could become about 16Mb-length string.  
> > I plan to populate once, then keep updated.
> > 
> > If any of you have also any input/advice on any other topic of the  
> > proposed idea don't hesitate to add it.
> > 
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Paco Guzmán  
> e-mail: [pacog...@gmail.com](mailto:pacog...@gmail.com) \<javascript:\>  
> blog: [pacoguzman.lacoctelera.net/](http://pacoguzman.lacoctelera.net/)

--  
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:48am UTC](https://discuss.elastic.co/t/adding-removing-entries-from-an-array/11002/4 "2017-07-06T02:48:14Z")

</div>


