# Is it possible to access to a different document by ID in script?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-access-to-a-different-document-by-id-in-script/152329>\
**Category:** Elasticsearch\
**Created:** [October 13, 2018, 12:37pm UTC](https://discuss.elastic.co/t/is-it-possible-to-access-to-a-different-document-by-id-in-script/152329 "2018-10-13T12:37:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yyz1989](https://avatars.discourse-cdn.com/v4/letter/y/aca169/32.png) [@yyz1989](https://discuss.elastic.co/u/yyz1989)\
**Post date:** [October 13, 2018, 12:37pm UTC](https://discuss.elastic.co/t/is-it-possible-to-access-to-a-different-document-by-id-in-script/152329/1 "2018-10-13T12:37:27Z")

</div>

Dear friends,

Assume I have three documents in three indexes referencing each other through some attributes:

```
{
  "_index": "a",
  "_id": "a:0",
  "_source": {
    "child": "b:0"
  }
}

{
  "_index": "b",
  "_id": "b:0",
  "_source": {
    "child": "c:0",
    "parent": "a:0"
  }
}

{
  "_index": "c",
  "_id": "c:0",
  "_source": {
    "parent": "b:0"
  }
}

```

I wanted to perform a query to get all documents from index c whose "grand parent" is document "a:0". Unfortunately I cannot find out the way to do it in one query, so I have to query for documents whose parent is "a:0" from index b, and then query for documents from index c with the previous result as a filter.

To improve performance, it seems I have to insert "grand parent" document IDs to the documents in index c as well, then I just need to query once. In fact my index is quite large already, so I would prefer to insert this new field into the documents instead of reindexing everything. To do this I need to access to both indexes b and c, so that for each document in c I get parent document ID, and access to this document through ID index b to get "grand parent" document ID. However I also didn't find any example from the Web moving towards this direction.

I am not sure if I have explained my problem clearly. Is such solution really possible?

Any help is highly appreciated.

Best regards,  
Yang

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 15, 2018, 10:57am UTC](https://discuss.elastic.co/t/is-it-possible-to-access-to-a-different-document-by-id-in-script/152329/2 "2018-10-15T10:57:18Z")

</div>

scripts cannot do further look ups to other documents. You would need to solve this issue at index time.

A look at the [join datatype](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/parent-join.html) might help, but please take your time to properly read the documentation about it, as it comes with some limitations.

--Alex

---

<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:** [November 12, 2018, 10:57am UTC](https://discuss.elastic.co/t/is-it-possible-to-access-to-a-different-document-by-id-in-script/152329/3 "2018-11-12T10:57:21Z")

</div>

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