您如何在 Elasticsearch 中突出显示 span_near 个查询?
How do you highlight span_near queries in Elasticsearch?
我试图根据 Elasticsearch 2.1.1 中的 span_near
查询突出显示文档,而 ES 错误地突出显示了一个实际上不是命中的术语因为它超出了相关范围。
我正在执行的步骤是:
创建索引
curl -XPUT 'http://localhost:9200/twitter/' -d '{
"mappings": {
"tweet": {
"properties": {
"message": {
"type": "string",
"term_vector": "with_positions_offsets",
"store": true
}
}
}
}
}'
索引文档
curl -XPUT 'localhost:9200/twitter/tweet/1?refresh=true' -d '{
"message" : "A new bonsai tree in the office. Bonsai!"
}'
搜索
curl -XGET 'http://localhost:9200/twitter/tweet/_search?pretty' -d '{
"query" : {
"span_near" : {
"clauses" : [
{"span_term": {"message": "new"}},
{"span_term": {"message": "bonsai"}}
],
"slop": 1,
"in_order": false
}
},
"highlight": {"fields": {"message": {"type": "plain"}}}
}'
上面的搜索正在返回:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.13561106,
"hits" : [ {
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_score" : 0.13561106,
"_source":{"message" : "A new bonsai tree in the office. Bonsai!"},
"highlight" : {
"message" : [ "A <em>new</em> <em>bonsai</em> tree in the office. <em>Bonsai</em>!" ]
}
} ]
}
}
如您所见,它错误地突出显示了字段末尾 "Bonsai" 的出现,该字段不在 "new" 的 1 个单词内。有几点需要注意:
- 这组完全相同的步骤针对 Elasticsearch 1.5.2.
生成 正确 突出显示结果
span_near
查询有一个未解决的错误,无法使用快速矢量荧光笔 (FVH) -- https://github.com/elastic/elasticsearch/issues/5496 -- 这就是我尝试使用 plain
上面的荧光笔
为了突出显示以处理 span_near
查询,我是否遗漏了什么?
我回去在测试环境中玩了一下,我认为正在发生的事情是你误解了 span_near 查询在做什么。我正在使用 Sense 来执行此操作,因此在语法上它可能看起来有点不同,但您应该能够跟随并重现它。
我首先通过映射创建了一个索引
PUT /testindex
{
"mappings": {
"post": {
"properties": {
"message": {
"type": "string",
"store": true,
"analyzer": "english",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
我省略了你的 属性 因为 term_vector 它对我的测试没有影响,我认为这是你尝试矢量突出显示时遗留下来的一些遗产 属性。
然后我用一些数据更新了索引
PUT /testindex/post/1
{
"message": "Bonsai new. A new bonsai tree in the office. Bonsai!"
}
然后执行您的查询,得到了相同的结果(不会 post 因为它与上面列出的相同)。
我认为混淆的地方在于模糊了荧光笔在做什么与 span_near。该查询正在搜索术语 new 和 bonsai,其中包含一些有效的术语。
要对此进行测试,请添加以下条目:
PUT /testindex/post/2
{
"message": "Bonsai blah blah new blah blah bonsai tree in the office Bonsai!"
}
运行 您的查询 returns 没有结果,因为新到盆景的距离现在大于一。将倾斜度更改为 5 或 6 之类的值会让您返回匹配项。
这与突出显示无关。突出显示是查看独立于 span 查询的术语,但如果该术语在返回的响应中,则突出显示将应用于我们看到的术语。突出显示在 2.0+ 中肯定经历了一些变化,因为我们在迁移到 2.0 引擎后进行了一些重写。
基于我看到的更改,突出显示现在似乎独立于查询工作,就好像它应用于响应 post 事件一样。我在这方面可能是错的,但它看起来好像完全符合预期。您会看到 Bonsai 突出显示,因为它是要搜索的术语之一。突出显示不考虑 slop 参数或 span_near 规则,只是结果中存在两个链接项。
我们将您输入的条目读作一个句子,但 ES 会去除标点符号并查看白色 space 是分隔符。索引和搜索您输入的内容会导致匹配,因为在 1 个间隔内有两个术语。然后根据搜索的术语而不是它们彼此落入的接近程度将突出显示应用于结果。
事实证明,这是 ES v2.1.1 中的一个已知错误,已通过此 pull request 修复:
https://github.com/elastic/elasticsearch/pull/15516。
根据该 PR 上的标签,此错误修复将成为 v2.1.2 的一部分。
我遇到了一些非常相似的事情,对 ES 1.7 和 2.3 进行了比较,并将其写在了 ES 讨论板上。现在这是一个 github 问题,以防有人想跟踪:https://github.com/elastic/elasticsearch/issues/18035
我试图根据 Elasticsearch 2.1.1 中的 span_near
查询突出显示文档,而 ES 错误地突出显示了一个实际上不是命中的术语因为它超出了相关范围。
我正在执行的步骤是:
创建索引
curl -XPUT 'http://localhost:9200/twitter/' -d '{
"mappings": {
"tweet": {
"properties": {
"message": {
"type": "string",
"term_vector": "with_positions_offsets",
"store": true
}
}
}
}
}'
索引文档
curl -XPUT 'localhost:9200/twitter/tweet/1?refresh=true' -d '{
"message" : "A new bonsai tree in the office. Bonsai!"
}'
搜索
curl -XGET 'http://localhost:9200/twitter/tweet/_search?pretty' -d '{
"query" : {
"span_near" : {
"clauses" : [
{"span_term": {"message": "new"}},
{"span_term": {"message": "bonsai"}}
],
"slop": 1,
"in_order": false
}
},
"highlight": {"fields": {"message": {"type": "plain"}}}
}'
上面的搜索正在返回:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.13561106,
"hits" : [ {
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_score" : 0.13561106,
"_source":{"message" : "A new bonsai tree in the office. Bonsai!"},
"highlight" : {
"message" : [ "A <em>new</em> <em>bonsai</em> tree in the office. <em>Bonsai</em>!" ]
}
} ]
}
}
如您所见,它错误地突出显示了字段末尾 "Bonsai" 的出现,该字段不在 "new" 的 1 个单词内。有几点需要注意:
- 这组完全相同的步骤针对 Elasticsearch 1.5.2. 生成 正确 突出显示结果
span_near
查询有一个未解决的错误,无法使用快速矢量荧光笔 (FVH) -- https://github.com/elastic/elasticsearch/issues/5496 -- 这就是我尝试使用plain
上面的荧光笔
为了突出显示以处理 span_near
查询,我是否遗漏了什么?
我回去在测试环境中玩了一下,我认为正在发生的事情是你误解了 span_near 查询在做什么。我正在使用 Sense 来执行此操作,因此在语法上它可能看起来有点不同,但您应该能够跟随并重现它。
我首先通过映射创建了一个索引
PUT /testindex
{
"mappings": {
"post": {
"properties": {
"message": {
"type": "string",
"store": true,
"analyzer": "english",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
我省略了你的 属性 因为 term_vector 它对我的测试没有影响,我认为这是你尝试矢量突出显示时遗留下来的一些遗产 属性。
然后我用一些数据更新了索引
PUT /testindex/post/1
{
"message": "Bonsai new. A new bonsai tree in the office. Bonsai!"
}
然后执行您的查询,得到了相同的结果(不会 post 因为它与上面列出的相同)。
我认为混淆的地方在于模糊了荧光笔在做什么与 span_near。该查询正在搜索术语 new 和 bonsai,其中包含一些有效的术语。 要对此进行测试,请添加以下条目:
PUT /testindex/post/2
{
"message": "Bonsai blah blah new blah blah bonsai tree in the office Bonsai!"
}
运行 您的查询 returns 没有结果,因为新到盆景的距离现在大于一。将倾斜度更改为 5 或 6 之类的值会让您返回匹配项。
这与突出显示无关。突出显示是查看独立于 span 查询的术语,但如果该术语在返回的响应中,则突出显示将应用于我们看到的术语。突出显示在 2.0+ 中肯定经历了一些变化,因为我们在迁移到 2.0 引擎后进行了一些重写。
基于我看到的更改,突出显示现在似乎独立于查询工作,就好像它应用于响应 post 事件一样。我在这方面可能是错的,但它看起来好像完全符合预期。您会看到 Bonsai 突出显示,因为它是要搜索的术语之一。突出显示不考虑 slop 参数或 span_near 规则,只是结果中存在两个链接项。
我们将您输入的条目读作一个句子,但 ES 会去除标点符号并查看白色 space 是分隔符。索引和搜索您输入的内容会导致匹配,因为在 1 个间隔内有两个术语。然后根据搜索的术语而不是它们彼此落入的接近程度将突出显示应用于结果。
事实证明,这是 ES v2.1.1 中的一个已知错误,已通过此 pull request 修复:
https://github.com/elastic/elasticsearch/pull/15516。
根据该 PR 上的标签,此错误修复将成为 v2.1.2 的一部分。
我遇到了一些非常相似的事情,对 ES 1.7 和 2.3 进行了比较,并将其写在了 ES 讨论板上。现在这是一个 github 问题,以防有人想跟踪:https://github.com/elastic/elasticsearch/issues/18035