# Scripted filter with fields from the top AND the nested document

**URL:** <https://discuss.elastic.co/t/scripted-filter-with-fields-from-the-top-and-the-nested-document/186024>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 17, 2019, 9:11am UTC](https://discuss.elastic.co/t/scripted-filter-with-fields-from-the-top-and-the-nested-document/186024 "2019-06-17T09:11:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuan\_Hong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuan_hong/32/47234_2.png) [@Yuan\_Hong](https://discuss.elastic.co/u/Yuan_Hong)\
**Post date:** [June 17, 2019, 9:11am UTC](https://discuss.elastic.co/t/scripted-filter-with-fields-from-the-top-and-the-nested-document/186024/1 "2019-06-17T09:11:55Z")

</div>

Hi,

Suppose I have an `order` index with the following mapping:

```
{
  "leadTime": 2,
  "shipments": [{
    "packageId": 1,
    "shipped": "2019-06-10",
    "received": null
  }, {
    "packageId": 2,
    "shipped": "2019-06-08",
    "received": null
  }]
}

```

Now I want to answer the question:

_Which packages are shipped before now - leadTime days but not yet received as of now?_

The `leadTime` varies for different orders.

Now I find myself stuck between two dead ends:

- If I try to use a script filter on the nested `shipments` document, the script won't have access the `leadTime` field on the top document.

- If I try to put the script filter on the outer query, the script can not access the nested shipments document since `doc['shipments']` is missing.

Is there any way for a script in the filter context to access both the top document fields **AND** the fields from the nested document?

Thanks for any advice.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [June 20, 2019, 11:10pm UTC](https://discuss.elastic.co/t/scripted-filter-with-fields-from-the-top-and-the-nested-document/186024/2 "2019-06-20T23:10:31Z")

</div>

There isn't a way, since filter scripts operate on each individual document within lucene, and the parent document, as well as each inner object, are all separate documents (to lucene).

One way to handle this is to add the `leadTime` field to all your nested documents. You could do this when building the document for indexing, or in an ingest pipeline.

---

<div class="post-metadata">

**Author:** ![Yuan\_Hong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuan_hong/32/47234_2.png) [@Yuan\_Hong](https://discuss.elastic.co/u/Yuan_Hong)\
**Post date:** [June 21, 2019, 4:19am UTC](https://discuss.elastic.co/t/scripted-filter-with-fields-from-the-top-and-the-nested-document/186024/3 "2019-06-21T04:19:49Z")

</div>

Thanks for the clear explanation.

Now I understand that a workaround has to be implemented on the application level to ensure all fields used in a query be available on the same lucene document.

---

<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 19, 2019, 4:19am UTC](https://discuss.elastic.co/t/scripted-filter-with-fields-from-the-top-and-the-nested-document/186024/4 "2019-07-19T04:19:52Z")

</div>

Closed on July 19, 2019, 4:19am UTC

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