# Using distanceInMiles to find distance fails

**URL:** https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232
**Category:** Elasticsearch
**Created:** [October 28, 2016, 4:20am UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232 "2016-10-28T04:20:47Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 28, 2016, 4:20am UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/1 "2016-10-28T04:20:47Z")

</div>

Using ES 2.3.3  
A section of my document mapping is as follows :

```
"rents": {
  "type": "nested",
  "properties": {.........e.g. "avg"}
},
"location": {
    "type": "geo_point"
}

```

And in groovy script, I try to find distance of this field

```
 {
  "nested": {
    "path": "rents",
    "query": {
      "function_score": {
        "query": {...some rents queries....},
        "functions": [
          { 
            "script_score": {
              "params": {
                "center_lat": 23.509518,
                "center_long":-18.378842
              },
              "script": "return doc['location'].distanceInMiles(center_lat, center_long)*rents.avg;"
            }
          }
        ]
      }
    }
  }
}

```

but this keeps throwing error:

```
"reason": {
  "type": "script_exception",
  "reason": "failed to run inline script [return doc['location'].distanceInMiles(center_lat, center_long)*rents.avg;] using lang [groovy]",
  "caused_by": {
    "type": "null_pointer_exception",
    "reason": null
  }
}

```

I am following the guide [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html#\_document\_fields](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html#_document_fields) then what am I doing wrong? Please note all my documents have `location` field, none of them are null or empty. It seems `doc` is not accessible here

---

<div class="post-metadata">

### Author: ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)
#### Post date: [October 28, 2016, 3:21pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/2 "2016-10-28T15:21:52Z")

</div>

You're looking at a Lucene Expression example ([here](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html#_lucene_expressions_scripts)), not Groovy.  
Also note the `rents.avg` part of the expression.

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 28, 2016, 3:30pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/3 "2016-10-28T15:30:36Z")

</div>

@ywelsch What is the solution you suggest?

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 28, 2016, 3:32pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/4 "2016-10-28T15:32:42Z")

</div>

And fyi, even if I remove `*rents.avg` that doesn't help. Somehow it cannot get access to `doc`, is it because I have `path: rents` at the top?

---

<div class="post-metadata">

### Author: ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)
#### Post date: [October 28, 2016, 4:07pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/5 "2016-10-28T16:07:31Z")

</div>

Is there a stack trace in the logs?

can you try using [\_source](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html#_source_field) to access the field?

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 28, 2016, 4:18pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/6 "2016-10-28T16:18:16Z")

</div>

I don't want to use `_source` as it will be much slower than `doc`

---

<div class="post-metadata">

### Author: ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)
#### Post date: [October 28, 2016, 4:31pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/7 "2016-10-28T16:31:35Z")

</div>

can you at least test if it works with \_source?

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 28, 2016, 4:40pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/8 "2016-10-28T16:40:23Z")

</div>

I tried with `_source` and even that did not work ☹ also I removed `*rents.avg` in my script. This is the log

```
[2016-10-28 09:38:32,418][DEBUG][action.search] [Franklin Richards] [listings_v1][0], node[9qsipmkpQWGVH6Rm8zQMUg], [R], v[57], s[STARTED], a[id=tShMmgAFSnW1bRVA3AxJRw]: Failed to execute [org.elasticsearch.action.search.SearchRequest@5f6c6e5e]
RemoteTransportException[[Blind Justice][127.0.0.1:9301][indices:data/read/search[phase/query]]]; nested: QueryPhaseExecutionException[Query Failed [Failed to execute main query]]; nested: ScriptException[failed to run inline script [return _source['location'].distanceInMiles(center_lat, center_long);] using lang [groovy]]; nested: NullPointerException[Cannot invoke method distanceInMiles() on null object];
Caused by: QueryPhaseExecutionException[Query Failed [Failed to execute main query]]; nested: ScriptException[failed to run inline script [return _source['location'].distanceInMiles(center_lat, center_long);] using lang [groovy]]; nested: NullPointerException[Cannot invoke method distanceInMiles() on null object];
  at org.elasticsearch.search.query.QueryPhase.execute(QueryPhase.java:409)
  at org.elasticsearch.search.query.QueryPhase.execute(QueryPhase.java:113)
  at org.elasticsearch.search.SearchService.loadOrExecuteQueryPhase(SearchService.java:366)
  at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:378)
  at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:368)
  at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:365)
  at org.elasticsearch.transport.TransportRequestHandler.messageReceived(TransportRequestHandler.java:33)
  at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:75)
  at org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.doRun(MessageChannelHandler.java:300)
  at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
```

