Rails Solr:如何通过模型ID排除结果

时间:2013-04-15 15:26:46

标签: ruby-on-rails solr

我正在尝试使用Solr排除具有特定ID的结果。 我做的是用可搜索的“整数id”定义然后在Model.search中使用“without”我做“没有:id,id”

这是我的代码示例:

 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

有一种方法可以使这项工作?

由于

1 个答案:

答案 0 :(得分:1)

奇怪,without应该有效......将它与数组一起使用:

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