[英]Rails 3 Sunspot solr search within bounds
您可以只創建類型為trie double的lat
和lng
字段,然后執行兩個范圍查詢(一個用於lat范圍,一個用於lng范圍)。
class Product < ActiveRecord::Base
seachable do
double :latitude
double :longitude
end
end
# search
Product.search do
with :latitude, 36.5667..80.4553
with :longitude, 76.4556..67.9987
end
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是從3.1開始在solr中添加的 ,我無法在黑子中找到對應的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(太陽黑子中捆綁的solr是1.4),並且索引字段的位置類似於
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.