我正在使用带有 elelasticsearch 的 Tire gem,我想像 Rails 范围一样过滤查询结果。我想排除结果的不同参数,就像我对这个范围所做的那样:
scope :online, ->{ where.not(price: nil || 0).where.not(name: 'Undefined').where(online: true) }
我怎样才能用我的self.search
方法做到这一点?我的方法正确吗?
这是我的Product
模型:
Class Product
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :name, type: 'string', analyzer: 'snowball', boost: 100
indexes :description, analyzer: 'snowball'
indexes :price, type: 'float'
indexes :category, type: 'string'
indexes :location, type: 'string'
indexes :online, type: 'boolean'
indexes :tags do
indexes :name, type: 'string', analyzer: 'snowball', boost: 100
end
end
def to_indexed_json
{
name: name,
description: description,
price: price,
category: category,
location: location,
online: online,
created_at: created_at,
updated_at: updated_at,
tags: tags
}.to_json
end
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 12, query: {"match_all" => {}} ) do
query { string params[:query], default_operator: "AND" }
end
end
end