【发布时间】:2018-08-28 02:57:21
【问题描述】:
我正在开发一个 Rails 5 和 Searchkick 项目,该项目具有搜索功能以及搜索多个模型的过滤器。例如,我有加入的大学和州立模型。我希望能够在 College 模型中搜索特定的校园环境,并在 State 模型中搜索特定的州。我希望查询使用 AND 运算符,而不是 OR,但到目前为止,我只能在以前的 Searchkick 问题中找到有关使用 OR 的信息。到目前为止,我想出的是:
@results = College.search "*", index_name: [College, State], where: {_and: [{_type: "college", campus_setting: 21}, {_type: "state", state: "Massachusetts"}]}
理想情况下,我将构建一个包含所有 where 条件的哈希,例如:
where:{
{_type: "college", campus_setting: 21},
{_type: "state", state: "Massachusetts"}
}
然后将where条件传递给搜索:
@results = College.search "*", index_name: [College, State], where: [where]
但这不会返回任何结果,即使我可以直接查询我的数据库并获得结果。
在 SQL 中,我的查询如下所示:
SELECT * FROM colleges c
INNER JOIN states s
ON c.state_id = s.id
WHERE c.campus_setting = 21 AND s.state = 'Massachusetts';
是否有其他方法可以编写此搜索,或者此功能不可用?我能找到的最接近的东西在这里:https://github.com/ankane/searchkick/issues/904
【问题讨论】:
标签: ruby-on-rails-5 searchkick