0

I have couple of date fields in my type . I am trying to run a script on those date fields find the duration between 2 dates.

Script takes a date field as String and gives No such property: millis for class. i beleive Elastic Search stores in the date in epoch millis.

Here is the script i am trying

ctx._source.duraton = ctx._source.sessionTerminationDateTime.value.millis - ctx._source.eventConversionDateTime.value.millis

Here is my mapping

 "access-event-logs": {
    "mappings": {
      "session-summary": {
        "dynamic_templates": [
          {
            "long_1": {
              "mapping": {
                "type": "long"
              },
              "match": "generation"
            }
          },
          {
            "datetime_1": {
              "mapping": {
                "format": "strict_date_optional_time||epoch_millis",
                "type": "date"
              },
              "match": "*DateTime"
            }
          },
          {
            "string_1": {
              "mapping": {
                "index": "not_analyzed",
                "type": "string"
              },
              "match": "*"
            }
          }
        ],
        "properties": {
          "eventConversionDateTime": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },
          "generation": {
            "type": "long"
          },
          "hostname": {
            "type": "string",
            "index": "not_analyzed"
          },
          "sessionKey": {
            "type": "string",
            "index": "not_analyzed",
            "include_in_all": false
          },
          "sessionTerminationDateTime": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },

        }
      }
    }
  }
}

How can i get the duration in millis between 2 date fields

doc['eventConversionDateTime'].date.getMillis(). 
{
"_index":"access-event-logs",
"_type":"session-summary",
"_id":"c2de4a9dkarabip1new.lab.fp.f5net.com",
"_version":1,
"found":true,
"_source":{
"sessionKey":"c2de4a9dkarabip1new.lab.fp.f5net.com",
"eventConversionDateTime":"2016-01-06T16:08:43.047-08:00",
"badIpReputation":false,
"virtualServer":"/Common/access_virtual",
"lastUpdateMicros":0
}
}
kavi
  • 172
  • 1
  • 14

1 Answers1

0

You can convert the string to date, This worked for me

POST /access-event-logs/session-summary/1/_update
{
  "script": "ctx._source.duration=new Date().parse(\"yyyy-MM-dd HH:mm:ss.SSS\",ctx._source.eventConversionDateTime.replace(\"T\", \" \").substring(0,23)).getTime();"
}

Hope this helps!!

ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
  • I am not able to access fields with doc. I tried to run above script and keeping getting this error.Does this have to do anything with ' not recognized . "}],\"type\":\"illegal_argument_exception\",\"reason\":\"failed to execute script\",\"caused_by\":{\"type\":\"groovy_script_execution_exception\",\"reason\":\"failed to run file script [session-duration-script] using lang [groovy]\",\"caused_by\":{\"type\":\"missing_property_exception\",\"reason\":\"No such property: doc for class: a85796c5a6014f123733479c767131bae34307c5\"}}},\"status\":400}", "referer": "172.17.86.89", – Karthikeyan Ramasamy Jan 06 '16 at 22:05
  • could you share one of your sample documents? do all of your documents have both fields? – ChintanShah25 Jan 06 '16 at 22:40
  • Currently i am trying with only one field. ctx._source.duraton = doc['eventConversionDateTime'].date.getMillis() . { "_index": "access-event-logs", "_type": "session-summary", "_id": "c2de4a9dkarabip1new.lab.fp.f5net.com", "_version": 1, "found": true, "_source": { "sessionKey": "c2de4a9dkarabip1new.lab.fp.f5net.com", "eventConversionDateTime": "2016-01-06T16:08:43.047-08:00", "badIpReputation": false, "virtualServer": "/Common/access_virtual", "lastUpdateMicros": 0 } } – Karthikeyan Ramasamy Jan 07 '16 at 00:11
  • I have updated my answer, even I am getting `no such property` with `doc values`, I am not sure if you could combine `_source` with `doc`.Also please put you sample document,error in question and not in comment section. – ChintanShah25 Jan 07 '16 at 03:23
  • my bad, can't use doc while updating as you cant access it, sorry about that :P – ChintanShah25 Jan 07 '16 at 03:46
  • 1
    although I have answered that question, I dont think it is a good idea to post link to different question like this. SO is a huge community, somebody will surely look into your questions and will try to help you. – ChintanShah25 Jan 08 '16 at 02:39