简体   繁体   中英

How to return two different search results in one in searchkick (ElasticSearch)

I have these two 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}

I want that foods and recipes been returned in the same result. Like merge them, something like above or other better way.

return foods + reciples

I resolved this with Searchkick.search Instead of:

foods = Food.search name, fields: [:name], where: {
   or: [
    [{nutritionist_id: nil}, {nutritionist_id: current_nutritionist_id}]
   ]
}, order: {_score: :desc}

and

recipes = Recipe.search name, fields: [:recipe_name], where: {
   or: [
        [{nutritionist_id: nil}, {nutritionist_id: current_nutritionist_id}]
      ]
 }, order: {_score: :desc}

I used.

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

This way the Searchkick search in these two models Food and Recipe and returns the array with both results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM