rails elasticsearch-model multiple params query

时间:2017-08-16 13:44:47

标签: ruby elasticsearch ruby-on-rails-5 kaminari elasticsearch-model

我想用下面的elasticsearch-model gem运行多个搜索参数。在我使用 gem tire 使用多个查询参数搜索记录之前。

我的模特:

class Book < ApplicationRecord
  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
   indexes :id, type: 'integer'
   indexes :title
   indexes :author
   indexes :category
   indexes :price
  end

 def self.search(params)
  tire.search(load: true) do
    query do
      boolean do
       must { string params[:title] } if params[:title].present?
       must { string params[:author] } if params[:author].present?
       must { string params[:category] } if params[:category].present?
       should { range :price, { gte: params[:price_range_min], lte: 
       params[:price_range_max] } } if 
       params[:price_range_min].present? 
       && params[:price_range_max].present?
     end
    end
   end
 end

end

我的控制器:

class BookController < ApplicationController
   def index
    @books = Book.search(params)
    @books = 
    Kaminari.paginate_array(@books).page(params[:page]).per(10)
   end
 end

我遵循了这个文档elasticsearch-model,但我只得到两个字段的结果。

任何帮助?

0 个答案:

没有答案