steven johns
steven johns

Reputation: 487

Match documents where the field 1 is more than field 2

doc1 = {
    "field1": 34,
    "field2": 15
}
doc2 = {
    "field1": 12,
    "field2": 46
}

How to match all the documents where field 1 is greater that field 2. That is in the above case the search will result in the matching of doc1 only.

Upvotes: 0

Views: 175

Answers (1)

Val
Val

Reputation: 217324

You can use a script filter that can directly compare the fields.

{
  "query": {
    "filtered": {
      "filter": {
        "script": {
          "script": "doc.field1.value > doc.field2.value"
        }
      }
    }
  }
}

Note that in order for this to work you need to enable dynamic scripting.

Upvotes: 1

Related Questions