简体   繁体   English

Rails 3 Sunspot Solr搜索范围内

[英]Rails 3 Sunspot solr search within bounds

I have a question to ask regarding the new Sunspot/solr feature: restriction with near I have (via the google geocoding API) the viewport and the bounds which are latitude/longitude coordinates of the southwest and northeast corners of a bounding box. 关于新的Sunspot / solr功能,我有一个问题要问:我(通过google地理编码API)的视口和边界(即边界框的西南角和东北角的纬度/经度坐标)在附近具有限制 I want Sunspot/Solr to search within this bounding box but I haven't figured it out yet. 我希望Sunspot / Solr在此边界框内搜索,但我还没有弄清楚。 So my question is: Is it possible to make Solr (or via any of the solr plugins) capable for searches within a bounding box? 所以我的问题是: 是否可以使Solr(或通过任何solr插件)能够在边界框中进行搜索? If yes, how? 如果是,怎么办?

Thank you 谢谢

You can just create a lat and lng field of type trie double, and then do two range queries (one for a lat range, one for a lng range). 您可以只创建类型为trie double的latlng字段,然后执行两个范围查询(一个用于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 supports spartial search using Geohash, see RestrictionWithNear . Sunspot支持使用Geohash进行局部搜索,请参阅RestrictionWithNear But you can only use the predifined distance (though :precision ). 但是,您只能使用预定义的距离(尽管: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 is added in solr starting from 3.1, I cannot find correspondng DSL in sunspot, but you can always use adjust_solr_params to add customized parameters: 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

You have to use Solr 3.1 (the bundled solr in sunspot is 1.4), and index field location like 您必须使用Solr 3.1(太阳黑子中捆绑的solr是1.4),并且索引字段的位置类似于

class Product < ActiveRecord::Base
  searchable do
    string(:location, :as => :location) { [lat,lng].join(',') }
  end
end

Also the field type needs to be added into schema.xml. 同样,字段类型也需要添加到schema.xml中。 (An example, I have not tried it myself) (例如,我自己没有尝试过)

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM