# Performance problem within add a new object to a nested array

**URL:** <https://discuss.elastic.co/t/performance-problem-within-add-a-new-object-to-a-nested-array/212195>\
**Category:** Elasticsearch\
**Created:** [December 17, 2019, 4:46pm UTC](https://discuss.elastic.co/t/performance-problem-within-add-a-new-object-to-a-nested-array/212195 "2019-12-17T16:46:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Oprya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_oprya/32/59569_2.png) [@Sergey\_Oprya](https://discuss.elastic.co/u/Sergey_Oprya)\
**Post date:** [December 17, 2019, 4:46pm UTC](https://discuss.elastic.co/t/performance-problem-within-add-a-new-object-to-a-nested-array/212195/1 "2019-12-17T16:46:20Z")

</div>

Hi Everyone,

I have a performance issue when I try to add object to a nested array field. If this array has ~ 0-500 objects performance is ok but when I add more and more the performance is going down quite fast. Eventually when I do bulk query to update 100 documents to add new object to the nested array field of these documents execution time of this query can be 10-40 seconds. Couldn't find a solution to solve this problem. If someone knows how to solve this I will be glad of any help with this.

My mapping:  
dynamic\_templates: [  
...  
{ activity: {  
match\_mapping\_type: '\*',  
match: 'activity',  
mapping: {  
type: 'nested',  
properties: {  
activityId: {  
type: 'keyword',  
},  
filterType: {  
type: 'keyword',  
},  
category: {  
type: 'keyword',  
},  
createdAt: {  
type: 'date',  
},  
text: {  
type: 'text',  
analyzer: 'custom\_english\_analyzer',  
}  
}  
}  
}}  
...  
]

My bulk operation:  
{"update": { "\_index": "my\_index", "\_id": 1111111 }}  
{"script": {"source": "if(ctx.\_source.activity != null) { ctx.\_source.activity.removeIf(activity -\> activity.activityId == params.activity.activityId) } else { ctx.\_source.activity = new ArrayList() } ctx.\_source.activity.add(params.activity)", "params": {"activity": {"activityId": "5dd2d60dd7ef16348aec8d9e","filterType": "FILTER\_TYPE","category": "CATEGORY","action": "ACTION","createdAt": "2019-01-01T00:00:00.000Z","text": "Searchable text"}}}}  
...

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 17, 2019, 5:40pm UTC](https://discuss.elastic.co/t/performance-problem-within-add-a-new-object-to-a-nested-array/212195/2 "2019-12-17T17:40:14Z")

</div>

Each nested instance within the document is a separate document in Elasticsearch and all need to be reindexed when a new nested object is added, which means that updates get more and more expensive with increased size. This is one of the main drawbacks with nested documents. If you are adding a lot of objects you may want to model your data differently.

---

<div class="post-metadata">

**Author:** ![Sergey\_Oprya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_oprya/32/59569_2.png) [@Sergey\_Oprya](https://discuss.elastic.co/u/Sergey_Oprya)\
**Post date:** [December 17, 2019, 6:01pm UTC](https://discuss.elastic.co/t/performance-problem-within-add-a-new-object-to-a-nested-array/212195/3 "2019-12-17T18:01:20Z")

</div>

Even when I try to keep this array not as a nested field I have this issue. It seems if an array just a field in main document Elasticsearch reindexing only main document, am I right?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 17, 2019, 6:28pm UTC](https://discuss.elastic.co/t/performance-problem-within-add-a-new-object-to-a-nested-array/212195/4 "2019-12-17T18:28:55Z")

</div>

Frequent updates to the same document can also be a cause of poor performance as it may lead to a lot of small refreshes that can be quite expensive. Every time a document is updated that has not been refreshed, a new refresh is triggered.

---

<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:** [January 14, 2020, 6:28pm UTC](https://discuss.elastic.co/t/performance-problem-within-add-a-new-object-to-a-nested-array/212195/5 "2020-01-14T18:28:55Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
