Sunspot 支持使用 Geohash 进行局部搜索,请参阅 RestrictionWithNear。但是你只能使用预定义的距离(虽然:precision)。
# model
# lat: decimal
# lng: decimal
class Product < ActiveRecord::Base
seachable do
location :location do
Sunspot::Util::Coordinates.new(lat, lng)
end
end
end
# search
Product.search do
# near(lat, lng)
with(:location).near(76.4556, 67.9987, :precision => 3)
end
Sparcial 是 solr 3.1 开始的added,我在 sunspot 中找不到对应的 DSL,但你可以随时使用adjust_solr_params 添加自定义参数:
Product.search do
adjust_solr_params do |params|
parmas[:fq] << '{!geofilt pt=74.4556,67.9987 sfield=location d=5}'
end
end
你必须使用 Solr 3.1(sunspot 中捆绑的 solr 是 1.4),和 index field location 之类的
class Product < ActiveRecord::Base
searchable do
string(:location, :as => :location) { [lat,lng].join(',') }
end
end
还需要将字段类型添加到 schema.xml 中。 (举个例子,我自己没试过)
<types>
...
<fieldType name="geo" class="solr.LatLonType" omitNorms="true"/>
</types>
<fields>
...
<field name="location" stored="false" termVectors="false" type="geo" multiValued="false" indexed="true"/>
</fields>