Sunspot Solr Rails - 使用'with'搜索多个模型

5

我很高兴能在我的应用程序中设置Solr。我已经成功地搜索了两个模型,但现在正在尝试使用“with”子句来微调我的搜索。

我的事件模型有:

class Event < ActiveRecord::Base
  searchable do
   text :headline, :info
   time :event_date
  end
end

我的场馆模型具有以下特点:

class Venue < ActiveRecord::Base
  searchable do
   text :name, :address_1, :address_2, :town, :postcode
  end
end

然后我有一个搜索控制器,在其中我之前成功地根据我的params[:search]返回了事件和场馆。现在我正在尝试返回即将到来的事件和场馆,但是由于场馆中没有'event_date',所以我现在只能得到事件。简而言之,我如何使with(:event_date)仅适用于Event模型?

class SearchController < ApplicationController
  def search
    @search = Sunspot.search [Event, Venue] do
      fulltext params[:search]
      with(:event_date).greater_than(Time.zone.now)
    end
    @results = @search.results
    respond_to do |format|
      format.json { render json: @results }
    end
  end  
end  

+1 星标,也想看看大家对此的想法。 - kobaltz
1个回答

1
你可以将这个字段添加到其他“可搜索”中,方法如下:
class Venue < ActiveRecord::Base
  searchable do
   text :name, :address_1, :address_2, :town, :postcode
   time :event_date { Time.zone.now }
  end
end

你可以在 { #这里 } 放置任何你想要的内容。

希望能对你有所帮助。


time :event_date do Time.zone.now end 理想情况下,可以在sunspot内部使用一些作用域查询,然后将结果组合起来。 - Nishant
你能澄清一下添加这个块允许你做什么吗?这样做是否允许 OP 实现他想要的功能? - aidan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接