user3023421
user3023421

Reputation: 363

Sunspot multiple models specific condition

I have multiple models setup and I am trying to search through them all. However, some models have specific conditions that need to be satisfied. A basic example of my search:

@search = Sunspot.search [User, Event, Story, Album, Photo] do
  any_of do # Return anything that matches any of the scopes in this block
    all_of do # Return only those results that match these scopes
      with :class, Album # This limits scopes in this block to Album results
      with :private, false
    end
  end

  fulltext params[:q]
  with :deleted_at, nil
  order_by :created_at, :desc
end

In this example, I am trying to only show albums that are not private. This however doesn't work, nothing is returning. When I removed the any_of block, results return.

I referenced: Querying multiple models with different attributes using Sunspot

Similar issue: Searching across multiple models using sunspot/solr

Any help would be appreciated.

Upvotes: 0

Views: 228

Answers (2)

Abibullah Rahamathulah
Abibullah Rahamathulah

Reputation: 2891

I think you are looking for something similar to this I guess, this is happening due to some weird scenario. I'm not sure what it is.

But I have found another way to solve this. I have added my answer here: Querying multiple models with different attributes using Sunspot

@search = Sunspot.search [User, Event, Story, Album, Photo] do
  any_of do # Return anything that matches any of the scopes in this block
    all_of do # Return only those results that match these scopes
      with :class, Album # This limits scopes in this block to Album results
      with :private, false
    end

    all_of do
      with :deleted_at, nil
    end
  end

  fulltext params[:q]
  order_by :created_at, :desc
end

Upvotes: 0

user3023421
user3023421

Reputation: 363

So I managed to find a workaround

Instead of searching for the private condition, I am not indexing album records who are private

For those who stumble upon this, inside the Album model:

searchable :if => proc { |album| album.deleted_at == nil && album.private == false } do
  text :title
  text :description

  boolean :private
  time :created_at
end

Upvotes: 0

Related Questions