如何突出显示与太阳黑子相关的单词?

发布于 2024-12-01 00:13:25 字数 979 浏览 2 评论 0原文

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

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

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_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

但它不能按预期工作,它总是突出显示标题的第一个单词,即使搜索到的单词位于标题的末尾。

有没有更简单的方法来做到这一点?

I want to highlight found words in text, for example, as shown here.

As far as I know I must follow these steps:

1) In my model, I must add :stored => true option to the field which I want to highlight:

searchable do 
    text :title, :stored => true
    text :description
end

2) In my controller, I have to declare which field I want highlighted:

def search
    @search = Article.search do
        keywords params[:search] do
            highlight :title
        end
    end
end

3) In the view I'm not sure what to do, I tried this:

- @search.each_hit_with_result do |hit, result|
    %p= link_to raw(hit_title(hit)), article_path(result)

It is what doing method hit_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

But it doesn't work as expected, it always highlights the first word of the title, even if the searched word is at the end of it.

Is there an easier way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我爱人 2024-12-08 00:13:25

我遇到了这个问题,正在寻找一种解决方案,可以在轨道视图上渲染来自太阳黑子搜索的高光。

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

就我而言,我对两个字段进行了全文搜索,将它们称为 notesdescription

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

Entry.rb:

searchable do
    text :description, :stored => true
    text :notes, :stored => true
end

entries_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
@[email protected]

@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

在视图上,无论我想要呈现这些值的任何位置,我都会执行以下操作:

<% @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 定义使文字引人注目。

I bumped into this looking for a solution to render highlights from sunspot search on rails view.

I didn't find much of a ready solution anywhere, so I used part of this post to make one of my one.
I am quite new to rails so this might not be fully the RoR way.

In my case, I did a full text search on two fields, call them notes and description.

In order to be able to render to html the highlights, I introduced a hash of values containing the id of the record, the name of the column and its highlighted value, adequately formatted. This allows me to highlight the search results on different fields.

entry.rb:

searchable do
    text :description, :stored => true
    text :notes, :stored => true
end

entries_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
@[email protected]

@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

and on the view, wherever I want to render any value of those, I do the following:

<% @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 %>

Please, note I created a css definition for <result> markup to make the text notable.

吻泪 2024-12-08 00:13:25

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

另外,您可以尝试将 solrconfig.xml 恢复为其默认值吗?有人在修改solrconfig.xml后遇到了类似的问题,参考https://groups.google.com/forum/#!searchin/ruby-sunspot/highlight/ruby-sunspot/kHq0Dw35UWs/ANIUwERArTQJ

如果您想覆盖 solrconfig 中的突出显示选项。 xml,在此站点上搜索 max_snippets http://outoftime.github.io/ 。您可能想尝试以下选项

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

Code looks good to me for highlighting the first matching word in the title, since I have similar code. Have you tried rebuilding your solr index and restarting the servers?

Also, can you try reverting your solrconfig.xml to its default values? Someone had a similar problem after modifying solrconfig.xml, Ref https://groups.google.com/forum/#!searchin/ruby-sunspot/highlight/ruby-sunspot/kHq0Dw35UWs/ANIUwERArTQJ

If you want to override the highlighting option in solrconfig.xml, search for max_snippets on this site http://outoftime.github.io/ . You may want to try options like

highlight :title, :max_snippets => 3, :fragment_size => 0  # 0 is infinite
疯狂的代价 2024-12-08 00:13:25

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

Are you using substring search? I've got the same problem here and realized that enabling substring match by following sunspot wiki tutorial led to the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文