sameera207
sameera207

Reputation: 16629

Is there a way to combine two seperate `Searchkick::Results` objects in to one

Is there a way to combine two seperate Searchkick::Results object in to one.

E.g

user = User.search("abc", where: { <condition 1 > })
blog = Blog.search("abc", where: { <condition 2 > })

combined both results to a one Searchkick::Results object so that I can use pagination , relevance search etc.. as a one unit.

I had a look at Multi Search, but that seems to be not doing what I want

PS: I've opened a ticket in searchkick repo asking the same

Upvotes: 3

Views: 846

Answers (2)

Andrew Kane
Andrew Kane

Reputation: 3236

You can search across multiple indices with:

where = {
  _or: [
    {_type: "user", somefield: "value1"},
    {_type: "blog", otherfield: "value2"}
  ]
}
Searchkick.search "abc", index_name: [User, Blog], where: where

Source

Upvotes: 1

wish
wish

Reputation: 43

user = User.search("abc", where: { <condition 1 > })

blog = Blog.search("abc", where: { <condition 2 > })

Convert two objects into an array and merge array using concat method.

user_blog = user.to_a.concat(blog.to_a)

Upvotes: 1

Related Questions