如何使用 rails sunspot for rsolr 进行 OR 查询
How to do an OR query with rails sunspot for rsolr
我找不到如何使用太阳黑子进行 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 部分下,如果他们搜索了某个位置并且还指示包括远程工作,则查询应该类似于 "where location=locationsearchtext OR work_remotely=true"。那么,你如何用太阳黑子构建它?谢谢
这是你需要做的:
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
您可以在 sunspot wiki 上找到更多信息:
https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields#disjunctions-and-conjunctions
我找不到如何使用太阳黑子进行 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 部分下,如果他们搜索了某个位置并且还指示包括远程工作,则查询应该类似于 "where location=locationsearchtext OR work_remotely=true"。那么,你如何用太阳黑子构建它?谢谢
这是你需要做的:
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
您可以在 sunspot wiki 上找到更多信息: https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields#disjunctions-and-conjunctions