在rails中,我使用searchkick
gem进行搜索。当我为where子句添加更多字段时,返回零结果。
实际搜索适用于以下方法(User.rb),
searchkick word_start: [:name]
def initialize(name, limit = User::SUGGESTION_LIMIT, page = nil)
@name = name
@limit = limit
@page = page
@per_page = limit.to_i
end
query = {
match: :word_start,
fields: [{ emails: "exact" }, "name^5"],
misspellings: { prefix_length: 2 },
load: false
}
User.search(name, query).records
当我添加类似
where: {active: false, inactive: true, deleted_at: nil}
的条件时,它不返回任何数据。query = {
match: :word_start,
where: {active: false, inactive: true, deleted_at: nil},
fields: [{ emails: "exact"}, "name^5"],
misspellings: { prefix_length: 2 },
load: false
}
以上
where
条件是否有任何错误?请帮我解决这个问题。我是第一次使用这种 gem 。
最佳答案
实际上,where
子句中使用的字段的索引不同。
您必须将这些字段定义为filterable
:
searchkick word_start: [:name], filterable: [:active, :inactive, :deleted_at]
这应该工作。
顺便说一句:您不需要“ Activity ”和“非 Activity ”属性。如果“ Activity ”属性为false,则表明它为非 Activity 状态。
关于ruby-on-rails - Rails-如何在searchkick中添加更多字段以进行过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47977917/