user3579614
user3579614

Reputation: 63

elasticsearch rails - illegal latitude value

When trying to search from latitude dynamically from records in my index I've been getting an illegal latitude value. When looking at my index I can't see any invalid latitude and longitude values, so I'm guessing it's an error in my code.

Exact error;

{"type":"query_parsing_exception","reason":"illegal latitude value [269.99999983236194] for [geo_distance]","index":"addresses","line":1,"col":172}}]},"status":400}

Model code for search;

def self.search(query)
    __elasticsearch__.search(
        {
            query:{
                multi_match: {
                    query: query,
                    fields: ['full_address']
                }
            },
            filter:{
                geo_distance:{
                    distance: "6miles",
                    location: "address.location" 
                }
            }
        }
    )
end

Mapping;

{ "addresses" : {
"mappings" : {
  "address" : {
    "properties" : {
      "county" : {
        "type" : "string"
      },
      "full_address" : {
        "type" : "string"
      },
      "location" : {
        "type" : "geo_point"
      }
    }
  }
}

} }

Upvotes: 2

Views: 5273

Answers (2)

user2434435
user2434435

Reputation: 177

It's better to clean longitude with something like this:

$adjustedLongitude = function($vongitude) {
    $result = $vongitude - 360 * floor($vongitude / 360);
    if (abs($result) > 180) {
        $result = 360 - $result;
    }

    return $result;
}

Upvotes: 0

Milos Blasko
Milos Blasko

Reputation: 644

I experienced the same error today but with longitude. Turns out Elasticsearch version 2.0 and newer only allows longitude values from range -180..180.

For example version 1.7.5 (which I had in my local environment) worked with both ranges -180..180 and 0..360.

Your error states:

illegal latitude value [269.99999983236194]

For latitude, only values from range -90..90 are allowed.

The only fix is to re-compute lat/long values before sending them to Elasticsearch. For example for longitude you could do:

((longitude.to_f + 180) % 360) - 180

Upvotes: 7

Related Questions