使用Elasticsearch,我可以用不同的HTML标记突出显示不同的匹配标记吗?

2024-10-01 15:38:31 发布

您现在位置:Python中文网/ 问答频道 /正文

目前正在学习ES,但我非常渴望实现这一点

我知道您可以在查询中使用highlightpre_tagspost_tags键突出显示具有不同标记的不同字段。。。但是,如果返回的片段对于每个单独标识的单词都有不同的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>"]

相反,我想要这个领域

"the other data day data was walking through some interesting woods and data had an interesting thought about some 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”的词干都是“change”,而“element”和“element”的词干都是“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>

Tags: the标记datatagssomeblueredcolor
1条回答
网友
1楼 · 发布于 2024-10-01 15:38:31

快速矢量荧光灯是一个很好的起点。我还没有学习W/Frand,所以不要考虑下面的权威性,但是基于内置的{a1},我们可以做这样的事情:

PUT multilang_index
{
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "term_vector": "with_positions_offsets",
        "fields": {
          "french": {
            "type": "text",
            "analyzer": "french",
            "term_vector": "with_positions_offsets"
          }
        }
      }
    }
  }
}

FYIfrench分析器可以是reimplemented/extended as shown here

在吞食了英语和;法国的例子:

POST multilang_index/_doc
{
  "description": "the other data day data was walking through some interesting woods and data had an interesting thought about some data"
}

POST multilang_index/_doc
{
  "description": "Les autres éléments ont été changés car on a appliqué un changement à chaque élément"
}

我们可以像这样查询interesting data

POST multilang_index/_search
{
  "query": {
    "simple_query_string": {
      "query": "interesting data",
      "fields": [
        "description"
      ]
    }
  },
  "highlight": {
    "fields": {
      "description": {
       "type": "fvh",
       "pre_tags": ["<font color=\"red\">", "<font color=\"blue\">"],
       "post_tags": ["</font>", "</font>"]
      }
    },
    "number_of_fragments": 0
  }
}

屈服

the other <font color="blue">data</font> day <font color="blue">data</font> 
was walking through some <font color="red">interesting</font> woods and 
<font color="blue">data</font> had an <font color="red">interesting</font>
thought about some <font color="blue">data</font>

类似地,对于changer élément

POST multilang_index/_search
{
  "query": {
    "simple_query_string": {
      "query": "changer élément",
      "fields": [
        "description.french"
      ]
    }
  },
  "highlight": {
    "fields": {
      "description.french": {
       "type": "fvh",
       "pre_tags": ["<font color=\"red\">", "<font color=\"blue\">"],
       "post_tags": ["</font>", "</font>"]
      }
    },
    "number_of_fragments": 0
  }
}

屈服

Les autres <font color="blue">éléments</font> ont été 
<font color="red">changés</font> car on a appliqué un 
<font color="red">changement</font> à chaque <font color="blue">élément</font>

对我来说,这看起来是正确的


请注意,^{}顺序是根据simple_query_string查询中与第一个匹配的令牌强制执行的。当查询changer élément时,description中的木瓦éléments首先匹配,但导致它匹配的是第二个标记(élément),因此是bluehtml标记而不是red

相关问题 更多 >

    热门问题