Script filter in Elasticsearch
For an es type ItemTransaction of following mapping, lets query documents having paidDate one day ahead of orderDate.
Es Type Mapping
{
  "ItemTransaction": {
    "properties": {
      orderDate: {
        type: "date",
        format: "dateOptionalTime"
      },
      paidDate: {
        type: "date",
        format: "dateOptionalTime"
      },
      customerId: {
        type: "string",
        index: "not_analyzed",
        omit_norms: true,
        index_options: "docs"
      },
      paidAmount: {
        type: "long"
      }
    }
  }
}
Query
The query using script filter would be as below,
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "and": {
          "filters": [
            {
              "script": {
                "script": "doc['orderDate'].value > doc['paidDate'].value+86400000"
              }
            }
          ]
        }
      }
    }
  },
  "fields": [
    "cutomerId",
    "orderDate",
    "paidDate",
    "paidAmount"
  ]
}
References
Written by Prayag Upd
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
 #Elasticsearch 
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#