title: How to do Date comparisons in elastic search script?
body: <p>I'm try to write a update script that will update the record only if the date of the updatedAt field is greater than the updatedAt date on the source record.</p>
  
  <p>This is the script that I am using, don't mind the $ and {} they are string literal syntax in javascript.</p>
  
  <p>The script uses painless.</p>
  
  <pre><code>`ctx.op = ${moment(updatedAt).unix()} &gt; ctx._source["updatedAt"] ? "update" : "none";`
  </code></pre>
  
  <p>The mapping for the field is Date with no specified format. When I try to do this query I get this error:</p>
  
  <pre><code>{
    "took": 7,
    "errors": true,
    "items": [
      {
        "update": {
          "_index": "search",
          "_type": "locations",
          "_id": "119",
          "status": 400,
          "error": {
            "type": "illegal_argument_exception",
            "reason": "failed to execute script",
            "caused_by": {
              "type": "script_exception",
              "reason": "runtime error",
              "caused_by": {
                "type": "class_cast_exception",
                "reason": "Cannot apply [&gt;] operation to types [java.lang.Integer] and [java.lang.String]."
              },
              "script_stack": [
                "ctx.op = 1485985330 &gt; ctx._source[\"updatedAt\"] ? \"update\" : \"none\";",
                "                                 ^---- HERE"
              ],
              "script": "ctx.op = 1485985330 &gt; ctx._source[\"updatedAt\"] ? \"update\" : \"none\";",
              "lang": "painless"
            }
          }
        }
      }
    ]
  }
  </code></pre>