# Filtering on Script Value

**URL:** https://discuss.elastic.co/t/filtering-on-script-value/18315
**Category:** Elasticsearch
**Created:** [June 25, 2014, 2:36am UTC](https://discuss.elastic.co/t/filtering-on-script-value/18315 "2014-06-25T02:36:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bbehling](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@bbehling](https://discuss.elastic.co/u/bbehling)
#### Post date: [June 25, 2014, 2:36am UTC](https://discuss.elastic.co/t/filtering-on-script-value/18315/1 "2014-06-25T02:36:33Z")

</div>

I'm trying to calculate a value for each hit then select or filter on a  
calculated value. Something like below:

```
"query": {
    "match_all": {}
    },
    "script_fields" : {
        "counter" : {
            "script" : "count++",
            "params" : {
                "count" : 1
            }
        },
        "source" : {
          "script" : "_source"
    }
  }

```

I'd like to filter on the count parameter.

I've read on a StackOverflow post that you cannot filter on a script value.

So is there another way to calculate some value dynamically and filter on  
that value?

If not, is there a nested SQL SELECT equivalent in ElasticSearch? Maybe I  
could execute the first query to calculate the 'count' then execute another  
query to filter by a value?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8206ea84-b314-4b8e-8f3c-248d9f5a99e7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8206ea84-b314-4b8e-8f3c-248d9f5a99e7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Cedric\_Hourcade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cedric_hourcade/32/1451_2.png) [@Cedric\_Hourcade](https://discuss.elastic.co/u/Cedric_Hourcade)
#### Post date: [June 25, 2014, 6:02am UTC](https://discuss.elastic.co/t/filtering-on-script-value/18315/2 "2014-06-25T06:02:31Z")

</div>

Hello,

You should be able to filter with a script using the script filter:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Cédric Hourcade  
[ced@wal.fr](mailto:ced@wal.fr)

On Wed, Jun 25, 2014 at 4:36 AM, Brian Behling [brian.behling@gmail.com](mailto:brian.behling@gmail.com) wrote:

> I'm trying to calculate a value for each hit then select or filter on a  
> calculated value. Something like below:
> 
> ```
> "query": {
> "match_all": {}
> },
> "script_fields" : {
> "counter" : {
> "script" : "count++",
> "params" : {
> "count" : 1
> }
> },
> "source" : {
> "script" : "_source"
> }
> }
> 
> ```
> 
> I'd like to filter on the count parameter.
> 
> I've read on a StackOverflow post that you cannot filter on a script value.
> 
> So is there another way to calculate some value dynamically and filter on  
> that value?
> 
> If not, is there a nested SQL SELECT equivalent in Elasticsearch? Maybe I  
> could execute the first query to calculate the 'count' then execute another  
> query to filter by a value?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/8206ea84-b314-4b8e-8f3c-248d9f5a99e7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8206ea84-b314-4b8e-8f3c-248d9f5a99e7%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAJQxjPPE0dAt%2BoZr%3Dev9SpDwy-04fkWX3xdAmsLcXEL%3D6i6fRA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJQxjPPE0dAt%2BoZr%3Dev9SpDwy-04fkWX3xdAmsLcXEL%3D6i6fRA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![bbehling](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@bbehling](https://discuss.elastic.co/u/bbehling)
#### Post date: [June 25, 2014, 2:39pm UTC](https://discuss.elastic.co/t/filtering-on-script-value/18315/3 "2014-06-25T14:39:47Z")

</div>

I've tried that with this query:

GET leases\_allv2/\_search  
{  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"or": [  
{  
"term": {  
"states": "co"  
}  
}  
]  
},  
{

```
    "script" : {
        "script" : "count++; count <= 3",
        "params" : {
            "count" : 0
        }
    }
      }
    ]
  }
  
}

```

}  
}

But what happens with that query is since I have 5 shards on our database,  
I get 15 results back. So the counter variable is getting reset for each  
shard.

Is there a way to keep that counter from resetting when searching each  
shard?

On Wednesday, June 25, 2014 12:02:57 AM UTC-6, Cédric Hourcade wrote:

> Hello,
> 
> You should be able to filter with a script using the script filter:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-script-filter.html)
> 
> Cédric Hourcade  
> [c...@wal.fr](mailto:c...@wal.fr) \<javascript:\>
> 
> On Wed, Jun 25, 2014 at 4:36 AM, Brian Behling \<[brian....@gmail.com](mailto:brian....@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > I'm trying to calculate a value for each hit then select or filter on a  
> > calculated value. Something like below:
> > 
> > ```
> > "query": { 
> > "match_all": {} 
> > }, 
> > "script_fields" : { 
> > "counter" : { 
> > "script" : "count++", 
> > "params" : { 
> > "count" : 1 
> > } 
> > }, 
> > "source" : { 
> > "script" : "_source" 
> > } 
> > } 
> > 
> > ```
> > 
> > I'd like to filter on the count parameter.
> > 
> > I've read on a StackOverflow post that you cannot filter on a script  
> > value.
> > 
> > So is there another way to calculate some value dynamically and filter  
> > on  
> > that value?
> > 
> > If not, is there a nested SQL SELECT equivalent in Elasticsearch? Maybe  
> > I  
> > could execute the first query to calculate the 'count' then execute  
> > another  
> > query to filter by a value?
> > 
> > --  
> > You received this message because you are subscribed to the Google  
> > Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send  
> > an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit
> 
> [https://groups.google.com/d/msgid/elasticsearch/8206ea84-b314-4b8e-8f3c-248d9f5a99e7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8206ea84-b314-4b8e-8f3c-248d9f5a99e7%40googlegroups.com).
> 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/def2deb6-9bdc-4fb2-a1ad-b26832a725c6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/def2deb6-9bdc-4fb2-a1ad-b26832a725c6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:19am UTC](https://discuss.elastic.co/t/filtering-on-script-value/18315/4 "2017-07-06T01:19:50Z")

</div>


