# Painless: access params.\_source in script filter in 6.1

**URL:** <https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449>\
**Category:** Elasticsearch\
**Created:** [January 14, 2018, 4:25pm UTC](https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449 "2018-01-14T16:25:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahtuag](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mahtuag](https://discuss.elastic.co/u/mahtuag)\
**Post date:** [January 14, 2018, 4:25pm UTC](https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449/1 "2018-01-14T16:25:38Z")

</div>

Within painless scripts I use for script queries (in a filter context), sort scripts and script fields, I need to refer to the JSON structure of the source document, which is fairly complex and has several arrays of objects within its structure (stored as nested fields).

The queries I write are analytics-heavy so performance isn't the _most_ important thing.

To refer to the document source, I'm able to refer to `params._source` within painless when I'm writing a script field or a sort script. But this `params._source` field is missing when I'm writing a script query.

Is there a way around this? I cannot do `doc['path.to.field'].value` when `path.to` is a nested object, and I need to run a for loop through the objects in `path.to`.

---

<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:** [January 15, 2018, 6:26pm UTC](https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449/2 "2018-01-15T18:26:43Z")

</div>

Accessing source is _very_ slow. I would think with analytics performance would be very important. Regardless, it was never intentional to allow access to source from search scripts in painless. It was simply a side effect of giving access for script fields, which we are now correcting with the separation of script contexts.

What kind of calculation are you trying to do over the nested docs? Have you tried a nested query? You can do `sum`, `min` or `max` currently.

---

<div class="post-metadata">

**Author:** ![mahtuag](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mahtuag](https://discuss.elastic.co/u/mahtuag)\
**Post date:** [January 15, 2018, 7:09pm UTC](https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449/3 "2018-01-15T19:09:39Z")

</div>

I don't have that many documents for the speed to have that much of an  
impact. I'm happy with the speed at which my sort filter is running and it  
uses `params._source`.

The documents themselves are fat though. The JSON file of a single document  
with indentation is around 32kb. My mapping was automatically generated by  
observing the structure of a document - each time I have an array of  
objects I treat it as a nested object whole everything else is a regular  
object. All other fields are keywords, booleans or numerics.

The reason I need access is that I sometimes need to search an array of  
objects and look for an entry which has a key containing a specific value  
and then read another key's value in that entry and use it in some numeric  
computations. Other times I need to selectively apply a different filter  
depending on what value a specific field contains.

I suppose my biggest problem is that I am running queries on raw data that  
hasn't been processed by hand for optimal query performance so I need the  
queries to be very flexible.

Without examining the base document structure this would be really hard.

Is there some way of iterating through nested field objects with regular  
queries?

---

<div class="post-metadata">

**Author:** ![waitany](https://avatars.discourse-cdn.com/v4/letter/w/a6a055/32.png) [@waitany](https://discuss.elastic.co/u/waitany)\
**Post date:** [January 22, 2018, 7:21am UTC](https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449/4 "2018-01-22T07:21:40Z")

</div>

The same question, how did you solve it? thanks

---

<div class="post-metadata">

**Author:** ![mahtuag](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mahtuag](https://discuss.elastic.co/u/mahtuag)\
**Post date:** [January 22, 2018, 5:22pm UTC](https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449/5 "2018-01-22T17:22:07Z")

</div>

I'm using a script filter within a nested query. The result is less readable but works for my specific use-case. If the use-case becomes more complex and I absolutely need `_source`, I'll start worrying about how to do it then. Right now my ideas are along the lines of implementing field double indexing (store the same field twice, once as nested object and another as a regular object) in some form when the need arises.

---

<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:** [February 19, 2018, 5:22pm UTC](https://discuss.elastic.co/t/painless-access-params-source-in-script-filter-in-6-1/115449/6 "2018-02-19T17:22:09Z")

</div>

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