# Impossible to use scripts in nested filters?

**URL:** <https://discuss.elastic.co/t/impossible-to-use-scripts-in-nested-filters/83994>\
**Category:** Elasticsearch\
**Created:** [April 28, 2017, 9:51am UTC](https://discuss.elastic.co/t/impossible-to-use-scripts-in-nested-filters/83994 "2017-04-28T09:51:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knights](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@knights](https://discuss.elastic.co/u/knights)\
**Post date:** [April 28, 2017, 9:51am UTC](https://discuss.elastic.co/t/impossible-to-use-scripts-in-nested-filters/83994/1 "2017-04-28T09:51:56Z")

</div>

Hello, everyone.

Currently using ES v1.7, I encounter the following behaviour :

Consider the following document structure, where products is a nested field

> id : "string",  
> products : [  
> "price" : long,  
> "quantity" : long  
> ]

I need to write a request that will bring back all documents where the value of my stock is supérior to a thresold, say 5000.

Thus, I write the following filter (using filter cause I don't care about scoring)

> "nested" : {  
> "filter" : {  
> "script" : {  
> "script" : "doc['products.price'].values.sum() \* doc['products.quantity'].values.sum()\>5000"  
> }  
> },  
> "path" : "products"  
> }

But it does not work.  
I really need the filter to be nested to avoid cross-matches .. Is there a proper way to proceed?

Thanks.

---

<div class="post-metadata">

**Author:** ![knights](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@knights](https://discuss.elastic.co/u/knights)\
**Post date:** [May 3, 2017, 2:14pm UTC](https://discuss.elastic.co/t/impossible-to-use-scripts-in-nested-filters/83994/2 "2017-05-03T14:14:20Z")

</div>

It's a tough question. Isn't there anyone that had to solve that roblem before?

---

<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:** [May 4, 2017, 3:02am UTC](https://discuss.elastic.co/t/impossible-to-use-scripts-in-nested-filters/83994/3 "2017-05-04T03:02:05Z")

</div>

Nested fields are separate documents in Lucene. So `doc` in one invocation of the script is just a single nested doc. There is no way to get access to all the nested docs at once.

---

<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:** [June 1, 2017, 3:16am UTC](https://discuss.elastic.co/t/impossible-to-use-scripts-in-nested-filters/83994/4 "2017-06-01T03:16:42Z")

</div>

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