# Range with script fields

**URL:** <https://discuss.elastic.co/t/range-with-script-fields/326487>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 25, 2023, 7:41am UTC](https://discuss.elastic.co/t/range-with-script-fields/326487 "2023-02-25T07:41:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TECHNOID](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/technoid/32/117747_2.png) [@TECHNOID](https://discuss.elastic.co/u/TECHNOID)\
**Post date:** [February 25, 2023, 7:41am UTC](https://discuss.elastic.co/t/range-with-script-fields/326487/1 "2023-02-25T07:41:50Z")

</div>

Hello, tell me if it's possible to filter aggregation by scriptfield range

search result

```auto
    [hits] => Array
        (
...
            [hits] => Array
                (
                    [0] => Array
                        (
                            ...
                            [fields] => Array
                                (
                                    [price] => Array
                                        (
                                            [0] => 144360
                                        )
                                )
                        )
                )
        )

```

my aggrigation (filtering occurs by the price field in \_source)

```auto
Array
(
    [filters] => Array
        (
            [filters] => Array
                (
                    [brand:1] => Array
                        (
                            [bool] => Array
                                (
                                    [filter] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [range] => Array
                                                        (
                                                            [price] => Array <--- filtered by price in _source
                                                                (
                                                                    [gt] => 526
                                                                    [lt] => 22032
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )

```

Script field

```auto
'price' => [
                'script' => [
                    "lang" => "painless",
                  //this is to measure that the document may or may not be subject to currency
                    'source' => "emit(doc['currency'].empty ? doc['price'].value * params.currency : doc['price'].value)", 
                    "params" => [
                        "currency" => 40
                    ]
                ],
            ],
        ]

```

In the \_source, I also have a price field, but the filtering takes place exactly on it, How to make range work with a scriptfield.

The final task is to dynamically impose the exchange rate on the price and filter the price taking into account the exchange rate.

Can anyone help or tell me what I'm doing wrong?

---

<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:** [March 25, 2023, 7:42am UTC](https://discuss.elastic.co/t/range-with-script-fields/326487/2 "2023-03-25T07:42:25Z")

</div>

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