0

Im running this script to calculate the distances between geo points based on groovy script.

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "xxxxxx",
          "analyze_wildcard": true
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "@timestamp": {
                  "gte": "2012-03-01 00:00:00.000",
                  "lte": "2012-03-31 23:59:59.999",
                  "format": "yyyy-MM-dd HH:mm:ss.SSS"
                }
              }
            }
          ],
          "must_not": []
        }
      }
    }
  },
  "aggs": {
    "by_day": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "day"
      },
      "aggs": {
        "gpoints": {
          "scripted_metric": {
            "init_script": "_agg['points'] = [];",
            "map_script": " _agg['points'] = doc['geoip.location'];",
            "reduce_script": "diff = 0;prev_p = 0;count = 0;for(p in _aggs){if(count == 0){prev_p = p.points; count = 1;} else{diff=prev_p[0].distanceInKm(p.points[0].getLat(),p.points[0].getLon()); prev_p = p.points;};}; return diff;"
          }
        }
      }
    }
  }
}

but i got the following error:

{
   "error": {
      "root_cause": [],
      "type": "reduce_search_phase_exception",
      "reason": "[reduce] ",
      "phase": "fetch",
      "grouped": true,
      "failed_shards": [],
      "caused_by": {
         "type": "script_exception",
         "reason": "failed to run inline script [diff = 0;prev_p = 0;count = 0;for(p in _aggs){if(count == 0){prev_p = p.points; count = 1;} else{diff=prev_p[0].distanceInKm(p.points[0].getLat(),p.points[0].getLon()); prev_p = p.points;};}; return diff;] using lang [groovy]",
         "caused_by": {
            "type": "missing_method_exception",
            "reason": "No signature of method: org.elasticsearch.common.geo.GeoPoint.distanceInKm() is applicable for argument types: (java.lang.Double, java.lang.Double) values: [28.233599960803986, -82.18120012432337]"
         }
      }
   },
   "status": 503
}

is there away to fix this code? Or, can anyone suggest away to find the difference between geo points and detect if someone was moving or not during a day?. Also, I want to know how far he was moving?

  • Try doing only `prev_p.distanceInKm(p.points[0].getLat(),p.points[0].getLon())` (without `[0]` as ES will take the first geo point value by itself). – Andrei Stefan May 06 '16 at 09:33
  • I tried but its not working. I think the problem is that i have a point of type GeoPoint, which does not have distanceInKm() function. so the question is how to convert it to appropriate object that can use this function? – Issam AlZinati May 06 '16 at 16:00
  • similar to this question: http://stackoverflow.com/questions/32213624/elasticsearch-missingmethodexception-on-running-distanceinkm-on-geo-point-arra – Renato May 07 '16 at 16:45

0 Answers0