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)
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
[...]
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:
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
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).
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.