【发布时间】:2021-05-13 12:11:05
【问题描述】:
目前正在学习 ES,但我非常热衷于实现这一点。
我知道您可以在查询中使用highlight 的pre_tags 和post_tags 键以不同的标签突出显示不同的字段...但是是否可以提供标记- up 字符串,其中返回的片段对于每个单独的识别词都有不同的 HTML 颜色标记,例如使用simple query string?
所以我用“有趣的数据”查询并返回一个文档字段,如下所示:
the other day I was walking through the woods and I had an <font color="blue">interesting</font>
thought about some <font color="red">data</font>
我的意思不仅仅是标签“无意识地”交替:同样,您可以使用Fast Vector Highlighter,例如:
"highlight": {
"fields": {
"description": {
"pre_tags": ["<b>", "<em>"],
"post_tags": ["</b>", "</em>"]
相反,我想要该字段
“其他数据日数据正在穿过一些有趣的树林 和data对一些数据有一个有趣的想法”
这样返回:
the other <font color="red">data</font> day <font color="red">data</font> was walking through some <font color="blue">
interesting</font> woods and <font color="red">data</font> had an <font color="blue">
interesting</font> thought about some <font color="red">data</font>
我以前使用 Lucene(即 Java)进行编码,并且我确实设法实现了这种东西,主要是跳过了一些障碍。
注意,对此的一个答案可能是“忘记 ES 返回标记的文本,只需使用 re.sub( r'\bdata\b', '<font color="red">data</font>', field_string ) 应用您自己的标签”。
这对于像这样的简单用例来说是可以的。但它不适用于词干分析器。例如,举一个法语例子:搜索查询是“changer élément”。我想要以下标记结果:
Les autres <font color="red">éléments</font> ont été <font color="blue">
changés</font> car on a appliqué un <font color="blue">changement</font>
à chaque <font color="red">élément</font>
即“changer”、“changes”和“changement”都源于“chang”,而“élément”和“éléments”都源于“element”。因此,该字段的标准突出显示返回是:
Les autres <em>éléments</em> ont été <em>changés</em> car on a appliqué un
<em>changement</em> à chaque <em>élément</em>
【问题讨论】:
标签: python elasticsearch highlight elasticsearch-query elasticsearch-highlight