【问题标题】:Sunspot Solr Rails - Search Multiple Models using 'with'Sunspot Solr Rails - 使用“with”搜索多个模型
【发布时间】:2013-09-19 01:50:49
【问题描述】:

我在我的应用程序中安装 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) 仅适用于事件模型?

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 已加星标 也希望看到对此的一些想法

标签: ruby-on-rails ruby-on-rails-3 search solr sunspot


【解决方案1】:

您可以做的一件事是将此字段添加到其他“可搜索”字段,如下所示:

class Venue < ActiveRecord::Base
  searchable do
   text :name, :address_1, :address_2, :town, :postcode
   time :event_date { Time.zone.now }
  end
end

你可以在 { #here } 中放任何你想要的东西

希望对你有帮助

【讨论】:

  • ``` time :event_date do Time.zone.now end``` 理想的情况是从太阳黑子中进行一些范围查询,然后组合结果。
  • 你能澄清一下添加块允许你做什么吗?这是否允许 OP 做他想做的事?
猜你喜欢
  • 2015-05-28
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多