我有这两个搜索
foods = Food.search name, fields: [:name], where: {
or: [
[{nutritionist_id: nil}, {nutritionist_id: current_nutritionist_id}]
]
}, order: {_score: :desc}
recipes = Recipe.search name, fields: [:recipe_name], where: {
or: [
[{nutritionist_id: nil}, {nutritionist_id: current_nutritionist_id}]
]
}, order: {_score: :desc}
我希望在同一结果中返回foods
和recipes
。喜欢合并它们,像上面的东西或其他更好的方式。
return foods + reciples
答案 0 :(得分:0)
我用Searchkick.search
解决了这个问题
而不是:
foods = Food.search name, fields: [:name], where: {
or: [
[{nutritionist_id: nil}, {nutritionist_id: current_nutritionist_id}]
]
}, order: {_score: :desc}
和
recipes = Recipe.search name, fields: [:recipe_name], where: {
or: [
[{nutritionist_id: nil}, {nutritionist_id: current_nutritionist_id}]
]
}, order: {_score: :desc}
我用过。
result = Searchkick.search(name, index_name: [Food, Recipe], fields: [:name, :recipe_name],
where: {
_or: [ {nutritionist_id: nil}, {nutritionist_id: current_nutritionist_id} ]
}, order: {_score: :desc})
result
这样,Searchkick会在这两个模型中搜索Food
和Recipe
并返回包含两个结果的数组。