使用Searchkick

时间:2018-03-19 14:00:59

标签: ruby-on-rails-5 searchkick

我正在开发一个Rails 5和Searchkick项目,该项目具有搜索功能,其中包含可搜索多个模型的过滤器。例如,我有加入的College和State模型。我希望能够在College模型中搜索特定的校园设置,并在州模型中搜索特定的州。我希望查询使用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

2 个答案:

答案 0 :(得分:0)

实际上,searchkick提供了执行多模型搜索的工具,请查看此处的文档:https://github.com/ankane/searchkick#multi-search

和提供的示例:

fresh_products = Product.search("fresh", execute: false)
frozen_products = Product.search("frozen", execute: false)
Searchkick.multi_search([fresh_products, frozen_products])

如果您使用不同的型号,您可以使用:

fresh_products = Product.search("apples", execute: false)
frozen_products = FrozenProduct.search("ice", execute: false)
Searchkick.multi_search([fresh_products, frozen_products])

同样由looking at the tests您可以找到一些指导。

答案 1 :(得分:0)

Searchkick.search "mike", models: [Video, Image]

使用更多高级选项搜索 -

Searchkick.search "mike", { fields: ["slug^5", "title", "about"], misspellings: {below: 5, edit_distance: 2}, models: [Video, Image]}
相关问题