-7e19w
Last Updated: January 16, 2018
·
3.398K
· prayagupd

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

script filter

Script in elasticsearch