【问题标题】:Rails 3 Sunspot solr search within boundsRails 3 Sunspot solr 搜索范围内
【发布时间】:2011-03-08 22:42:22
【问题描述】:

我有一个关于新太阳黑子/solr 功能的问题要问:restriction with near 我有(通过谷歌地理编码 API)视口和边界,它们是边界框西南角和东北角的纬度/经度坐标。我希望 Sunspot/Solr 在这个边界框内进行搜索,但我还没有弄清楚。所以我的问题是:是否有可能使 Solr(或通过任何 solr 插件)能够在边界框中进行搜索?如果是,如何?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 solr ruby-on-rails-plugins sunspot


    【解决方案1】:

    您可以只创建一个类型为 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
    

    【讨论】:

    • 感谢您的回答 :) 为了弄清楚,我必须在可搜索的 do double 中定义:lat, :trie => true double :lng, :trie => true end 而在我的搜索中,我只需使用 :lat, 36.5667..80.4553 和 :lng, 76.4556..67.9987 ??有这么简单吗??我现在觉得很愚蠢:P P.S.提供的坐标我不知道它们通向哪里:P
    • 是的,这正是我的建议:)
    • Nice :) 另外您是否知道将来是否会实施 Sunspot/solr 来进行多边形区域搜索?您是否知道插件或 solr 用来执行此操作的东西?再次感谢!你帮了大忙!
    【解决方案2】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多