Using script query for custom logic

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.


There is no way to do this with a filter script, but have you looked at field collapsing?

1 Like

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

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