【问题标题】:Filter association in search results using rails sunspot使用 rails sunspot 过滤搜索结果中的关联
【发布时间】:2020-04-24 01:47:53
【问题描述】:

我有这个模型

class Subject
  has_many: contents
  has_many: titles, :through => :contents

  searchable do
    text :titles, :default_boost => 5, :stored => true do
      my_titles = titles.map { |title| title.try(:name) }

      if my_titles.present?
        my_titles += titles.map do |title|
          title.path.map { |title| title.try(:name) }
        end
      end

      my_titles.flatten.uniq.join(" ")
    end
  end
end

class Title
  has_ancestry
end

当我搜索主题时,它工作正常,但我需要添加该功能以删除视图中不必要的标题。例如,当我搜索名为“人类生物学”的主题时,我会得到结果

Subject: Science 1
Titles: 
  - Environmental Science >> Human Biology # <-----this record need to stay
  - Chemistry 1 >> Chemistry 2 # <-----remove this
  - Physics 1 >> Physics 2 # <-----remove this

Subject: Science 2
Titles:
    Environmental Science >> Human Biology # <-----this record need to stay
    Chemistry 1 >> Chemistry 2 # <-----remove this

主题出现在我的搜索结果中,而我需要根据搜索内容过滤标题。

这是我的观点:

<% @subjects.each_hit_with_result.each do |hit, subject| %>
  <% subject.titles.uniq.each do |title| %>
    <br/>
    <%=raw title.path.map {|e| link_to("#{e.name}", contents_url(:title => e.id) )}.join(' > ') %>
    <br/>
    <% end %>
  <% end %>
<% end %>

我尝试获取搜索参数并使用选择功能,但我认为这不是正确的实现。

【问题讨论】:

  • 请再编辑一遍,并尝试改进标点符号和大小写。 SO 不是留言板或论坛,它就像一本在线参考书,这些东西在其中很重要。没有它,您会让我们更难理解您的要求。请参阅“How to Ask”及其链接页面,以及“Is it OK to downvote questions because of bad grammar?”。
  • @theTinMan 我的语法非常抱歉。但我试图说明我的问题
  • 语法与您试图说明问题无关。我强烈建议您对问题的文本使用语法检查器。
  • 我只是添加更多插图并等待可以帮助我的人谢谢。你已经否决了我的问题我应该怎么听:)
  • 你如何确定“不必要的主题”?您只想返回与您的查询匹配的项目吗?如果这些其他项目与您的查询无关,为什么它们会提供命中?

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


【解决方案1】:

我猜this 会帮助你进行正确的搜索。

像这样的

searchable do
  text :name
  join(:titles, target: Title, type: :text, join: { from: :subject_id, to: :id })
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    相关资源
    最近更新 更多