如何在 Solr 中包含 html 的字段中突出显示查询词 [英] How to highlight a query term in a field containing html in Solr

查看:21
本文介绍了如何在 Solr 中包含 html 的字段中突出显示查询词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正文字段,其中可能包含来自富文本编辑器的 html 文本.我希望能够从中返回突出显示的片段,但去掉了 html.

I have a body field that may contain html text from a rich text editor. I want to be able to return highlighted snippets from it, but with the html stripped out.

以下面被索引的文本为例:

Take the text below that gets indexed:

<p class="something">这里有一些文字和其他文字</p>

如果您搜索单词",您可能会得到以下高亮片段(取决于片段长度等):

If you search for 'words', you might get the following highlight snippet (depending on fragment length etc):

class="something">这里有一些文字,带有 <em>words</em>和其他文本

class="something">some text here with <em>words</em> and other text</p> or class="something">some text here with <em>words</em> and other text

HTML 拆分的地方,会导致前端出现渲染问题.我想在应用突出显示之前去除 html.我尝试了不同的高亮组件,但没有看到任何能起到作用的属性.

Where the HTML is split, which will cause rendering problems on the front-end. I want to strip out the html before the highlighting is applied. I've tried different highlighting components but haven't seen any properties that do the trick.

在索引之前我需要去除 HTML 吗?或者有其他的技术吗?

目前我使用 HTMLStripCharFilterFactory 在索引时去除 HTML,但高亮组件使用底层存储数据,即原始 html.

At the moment I'm using HTMLStripCharFilterFactory to strip out HTML at index time, but the highlighting component using the underlying stored data which is the original html.

推荐答案

我也有同样的问题,得出结论,唯一的办法就是使用两个字段,例如:

I also have the same problem and have concluded that the only way is to use two fields, for example:

  1. content_html:原始 html,使用 HtmlStripCharFilterFactory 所以只有 html 内容将被索引/标记
  2. content_stripped:仅文本内容,将用于突出显示的片段

例如,在 C# 中,我会使用 HtmlAgilityPack 将 HTML 加载到文档中,然后在根节点上调用 .Text.

In C# I'd use HtmlAgilityPack, for example, to load the HTML into a document then call .Text on the root node.

应该存储这两个字段,以便您可以将纯文本字段用于片段,并在希望突出显示整个字段时使用 html 字段.

Both fields should be stored so that you can use the text-only field for fragments and the html field for when you want the entire field to be highlighted.

我查看了 Solr 和 ElasticSearch,我发现没有内置方法,因为突出显示是对存储的任何内容执行的.

I looked into both Solr and ElasticSearch and from what I found there's no built-in way because highlighting is performed on whatever is stored.

相关:高亮时Solr Strip html带有存储的 html 字段

相关:如何处理SOLR中高亮片段中的html标签

这篇关于如何在 Solr 中包含 html 的字段中突出显示查询词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