如何突出显示与太阳黑子相关的单词?
我想突出显示文本中找到的单词,例如,如此处所示。
据我所知,我必须遵循以下步骤:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这个问题,正在寻找一种解决方案,可以在轨道视图上渲染来自太阳黑子搜索的高光。
我在任何地方都没有找到太多现成的解决方案,因此我使用了这篇文章的一部分来制作我的一个。
我对 Rails 还很陌生,所以这可能不完全是 RoR 方式。
就我而言,我对两个字段进行了全文搜索,将它们称为
notes
和description
。为了能够将突出显示的内容呈现为 html,我引入了一个值的哈希值,其中包含记录的 id、列的名称及其突出显示的值,并进行了适当的格式化。这使我能够突出显示不同字段的搜索结果。
Entry.rb:
entries_controller.rb:
在视图上,无论我想要呈现这些值的任何位置,我都会执行以下操作:
请注意,我为
标记创建了一个 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
anddescription
.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:
entries_controller.rb:
and on the view, wherever I want to render any value of those, I do the following:
Please, note I created a css definition for
<result>
markup to make the text notable.对于突出显示标题中的第一个匹配单词,代码对我来说看起来不错,因为我有类似的代码。您是否尝试过重建 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/ 。您可能想尝试以下选项
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
您使用子字符串搜索吗?我在这里遇到了同样的问题,并意识到通过遵循 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.