【问题标题】:ElasticSearch highlights after stripping HTML tags剥离 HTML 标签后的 ElasticSearch 高亮显示
【发布时间】:2016-11-18 08:49:36
【问题描述】:

我正在 ES 2.3.3 上构建 Elastic Search 索引,并且我已将以下字段定义为具有以下方式的一个子字段

"properties": {
        "content": {
            "type": "string",
            "index_options": "offsets",
            "store": "yes",
            "fields": {
                "base": {
                    "type": "string",
                    "analyzer": "base_analyzer"
                },
            }
}

我在设置中定义了base_analyzer,以便去除 HTML 内容

"base_analyzer":
                {
                    "tokenizer":    "standard",
                    "char_filter":  [ "html_strip"]
                }

我想做的是在content.base(从HTML标签中剥离的content字段)上执行搜索并突出显示搜索词,我正在按照以下方式进行操作

"query":  {
           {"match": {"content.base": {"query": "this is what I'm searching"}}},
            },
            "highlight": {
              "fields": {
               "content.base": {}
              }
            }

问题是如果我在_search 中使用以下查询,我仍然会在突出显示的字段中获得 HTML 标记。你知道为什么会这样吗?

【问题讨论】:

  • 因为高亮是在实际的_source或者存储的数据上做的,如果store: true,就是原来的输入。分析器仅使用术语构建倒排索引,突出显示不使用它。
  • 感谢@AndreiStefan,但如果我想存储content.base 字段,在该子字段中添加"store":"yes" 是否足够?这也适用于突出显示吗?或者有没有办法将content.base 添加到_source
  • 使用存储字段或_source都没有关系。它不适用于突出显示。这就是突出显示的工作方式,您无法更改它。您唯一能做的就是在索引之前剥离 html。
  • 感谢您澄清这一点!太糟糕的突出显示有这个限制。我想我只需要重新索引我的文档并删除 HTML。
  • 是的。默认情况下,不存储字段。

标签: elasticsearch


【解决方案1】:

现在,如果您想在索引和存储内容之前完全删除 html,您可以使用映射器附件插件 - 当您定义映射时,您可以将 content_type 分类为“html”。 您将能够在没有 html 标记的情况下突出显示。

映射器附件对很多事情都很有用,尤其是在您处理多种文档类型时,但最值得注意的是 - 我相信仅将其用于剥离 html 标签就足够了(您不能使用 html_strip char过滤器)。

不过只是一个警告 - 不会存储任何 html 标记。因此,如果您确实需要这些标签,我建议您定义另一个字段来存储原始内容。另一个注意事项:您不能为映射器附件文档指定多字段,因此您需要将其存储在映射器附件文档之外。请参阅下面的工作示例。

您需要生成此映射:

{
  "html5-es" : {
    "aliases" : { },
    "mappings" : {
      "document" : {
        "properties" : {
          "delete" : {
            "type" : "boolean"
          },
          "file" : {
            "type" : "attachment",
            "fields" : {
              "content" : {
                "type" : "string",
                "store" : true,
                "term_vector" : "with_positions_offsets",
                "analyzer" : "autocomplete"
              },
              "author" : {
                "type" : "string",
                "store" : true,
                "term_vector" : "with_positions_offsets"
              },
              "title" : {
                "type" : "string",
                "store" : true,
                "term_vector" : "with_positions_offsets",
                "analyzer" : "autocomplete"
              },
              "name" : {
                "type" : "string"
              },
              "date" : {
                "type" : "date",
               "format" : "strict_date_optional_time||epoch_millis"
              },
              "keywords" : {
                "type" : "string"
              },
              "content_type" : {
                "type" : "string"
              },
          "content_length" : {
                "type" : "integer"
              },
              "language" : {
                "type" : "string"
              }
            }
          },
          "hash_id" : {
            "type" : "string"
          },
          "path" : {
            "type" : "string"
          },
          "raw_content" : {
            "type" : "string",
            "store" : true,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "raw"
          },
          "title" : {
            "type" : "string"
          }
        }
      }
    },
    "settings" : { //insert your own settings here },
    "warmers" : { }
  }
}

这样在 NEST 中,我将这样组装内容:

Attachment attachment = new Attachment();
attachment.Content =   Convert.ToBase64String(File.ReadAllBytes("path/to/document"));
attachment.ContentType = "html";

Document document = new Document();
document.File = attachment;
document.RawContent = InsertRawContentFromString(originalText);

我在 Sense 中对此进行了测试 - 结果如下:

"file": {
    "_content": "PGh0bWwgeG1sbnM6TWFkQ2FwPSJodHRwOi8vd3d3Lm1hZGNhcHNvZnR3YXJlLmNvbS9TY2hlbWFzL01hZENhcC54c2QiPg0KICA8aGVhZCAvPg0KICA8Ym9keT4NCiAgICA8aDE+VG9waWMxMDwvaDE+DQogICAgPHA+RGVsZXRlIHRoaXMgdGV4dCBhbmQgcmVwbGFjZSBpdCB3aXRoIHlvdXIgb3duIGNvbnRlbnQuIENoZWNrIHlvdXIgbWFpbGJveC48L3A+DQogICAgPHA+wqA8L3A+DQogICAgPHA+YXNkZjwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD4xMDwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD5MYXZlbmRlci48L3A+DQogICAgPHA+wqA8L3A+DQogICAgPHA+MTAvNiAxMjowMzwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD41IDA5PC9wPg0KICAgIDxwPsKgPC9wPg0KICAgIDxwPjExIDQ3PC9wPg0KICAgIDxwPsKgPC9wPg0KICAgIDxwPkhhbGxvd2VlbiBpcyBpbiBPY3RvYmVyLjwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD5qb2c8L3A+DQogIDwvYm9keT4NCjwvaHRtbD4=",
    "_content_length": 0,
    "_content_type": "html",
    "_date": "0001-01-01T00:00:00",
    "_title": "Topic10"
},
"delete": false,
"raw_content": "<h1>Topic10</h1><p>Delete this text and replace it with your own content. Check your mailbox.</p><p> </p><p>asdf</p><p> </p><p>10</p><p> </p><p>Lavender.</p><p> </p><p>10/6 12:03</p><p> </p><p>5 09</p><p> </p><p>11 47</p><p> </p><p>Halloween is in October.</p><p> </p><p>jog</p>"
},
"highlight": {
"file.content": [
    "\n    <em>Topic10</em>\n\n    Delete this text and replace it with your own content. Check your mailbox.\n\n     \n\n    asdf\n\n     \n\n    10\n\n     \n\n    Lavender.\n\n     \n\n    10/6 12:03\n\n     \n\n    5 09\n\n     \n\n    11 47\n\n     \n\n    Halloween is in October.\n\n     \n\n    jog\n\n  "
    ]
}

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多