Ruby on rails 太阳黑子这个词是怎么发现的?

Ruby on rails 太阳黑子这个词是怎么发现的?,ruby-on-rails,ruby,search,highlighting,sunspot,Ruby On Rails,Ruby,Search,Highlighting,Sunspot,例如,我想突出显示文本中找到的单词,如图所示 据我所知,我必须遵循以下步骤: 1) 在我的模型中,我必须向要突出显示的字段添加:stored=>true选项: searchable do text :title, :stored => true text :description end 2) 在控制器中,我必须声明要突出显示的字段: def search @search = Article.search do keywords params[:

例如,我想突出显示文本中找到的单词,如图所示

据我所知,我必须遵循以下步骤:

1) 在我的模型中,我必须向要突出显示的字段添加
:stored=>true
选项:

searchable do 
    text :title, :stored => true
    text :description
end
2) 在控制器中,我必须声明要突出显示的字段:

def search
    @search = Article.search do
        keywords params[:search] do
            highlight :title
        end
    end
end
3) 在我看来,我不确定该怎么办,我尝试了以下方法:

- @search.each_hit_with_result do |hit, result|
    %p= link_to raw(hit_title(hit)), article_path(result)
这是做什么的方法
hit\u title

def hit_title(hit)
    if highlight = hit.highlight(:title)
        highlight.format { |word| "<font color='green'>#{word}</font>" }
    else
        h(hit.result.title)
    end
end
def hit_标题(hit)
如果highlight=hit.highlight(:title)
highlight.format{word}“{word}”
其他的
h(命中率、结果、标题)
结束
结束
但它并没有像预期的那样工作,它总是突出显示标题的第一个单词,即使搜索的单词在它的末尾


有没有更简单的方法

是否使用子字符串搜索?我在这里遇到了同样的问题,并且意识到通过遵循sunspot wiki教程来启用子字符串匹配导致了这个问题。

我偶然遇到了这个问题,正在寻找一个解决方案,从rails视图上的sunspot search渲染高光

我在任何地方都找不到现成的解决方案,所以我用了这篇文章的一部分来制作我的一个。 我对rails很陌生,所以这可能不是完全的RoR方式

在我的例子中,我对两个字段进行了全文搜索,分别称它们为
notes
description

为了能够将突出显示呈现为html,我引入了一个包含记录id、列名称及其突出显示值的哈希值,并进行了适当的格式化。这使我能够突出显示不同字段上的搜索结果

entry.rb:

searchable do
    text :description, :stored => true
    text :notes, :stored => true
end
条目\u controller.rb:

@search = Entry.search
    if params[:search].nil? || params[:search].empty?
        stext=''
    else
        stext=params[:search]
    end
    fulltext stext, :highlight => true
    paginate(page: params[:page], :per_page => 10)
end
@entries=@search.results

@results=Hash.new
@search.hits.each do |hit|
    hit.highlights(:description).each do |highlight|
        id=hit.primary_key.to_s.to_sym
        fr=highlight.format { |word| "<result>#{word}</result>" }
        @results.merge!(id => ["description",fr])
    end
    hit.highlights(:notes).each do |highlight|
        id=hit.primary_key.to_s.to_sym
        fr=highlight.format { |word| "<result>#{word}</result>" }
        @results.merge!(id => ["notes",fr])
    end
end
@search=Entry.search
如果参数[:search].nil?| |参数[:搜索]。是否为空?
stext=''
其他的
stext=params[:搜索]
结束
全文stext,:highlight=>true
分页(页码:参数[:页码],:每页=>10)
结束
@entries=@search.results
@结果=Hash.new
@search.hits.each do | hit|
hit.highlights(:description)。每个do|highlight|
id=hit.primary_key.to_.to_sym
fr=highlight.format{word}“{word}”
@结果,合并!(id=>[“说明”,fr])
结束
hit.highlights(:notes)。每个do|highlight|
id=hit.primary_key.to_.to_sym
fr=highlight.format{word}“{word}”
@结果,合并!(id=>[“注释”,fr])
结束
结束
在视图中,无论我想在哪里渲染这些值,我都会执行以下操作:

<% @entries.each do |f| %>
    <% j=f[:id].to_s.to_sym %>
    <% if !@results[j].nil? && @results[j][0]=="description" %>
        <%= @results[j][1].html_safe %>
    <% else  %>
        <%= f[:description] %>
    <% end %>
[...] (likewise for notes)
<% end %>

[…](注释也是如此)

请注意,我为
标记创建了css定义,以使文本引人注目。

代码在我看来很适合突出显示标题中的第一个匹配单词,因为我有类似的代码。您是否尝试过重建solr索引并重新启动服务器

另外,是否可以尝试将solrconfig.xml恢复为其默认值?有人在修改solrconfig.xml后遇到了类似的问题,参考

如果要覆盖solrconfig.xml中的突出显示选项,请在此网站上搜索max_代码段。您可能想尝试以下选项:

highlight :title, :max_snippets => 3, :fragment_size => 0  # 0 is infinite

这可能是您的问题,也可能是粘贴错误。。。。如果highlight=hit.highlight(:title)不,这不是打字错误。它是if子句中的赋值,用于干燥和压缩代码;)请将结果粘贴到hit.highlight(:title)。检查我按照上的说明添加了过滤器。那里的注释说,“我们也可以使用NGramFilterFactory进行子字符串搜索,而不仅仅是pre-/postfix搜索。”我仍然遇到与@zolter相同的问题。关于我可以做什么不同的想法吗?我只是按照这个链接:它起了作用。