Alexey Spiridonov
Alexey Spiridonov

Reputation: 93

Combine two sunspot searches with OR condition

How can I combine two sunspot search conditions with OR condition? I tried here ↓ but didn't have success.

first_search_conditions = Proc.new do |s|
  s.with(:store_id, 1)
end

second_search_conditions = Proc.new do |s|
  s.with(:store_id, 2)
  s.fulltext('hello') do |ft|
    ft.fields(name_words: 90)
  end
end

def search(first_block, second_block)
  Sunspot.new_search(Offer) do |s|
    s.any_of do |scope|
      first_block.call(scope)
      second_block.call(scope)
    end
  end
end

search(first_search_conditions, second_search_conditions)

Upvotes: 0

Views: 222

Answers (1)

sawa
sawa

Reputation: 168101

I don't know about sunspot, but if you have two procs first_search_conditions and second_search_conditions, and want the disjunction of them, you can just construct:

Proc.new do |s|
  first_search_conditions.call(s) ||
  second_search_conditions.call(s)
end

Upvotes: 0

Related Questions