【问题标题】:Rails search with sunspot使用太阳黑子进行 Rails 搜索
【发布时间】:2014-05-17 10:52:25
【问题描述】:

我在工作和公司之间有一对多的关联,并且我已经使用 sunspot gem 实现了一个工作搜索表单,但是我想在搜索 company_name 时搜索公司的所有工作结果我该怎么做

这是我的工作模式

class Job < ActiveRecord::Base

belongs_to :company




searchable do
  text :job_title, boost: 4
  text :profile_recherche
end 

这是我的公司模式

class Company < ActiveRecord::Base

has_many :jobs

这是我的工作搜索控制器

def search

@jobs = Sunspot.search(Job) do
  keywords params[:query]
  fulltext params[:query]
  paginate(page: params[:page], per_page: 1)
end.results

respond_to do |format|
  format.html { render :action => "index" }
end

end

【问题讨论】:

  • 您在什么时候搜索公司?这只是搜索工作......您不应该有公司模型的可搜索字段吗?

标签: ruby-on-rails search solr full-text-search sunspot


【解决方案1】:

听起来您想通过 job_title、profile_reserche 和 company.name 来搜索 Jobs。如果我猜对了你想要什么,改变你的可搜索块应该这样做:

class Job < ActiveRecord::Base

  belongs_to :company

  searchable do
    text :job_title, boost: 4
    text :profile_recherche
    text :company_name do
      company.name
    end
  end
end

这里需要注意的是,如果您在标记、词干、n-gram 以及这些不同字段之间的任何内容中有重叠,您可能会得到一些有趣的搜索结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2014-03-29
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多