gpt4 book ai didi

ruby-on-rails - 使用 Geocoder 和 Sunspot 进行搜索?

转载 作者:数据小太阳 更新时间:2023-10-29 08:17:46 26 4
gpt4 key购买 nike

我正在使用 GeocoderSunspot我的应用程序中有 gem,并且我有一个名为 :search_near_address 的字段,该字段旨在让用户能够输入他们想要在 X 英里数 附近搜索的地址。我试图映射到的是我的 Stores :address 以用于 :search_near_address 字段。这样,用户可以在 :search_near_address 字段中输入地址(即 451 University Avenue, Palo Alto, CA),它将在 50 英里的半径范围内进行搜索。

回答

太阳黑子 1.2.1


class Store < ActiveRecord::Base
attr_accessible :address, :latitude, :longitude
has_many :products
geocoded_by :address
after_validation :geocode
reverse_geocoded_by :latitude, :longitude
after_validation :reverse_geocode
end

class Product < ActiveRecord::Base
belongs_to :store

searchable do # Searching with product model.
string :search_near # For rake sunspot:reindex
location :location
end

def search_near_address
store.address if store # You may have to use the "if store".
end

def location
# may need the "if store" after longitude)....
Sunspot::Util::Coordinates.new(store.latitude, store.longitude)
end
end

class SearchController < ApplicationController

def index
@search = Product.search do |q| # Search with sunspot
q.fulltext params[:search]
q.with(:location).near(*Geocoder.coordinates(params[:search_near_address]), :precision => 4) if params[:search_near_address].present?
end
@products = @search.results # Return results from Product.search block.
end
end

# search/index/html.erb
<%= form_tag results_search_index_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= text_field_tag :search_near_address, params[:search_near_address] %>
<%= submit_tag "Go", :name => nil %>
<% end %>

最佳答案

首先,@m_x 说的是对的,您不能指望将 Store#near 结果分配给 @search 对象会起作用....

只需阅读 https://github.com/sunspot/sunspot 中有关地理空间的文档,您可以清楚地看到您缺少一些东西:

您必须使用地理编码器所需的纬度和经度字段声明您的地理空间字段:

class Product < ActiveRecord::Base
belongs_to :store

searchable do # for Sunspot search.
string :search_near
latlon(:location) {
Sunspot::Util::Coordinates.new(category.latitude, category.longitude)
}
end

def search_near
#I changed this because business_store will not work out.
store.address
end
end

然后你可以像这样搜索(在索引了一些东西之后):

class SearchController < ApplicationController

def index
# Search with sunspot
@search = Sunspot.search(Product) do
fulltext params[:search]

# The "*" pop off the elements of the array that
# Geocoder.coordinates returns.
with(:location).near(*Geocoder.coordinates(params[:search_near]),
:precision => 6)

# NOTE: You could also use the in_radius method but only in the pre-release version:
# with(:location).in_radius(*Geocoder.coordinates(params[:search_near]), 100) if params[:search_near].present?
end

# Return results from Product.search block.
@products = @search.results
end
end

另请阅读此处的 :precision 选项 http://sunspot.github.com/docs/Sunspot/DSL/RestrictionWithNear.html#near-instance_method

关于地理编码器 https://github.com/alexreisner/geocoder

关于ruby-on-rails - 使用 Geocoder 和 Sunspot 进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8477880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com