如何突出显示在 Apache Solr 中索引的文档中的搜索文本

How to highlight a searched text inside a document which is indexed in Apache Solr

我正在使用 Apache Solr 8.6 使用 Linux 中的 POST 工具为文档编制索引,如 Apache Solr Reference Guide 中所述。

POST 工具命令

bin/post -c testcore /testdocs/

文档已成功编入索引。现在,当我在 Solr Admin UI 中搜索字符串例如:hello 时,它返回匹配的文档。

现在我想突出显示文档中作为内容存在的字符串 "hello"。但我无法实现同样的目标。 我尝试启用突出显示并在 hl.fl=text_* 中输入 text_*。但是我无法实现结果。但是,如果此搜索字符串出现在任何索引字段中,它会在结果中正确突出显示。

managed-schema.xml 中,我添加了内容和文本字段,如下所示。

<field name="content" type="text_general" indexed="false" stored="true" multiValued="false"/> 

<field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>

我试过以下查询:

http://localhost:8983/solr/testcore/select?q=_text_%3a*hello*&hl=true&hl.fl=content&hl.usephrasehighlighter=true

http://localhost:8983/solr/testcore/select?q=_text_%3a*hello*&hl=true&hl.fl=text_*&hl.usephrasehighlighter=true

如果你能进一步指导我,我将不胜感激。

编辑:根据建议

我已经更改了内容和文本字段,如下所示,但它也不起作用。这意味着当我搜索文本字符串时,它不会将文档中搜索到的字符串突出显示为内容。

<field name="content" type="text_general" indexed="true" stored="true" multiValued="false"/> 
    
<field name="text" type="text_general" indexed="true" stored="true" multiValued="true"/>

最后通过修改 manage-schema.xml 文件中的 _text _ 字段解决了这个问题。我已将 stored="false" 的值更改为 stored="true",如下面的语法所示。

<field name="_text_" type="text_general" indexed="true" stored="true" multiValued="false"/>

然后我重新启动了 solr 并重新索引了文档。

现在我已经触发查询以突出显示文本,如下所示。在此查询中,我使用了 hl=on & hl.fl=_text _ 来突出显示评论。

查询以突出显示搜索到的字符串

http://localhost:8983/solr/testcore/select?q=_text_%3a*hello*&hl=on&hl.fl=_text_