[英]Highlight whole content in Elasticsearch for multivalue fields
Using the highlight feature of Elasticsearch: 使用Elasticsearch的突出显示功能:
"highlight": {
"fields": {
"tags": { "number_of_fragments": 0 }
}
}
With number_of_fragments: 0
, no fragments are produced, but the whole content of the field is returned. 使用number_of_fragments: 0
,不会生成任何片段,但会返回该字段的全部内容。 This is useful for short texts, because documents can be displayed as normal, and people can easily scan for highlighted parts. 这对于短文本很有用,因为文档可以正常显示,人们可以轻松扫描突出显示的部分。
How do you use this when a document contains an array with multiple values? 当文档包含具有多个值的数组时,如何使用它?
PUT /test/doc/1
{
"tags": [
"one hit tag",
"two foo tag",
"three hit tag",
"four foo tag"
]
}
GET /test/doc/_search
{
"query": {
"match": { "tags": "hit"}
},
"highlight": {
"fields": {
"tags": { "number_of_fragments": 0 }
}
}
}
Now what I would like to show the user: 现在我想向用户展示:
1 result: 1结果:
Document 1, tagged with: 文件1,标记为:
"one hit tag", "two foo tag", "three hit tag", "four foo tag" “one hit tag”,“two foo tag”,“three hit tag”,“four foo tag”
Unfortunately, this is the result of the query: 不幸的是,这是查询的结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.10848885,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "1",
"_score": 0.10848885,
"_source": {
"tags": [
"one hit tag",
"two foo tag",
"three hit tag",
"four foo tag"
]
},
"highlight": {
"tags": [
"one <em>hit</em> tag",
"three <em>hit</em> tag"
]
}
}
]
}
}
How can I use this to get to: 我该如何使用它来:
"tags": [
"one <em>hit</em> tag",
"two foo tag",
"three <em>hit</em> tag",
"four foo tag"
]
One possibility is to strip the <em>
html-tags from the highlighted fields. 一种可能性是从突出显示的字段中剥离<em>
html标签。 Then look them up in the original field: 然后在原始字段中查找它们:
tags = [
"one hit tag",
"two foo tag",
"three hit tag",
"four foo tag"
]
highlighted = [
"one <em>hit</em> tag",
"three <em>hit</em> tag",
]
highlighted.each do |highlighted_tag|
if (index = tags.index(highlighted_tag.gsub(/<\/?em>/, '')))
tags[index] = highlighted_tag
end
end
puts tags #=>
# one <em>hit</em> tag
# two foo tag
# three <em>hit</em> tag
# four foo tag
This does not receives a price for the most beautiful code, but I reckon it gets the job done. 这并没有收到最漂亮的代码的价格,但我认为它完成了工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.