---

<div class="post-metadata">

### Author: ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)
#### Post date: [October 28, 2016, 6:59pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/9 "2016-10-28T18:59:41Z")

</div>

> [@pungent](#):
>
> ```
> "script": "return doc['location'].distanceInMiles(center_lat, center_long)*rents.avg;"
> 
> ```

Hi,

I got interested in this problem as well and tried this on a stripped down example. I also get the NPE, the Stacktrace on ES 2.4.1 looks like

```auto
[...]
Caused by: java.lang.NullPointerException
    at org.elasticsearch.index.fielddata.ScriptDocValues$GeoPoints.distanceInMiles(ScriptDocValues.java:345)
    at ccc06c13684b2df15349c90529c18c72f105c627.run(ccc06c13684b2df15349c90529c18c72f105c627:1)
    at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript$1.run(GroovyScriptEngineService.java:281)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript.run(GroovyScriptEngineService.java:278)

```

which (after looking at the code) most likely means the geopoint information isn't there. This is probably the case because the script is run in the "nested" scope. I don't know yet if this is expected or avoidable, haven't found much information about acessing the "parent" docs fielddata like that yet.

One way of trying to circumvent the problem for now might be to use scripted sorting on the top level of the query, something like:

```auto
{
  "query": {    
     ...  
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "groovy",
        "params": {
          "center_lat": 23.509518,
          "center_long": -18.378842
        },
        "inline": "doc['location'].distanceInMiles(center_lat, center_long)"
      },
      "order": "desc"
    }
  }
}

```

but I haven't found a way to include the " \* rents.avg" multiplier with this approach yet. But maybe this helps a bit. Will keep digging.

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 28, 2016, 8:41pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/10 "2016-10-28T20:41:09Z")

</div>

Thanks @cbuescher for the suggested workaround. Unfortunately, that will not help me because I won't get access to `* rents.avg`

I am now changing the mapping to further de-normalize my data and include same `location` field with each `rents` object, so that I can access it in the `nested` scope.

However, I would suggest to natively build a solution of this problem in ES cc. @Clinton_Gormley

---

<div class="post-metadata">

### Author: ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)
#### Post date: [October 31, 2016, 9:12am UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/11 "2016-10-31T09:12:55Z")

</div>

@pungent Can you open a Github issue that describes this limitation of `function_score` in the context of nested queries? Thanks!

---

<div class="post-metadata">

### Author: ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)
#### Post date: [October 31, 2016, 10:26am UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/12 "2016-10-31T10:26:45Z")

</div>

Hi,

I just rechecked with @mvg who knows nested query much better and he confirms that you cannot access parent fields from a nested context and that you need to do so from the parent context. So if you need access to a nested field as well you currently can try to model your data differently or work around it maybe in a way that I suggested above (maybe that doesn't work in the end though).

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 31, 2016, 3:32pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/13 "2016-10-31T15:32:07Z")

</div>

Will do @ywelsch

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [October 31, 2016, 3:32pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/14 "2016-10-31T15:32:42Z")

</div>

Yes @cbuescher I modified my mapping now ☹

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:08pm UTC](https://discuss.elastic.co/t/using-distanceinmiles-to-find-distance-fails/64232/15 "2017-07-05T22:08:13Z")

</div>


