# Elastic script to update document based on documents present in other index

**URL:** <https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 12, 2024, 5:31pm UTC](https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313 "2024-04-12T17:31:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 12, 2024, 5:31pm UTC](https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313/1 "2024-04-12T17:31:26Z")

</div>

Hi Team,

I want to update document based on other index document.

I am trying something like below but I am getting "illegal\_argument\_exception" error. I am wondering is it possible or I am missing something?

POST test-transaction/\_update/2  
{  
"scripted\_upsert": true,  
"script": {  
"source": """  
def eventDoc = ctx.client.get({  
index: 'test-event',  
id: '3'  
});  
def eventStatus = eventDoc['\_source']['eventStatus'];

```
  ctx._source.status = eventStatus;
""",
"lang": "painless"

```

}  
}

I tried below query too but getting same error-

POST test-transaction/\_update/2  
{  
"scripted\_upsert": true,  
"script": {  
"source": """  
// Fetch the document from the other index  
def txnDoc = ctx.\_source;  
def status = txnDoc['status'];  
def evtDoc = ctx.\_index['test-event'].get('3');

```
""",
"lang": "painless"

```

}  
}

---

<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:** [April 12, 2024, 5:45pm UTC](https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313/2 "2024-04-12T17:45:02Z")

</div>

A script only has access to the current document and can not lookup documents in other indices. The only way you _may_ be able to do this would be through an ingest pipeline with an [enrich processor](https://www.elastic.co/guide/en/elasticsearch/reference/8.13/enrich-processor.html).

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 13, 2024, 9:08am UTC](https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313/3 "2024-04-13T09:08:03Z")

</div>

Thank you @Christian_Dahlqvist

I am able to achieve above problem statement via enrich documents.  
Just need one more help currently I am having below setup -

**Policy**  
PUT /\_enrich/policy/event-policy  
{  
"match": {  
"indices": "test-event",  
"match\_field": "correlationId",  
"enrich\_fields": ["eventStatus"],  
"query": {  
"match": {  
"eventStatus": "Delivered"  
}  
}  
}  
}

**Pipeline:**  
PUT /\_ingest/pipeline/event\_lookup  
{  
"processors" : [  
{  
"enrich" : {  
"description": "Add 'eventStatus' data based on 'correlation'",  
"policy\_name": "event-policy",  
"field" : "correlationId",  
"target\_field": "status",  
"max\_matches": "1",  
"override": true  
}  
}  
]  
}

**Incoming document:**  
POST test-transaction/\_doc/11?pipeline=event\_lookup  
{  
"status": "Initiate",  
"payload": "data",  
"correlationId": "123"  
}

And after applying pipeline incoming document will enrich something like below:  
"\_source" : {  
"payload" : "data",  
"correlationId" : "123",  
"status" : {  
"correlationId" : "123",  
"eventStatus" : "Delivered"  
}  
}

But I want something like below:  
"\_source" : {  
"payload" : "data",  
"correlationId" : "123",  
"status" : "Delivered"  
}

Please let me know how I can achieve this?

---

<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:** [April 13, 2024, 9:43am UTC](https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313/4 "2024-04-13T09:43:49Z")

</div>

Am I correctly understanding that you want an exect term match on the `correlationId` but only consider documents that have the `eventStatus` field set to "Delivered"?

I do not think the API allows this (am not sure though), but you might want to add [an alias with a filter clause](https://www.elastic.co/guide/en/elasticsearch/reference/8.13/indices-add-alias.html#indices-add-alias) on top of your indices and see if you can run the enrich policy based on this instead of directly linking to the indices. Have never done this so am not sure it will work.

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 13, 2024, 9:47am UTC](https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313/5 "2024-04-13T09:47:35Z")

</div>

Thanks @Christian_Dahlqvist m able to achieve that, I edited my questions. Please provide your suggestion on that.

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 13, 2024, 11:01am UTC](https://discuss.elastic.co/t/elastic-script-to-update-document-based-on-documents-present-in-other-index/357313/6 "2024-04-13T11:01:39Z")

</div>

Never mind, I found the solution via set processor.  
Thanks a lot for all your help.
