# Using script query for custom logic

**URL:** https://discuss.elastic.co/t/using-script-query-for-custom-logic/126172
**Category:** Elasticsearch
**Created:** [March 30, 2018, 1:45am UTC](https://discuss.elastic.co/t/using-script-query-for-custom-logic/126172 "2018-03-30T01:45:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vijay\_arora1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vijay_arora1/32/13789_2.png) [@Vijay\_arora1](https://discuss.elastic.co/u/Vijay_arora1)
#### Post date: [March 30, 2018, 1:45am UTC](https://discuss.elastic.co/t/using-script-query-for-custom-logic/126172/1 "2018-03-30T01:45:52Z")

</div>

I am using the current version of Elastic Search (6.2) and trying to see how i can use query script for a custom logic.  
This is how the index data looks like:  
indent preformatted text by 4 spaces  
ID : 1, 2, 3, 4, 5  
ResourceID : R1, R1, R1, R3, R4  
color: "Red", "Blue", "Blue", "Green", "Blue"

Now after I apply a terms filter on color, color = "Blue"  
I get documents with ID 2, 3, 5  
But now document 2 and 3 has the same ResourceID R1,  
So I want to get only one of those say Doc with ID 2,

Not sure how i would use the script query for this kind of thing.  
Based on the documentation and samples I tried this:

```
  "must" :{
      "script" : {
                "script" : {
                    "source": "doc['ResourceID'].value > 1",
                    "lang": "painless"
                 }
            }
     }

```

But this does not gives the expected result, not sure if there is something like distinct, or count to see if the count of similar ResourceID \> 1, get me only the first one.

Thanks

---

<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: [March 30, 2018, 5:47am UTC](https://discuss.elastic.co/t/using-script-query-for-custom-logic/126172/2 "2018-03-30T05:47:27Z")

</div>

There is no way to do this with a filter script, but have you looked at [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)?

---

<div class="post-metadata">

### Author: ![Vijay\_arora1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vijay_arora1/32/13789_2.png) [@Vijay\_arora1](https://discuss.elastic.co/u/Vijay_arora1)
#### Post date: [March 31, 2018, 1:32am UTC](https://discuss.elastic.co/t/using-script-query-for-custom-logic/126172/3 "2018-03-31T01:32:49Z")

</div>

Thanks Ryan for pointing me in the right direction, I am able to accomplish it using the Field Collapsing.

---

<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: [April 28, 2018, 1:32am UTC](https://discuss.elastic.co/t/using-script-query-for-custom-logic/126172/4 "2018-04-28T01:32:52Z")

</div>

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