我有一个account model
,其中每个帐户都有一个category_id:integer
和latitude:float
longitude:float
列。
我正在通过完整地址和geocoded gem.
我还使用html5地理位置来获取用户的当前位置,然后将坐标保存在Cookie lat_lng
中:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(setGeoCookie,showError);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function setGeoCookie(position) {
var cookieName = "lat_lng";
var now = new Date();
var time = now.getTime();
time += 3600 * 2000;
now.setTime(time);
var cookie_val = position.coords.latitude + "," + position.coords.longitude;
document.cookie = cookieName +"=" + cookie_val + '; expires=' + now.toUTCString() + '; path=/';
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
window.location = window.location;
window.alert("Please click on allow");
break;
case error.POSITION_UNAVAILABLE:
window.location = window.location;
window.alert("Location information is unavailable.");
break;
case error.TIMEOUT:
window.location = window.location;
window.alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
window.location = window.location;
window.alert("An unknown error occurred.");
break;
}
location.reload();
}
我的目标是通过category_id搜索并找到一个帐户...我正在控制器内执行以下操作:
@results = Account.__elasticsearch__.search(query: {match: { category_id: params[:category_id]}})
这很好用!
然后,我想按距离和距离用户当前位置25英里范围内的结果对结果进行排序...我想这样做:
@results = Account.__elasticsearch__.search(query: {match: { category_id: params[:category_id]}}, sort: [{_geo_distance: {location: {lat: current_location[0], lon: current_location[1]}, order: "asc", unit:"miles"}}])
def current_location
@lat_lng = cookies[:lat_lng].split(",")
end
以上返回此错误:
[400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"failed to find mapper for [location] for geo distance based sort"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"accounts","node":"AfyW4Pa4S-qKhua-3lY4rg","reason":{"type":"illegal_argument_exception","reason":"failed to find mapper for [location] for geo distance based sort"}}],"caused_by":{"type":"illegal_argument_exception","reason":"failed to find mapper for [location] for geo distance based sort","caused_by":{"type":"illegal_argument_exception","reason":"failed to find mapper for [location] for geo distance based sort"}}},"status":400}
elasticsearch是我的新手,因此非常感谢您的帮助。