0

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 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 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 available in my above script.

Please note even if I remove rents.avg from the script error remains there. So my suspicion is doc is not available inside path: rents ? If so then how to get it work?

JVK
  • 3,782
  • 8
  • 43
  • 67
  • 1
    You can not access the root values from nested query context, you could refer to my answer to similar question [here](http://stackoverflow.com/questions/40095499/elasticsearch-script-query-involving-root-and-nested-values/40098750#40098750), you would have to include avg values in root document with copy_to param. Let me know if it does not work out – ChintanShah25 Oct 28 '16 at 16:34
  • Thank you @ChintanShah25 for the suggestion. Unfortunately changing mapping and re-indexing is not an available option for me. I appreciate your help! – JVK Oct 28 '16 at 16:55

0 Answers0