简体   繁体   English

使用gem Tire和elasticsearch进行高级搜索

[英]Advanced search with gem Tire and elasticsearch

I am trying use gem tire to search in my application. 我正在尝试使用宝石轮胎在我的应用程序中搜索。 I have tables Areas, Cities, Hotels, Rooms and RoomInformations. 我有桌子区域,城市,酒店,房间和房间信息。

What I need is find hotel by query and other text fields as date_from, date_to and number of rooms. 我需要的是通过查询和其他文本字段找到酒店date_from,date_to和房间数。 In model I have method which calculate if hotel has free rooms or not and send boolean. 在模型中我有计算酒店是否有免费房间的方法并发送布尔值。 How can I use this boolean method to filter the query? 如何使用此布尔方法过滤查询?

Thank you 谢谢

in hotel.html.erb 在hotel.html.erb

<%= form_tag search_hotels_path, :method => 'post' do %>
  <%= label_tag :query %>
  <%= text_field_tag :query, params[:query] %>
  <%= label_tag 'From' %>
  <%= text_field_tag :date_f, params[:date_from] %>
  <%= label_tag 'To' %>
  <%= text_field_tag :date_to, params[:date_to] %>
  <%= label_tag 'Rooms' %>
  <%= text_field_tag :rooms, params[:rooms] %>
  <%= submit_tag :search %>
<% end %>

in hotel.rb 在hotel.rb

class Hotel < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks

belongs_to :city
belongs_to :area
has_one :area, :through => :city
has_many :rooms, :dependent => :delete_all


def search(params)

        tire.search do
            query do
                boolean do  
                    must { string params[:query] } if params[:query].present?
                    must { term rooms_available params, true}
                end
            end 
        end

end

self.include_root_in_json = false
def to_indexed_json
    to_json(methods: [:area_name,:city_name])
end

def area_name
    city.area.name
end
def city_name
    city.name
end


def rooms_available params
    exit
    begin_date = params[:date_from].to_date
    end_date = params[:date_to].to_date
    number_of_rooms = params[:rooms]

    rooms.each do |room|
        if room.available(begin_date, end_date, number_of_rooms)
            return true
        end
    end
    return false
end

end

Put this in to_json method: 把它放在to_json方法中:

def to_json
  {
    :area_name => self.area_name, 
    :city_name => self.city_name,
    :rooms_available => self.rooms_available
   }.to_json
end

Then: 然后:

def search(params)

    tire.search do
        query do
            boolean do  
                must { string params[:query] } if params[:query].present?
                must { term rooms_available, true}
            end
        end 
    end

end

In this case though, it is better to put rooms_available in filter, since it's a must and you don't care about scoring on that clause. 但在这种情况下,最好将rooms_available放在过滤器中,因为它是必须的,并且您不关心对该子句的评分。

def search(params)

    tire.search do
        query do
            boolean do  
                must { string params[:query] } if params[:query].present?
            end
        end 
        filter do
            boolean do  
               must { term rooms_available, true}
            end
        end
    end

end

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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