如何使用rails sunspot为rsolr进行OR查询

时间:2015-06-12 17:30:30

标签: sunspot sunspot-rails

我无法找到如何使用太阳黑子进行OR查询的示例。这是我的代码。

class JobSearch < ActiveRecord::Base
  monetize :salary, as: 'salary', allow_nil: true
  DEFAULT_DISTANCE_IN_MILES = 50

  def search(page, page_size)
    latitude, longitude = LatitudeLongitudeLookup.latitude_longitude_from_string(location) if location.present?
    Job.search(include: [:state]) do
      facet(:minimum_compensation_cents) do
        row(150000..Float::INFINITY) do
          with(:minimum_compensation_cents).greater_than(150000)
        end
        row(100000..149999) do
          with(:minimum_compensation_cents, 100000..149999)
        end
        row(50000..99999) do
          with(:minimum_compensation_cents, 50000..99999)
        end
        row(25000..49999) do
          with(:minimum_compensation_cents, 25000..49999)
        end
      end

      facet :titles, limit: 5
      facet :city_state, limit: 5
      facet :job_type
      facet :work_remotely

      if !include_remote
        with(:location).in_radius(latitude, longitude, DEFAULT_DISTANCE_IN_MILES, bbox: true) if latitude && longitude
      else #need to do a location with an OR include_remote
        with(:location).in_radius(latitude, longitude, DEFAULT_DISTANCE_IN_MILES, bbox: true) if latitude && longitude
        #OR - how to do that?
        with(:work_remotely, true)
      end

      with(:city_state, location) if location.present? && (!latitude && !longitude)
      order_by_geodist(:location, latitude, longitude) if latitude && longitude
      with(:job_type, job_type) if job_type.present?
      with(:titles, position_title) if position_title.present?

      if !latitude && !longitude && !include_remote 
        with(:work_remotely, include_remote)
      end

      fulltext keyword
      paginate page: page, per_page: page_size
    end
  end
end

我需要做的是直接在facet部分下面,如果他们在某个位置搜索并且还指示包含远程作业,则查询应该类似于&#34;其中location = locationsearchtext或work_remotely = true&#34 ;。那么,你如何用太阳黑子构建呢?感谢。

1 个答案:

答案 0 :(得分:0)

这是你需要做的:

facet :work_remotely

      if !include_remote
        with(:location).in_radius(latitude, longitude, DEFAULT_DISTANCE_IN_MILES, bbox: true) if latitude && longitude
      else #need to do a location with an OR include_remote
        any_of do
          with(:location).in_radius(latitude, longitude, DEFAULT_DISTANCE_IN_MILES, bbox: true) if latitude && longitude
          with(:work_remotely, true)
        end
      end

您可以找到有关太阳黑子维基的更多信息: https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields#disjunctions-and-conjunctions