2016-01-20 24 views
2

當試圖從我的索引中的記錄動態搜索緯度時,我得到了非法的緯度值。在查看我的索引時,我看不到任何無效的經度和緯度值,所以我猜這是我的代碼中的錯誤。elasticsearch rails - 非法緯度值

確切的錯誤;

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

搜索的型號代碼;

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

映射;

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

}}

+0

我假設'address.location'領域是一個數組,所以你可能需要通過它作爲'[lon,lat]',倒序。 [更多](https://www.elastic.co/guide/en/elasticsearch/reference/2.x/query-dsl-geo-distance-query.html#_lat_lon_as_array_3)就是這樣嗎? – ChintanShah25

+0

在我的數據庫中,位置保存爲一個字符串,在索引中它是一個geo_point。使用[lon,lat]或[lat,lon]中的任意一個的變化將返回未定義的局部變量或方法錯誤 。 – user3579614

+0

你能告訴我們你有一個樣本位置嗎?它是如何格式化的? – ChintanShah25

回答

3

我今天可是經度經歷了同樣的錯誤。原來Elasticsearch版本2.0和更新版本只允許從-180..180範圍內的經度值。

例如版本1.7.5(我在本地環境中使用)使用範圍-180..180和0-360。

你的錯誤狀態:

非法緯度值[269.99999983236194]

對於緯度,從範圍-90..90只值是允許的。

唯一的解決辦法是在將它們發送到Elasticsearch之前重新計算緯度/經度值。例如,對於經度,你可以這樣做:

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

這是更好地清潔經度是這樣的:

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

    return $result; 
}