Pietro
Pietro

Reputation: 1836

Rails Solr: how to exclude a result by model id

I'm trying, using Solr, to exclude results having certain ID. What I did is to define in searchable "integer id" and then using "without" in Model.search I do "without :id, id"

Here is my code sample:

 searchable do
    text(:title, boost: 2.0) { t.title }
    text(:intro) { t.intro }
    text(:detail) { t.detail }

    text(:subjects) { tags.select{ |t| t.name[0,1] == "@" }.map{ |t| t.name }.join( ' ' ) }
    text(:context) { tags.select{ |t| t.name[0,1] == "#" }.map{ |t| t.name }.join( ' ' ) }
    text(:genres) { tags.select{ |t| t.name[0,1] == "_" }.map{ |t| t.name }.join( ' ' ) }

    string :status
    integer :id
    boolean(:has_times) { (dates.reject { |d| d.times.blank? }).any? }
    time(:start_date, multiple: true) { dates.map{ |d| d.start_date } }
    time(:end_date, multiple: true) { (dates.map { |d| d.end_date }).compact }

    boolean :featured
    boolean :top
  end




def Event::tags_search query, event_id = nil, page=1, per_page=3
    search = Event.search(include: [:texts, :dates => :times]) do
      fulltext query do
        minimum_match 1
        boost_fields subjects: 4.0
        boost_fields context: 2.0
        boost_fields genres: 1.5
      end

      without :id, event_id

      # visibility constraints
      with :status, 'published'
      with :has_times, true

      # Combine restrictions (start_date OR ( end_date && !end_date.nil? ) )
      any_of do
        with(:start_date).greater_than_or_equal_to Time.now.to_s :db
        all_of do
          without :end_date, nil
          with(:end_date).greater_than_or_equal_to Time.now.to_s :db
        end
      end

      paginate page: page, per_page: per_page
    end
    search.results
  end

There is a way to make this work?

Thanks

Upvotes: 0

Views: 345

Answers (1)

pablomarti
pablomarti

Reputation: 2107

Strange, without should work... Use it with an array:

search = Post.search do
  ...
  without(:id, [post_id])
  paginate page: page, per_page: per_page
end

Upvotes: 1

Related Questions