问题描述
在 rails 中,我使用 searchkick
gem 进行搜索.当我为 where 子句添加更多字段时,返回零结果.
In rails, I am using searchkick
gem for search. When I am adding more fields for where clause then returning zero results.
实际上搜索适用于以下方法(User.rb),
Actually search is working for below method(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}
这样的条件时,它不返回任何数据.
When I add condition like where: {active: false, inactive: true, deleted_at: nil}
it returns no data.
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.
Is there any mistake in above where
condition? Please help me to solve this issue. I am using this gem for the first time.
推荐答案
实际上,where
子句中使用的字段的索引方式不同.您必须将这些字段定义为 filterable
:
Actually, the fields used in the where
clause are indexed differently.You have to define those fields as filterable
:
searchkick word_start: [:name], filterable: [:active, :inactive, :deleted_at]
这应该有效.
顺便说一句:您不需要活动"和非活动"属性.如果active"属性为假,您就知道它处于非活动状态.
BTW: You don't need both "active" and "inactive" attributes. If the "active" attribute is false, you know it is inactive.
这篇关于Rails - 如何在 searchkick 中为过滤器添加更多字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!