Danilo Cândido
Danilo Cândido

Reputation: 408

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

Upvotes: 1

Views: 268

Answers (1)

Danilo Cândido
Danilo Cândido

Reputation: 408

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.

Upvotes: 1

Related Questions