2
votes

When I add:

fields: [ "doc['Location'].lon" ]

to my query, the longitude value is different that what is shown in the document source.

Here you can see the results of a query where I fetched the doc['Latitude'].lon and .lat, and the _source.Latitude to compare:

https://gist.github.com/d9533170f1f50fd27e87 (note - these have been passed through json_decode in PHP, but the data is the same before using json_decode.)

I first noticed this when I was using "doc['field_name'].distance(lat, lon)" to try and add the distance as a field to my query. I tried both the "script_fields" and "fields" keys and each had the same result.

UPDATE: I noticed that the "doc['Location'].lon" is returning what I thought should be the doc['Location'].lat (the lat and lon are switched.)

1

1 Answers

5
votes

The problem was that when using GeoJSON format (or using lat/long as an array) you have to switch the order of lat/lng to lng/lat.

I am rebuilding my index, but, in order to work around this I have used this query for now:

doc['Location'].distance( lon + 180, lat ) // Temporary bandaid

Once I've rebuilt the index with the correct values I'll switch back to:

doc['field_name'].distance(lat, lon) // The correct method