如何在 searchkick (ElasticSearch) 中 return 将两个不同的搜索结果合而为一
How to return two different search results in one in searchkick (ElasticSearch)
我有这两个搜索
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
我用 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
和 returns 中搜索具有两个结果的数组。
我有这两个搜索
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
我用 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
和 returns 中搜索具有两个结果的数组。