Ruby on rails 3 使用SQL进行弹性搜索和搜索
在我当前的设置中,我需要添加一些定制的SQL语句,该语句将一些数据限定在Gem的范围内 我正在使用Rails3.2和Ruby1.9.3 在我的列表控制器中,我有以下内容:Ruby on rails 3 使用SQL进行弹性搜索和搜索,ruby-on-rails-3,
elasticsearch,tire,Ruby On Rails 3,
elasticsearch,Tire,在我当前的设置中,我需要添加一些定制的SQL语句,该语句将一些数据限定在Gem的范围内 我正在使用Rails3.2和Ruby1.9.3 在我的列表控制器中,我有以下内容: @listings = Listing.search() 对于my Listing.rb,我将搜索方法与许多过滤器一起使用,例如: def self.search(params={}) tire.search(load: true, page: params[:page], per_page: 50) do |sea
@listings = Listing.search()
对于my Listing.rb,我将搜索方法与许多过滤器一起使用,例如:
def self.search(params={})
tire.search(load: true, page: params[:page], per_page: 50) do |search|
search.query { string params[:query], :default_operator => "AND" } if params[:query].present?
search.filter :range, posted_at: {lte: DateTime.now}
search.filter :term, "property.rooms_available" => params[:property_rooms_available] if params[:property_rooms_available].present?
search.filter :term, "property.user_state" => params[:property_user_state] if params[:property_user_state].present?
...
end
我需要做的是将这个SQL语句添加到搜索方法中,这样它就可以根据lonitude和latitute来确定范围。“coords”由URL中的参数以表单形式传入
http://localhost:3000/listings?coords=51.0000,-01.0000 52.0000,-02.0000
(在-01.0000和52.0000之间有一个空格。)
目前我有:
sql = "SELECT title, properties.id, properties.lng,properties.lat from listings WHERE ST_Within(ST_GeometryFromText('POINT(' || lat || ' ' || lng || ')'),ST_GeometryFromText('POLYGON((#{coords}))'));"
我想尝试通过这样的方式在控制器中对其进行范围限定
def self.polyed(coords)
joins(:property).
where("ST_Within(ST_GeometryFromText('POINT(' || properties.lat || ' ' || properties.lng || ')'),ST_GeometryFromText('POLYGON((#{coords}))'))").
select("title, properties.id, properties.lng,properties.lat")
end
而这个
清单u controller.rb
它需要以@ListingsByHTML和json格式返回结果
http://localhost:3000/listings.json?
我已经在使用RABL自动生成json
这可能吗
非常感谢
Ryan一种可能性是在
:load
选项中传递选项joins
,where
,等等
但是,如果您想在Rails代码中过滤返回的结果,一种更好的方法似乎是使用Tire加载记录ID(使用字段选项限制返回的字段),然后在SQL查询中使用它们
http://localhost:3000/listings.json?