如何在searchkick中返回两个不同的搜索结果(ElasticSearch)

时间:2018-01-08 12:33:46

标签: ruby-on-rails elasticsearch searchkick

我有这两个搜索

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}

我希望在同一结果中返回foodsrecipes。喜欢合并它们,像上面的东西或其他更好的方式。

return foods + reciples

1 个答案:

答案 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会在这两个模型中搜索FoodRecipe并返回包含两个结果的数组。