簡體   English   中英

突出顯示Elasticsearch中針對多值字段的全部內容

[英]Highlight whole content in Elasticsearch for multivalue fields

使用Elasticsearch的突出顯示功能:

"highlight": {
  "fields": {
    "tags": { "number_of_fragments": 0 }
  }
}

使用number_of_fragments: 0 ,不會生成任何片段,但會返回該字段的全部內容。 這對於短文本很有用,因為文檔可以正常顯示,人們可以輕松掃描突出顯示的部分。

當文檔包含具有多個值的數組時,如何使用它?

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 }
    }
  }
}

現在我想向用戶展示:

1結果:

文件1,標記為:

“one hit tag”,“two foo tag”,“three hit tag”,“four foo tag”

不幸的是,這是查詢的結果:

{
     "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"
                 ]
              }
           }
        ]
     }
  }

我該如何使用它來:

   "tags": [
      "one <em>hit</em> tag",
      "two foo tag",
      "three <em>hit</em> tag",
      "four foo tag"
   ]

一種可能性是從突出顯示的字段中剝離<em> html標簽。 然后在原始字段中查找它們:

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

這並沒有收到最漂亮的代碼的價格,但我認為它完成了工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM