【问题标题】:How to modify _source field of an Elasticsearch document如何修改 Elasticsearch 文档的 _source 字段
【发布时间】:2016-09-20 01:26:53
【问题描述】:

问题:有什么方法可以从文档的 _source 中清除 html 吗? html 的剥离可以是周期性的、触发的,或者理想情况下是在被索引时动态进行。

我将传入 elasticsearch 的数据与一个分析器进行索引,该分析器在被索引之前剥离不需要的 htmls 标签。

在查询/获取其 _source 字段,其中包含返回给客户端的 html 的原始内容。

注意:

  • 我无法在传递给 elasticsearch 之前清理数据,我无法控制这一点。
  • 我的客户从 elasticsearch 检索数据可以在呈现数据之前使用 javascript 进行剥离,但也不是一个选项。

【问题讨论】:

  • 使用transform,您可以编辑/更改_sourceelastic.co/guide/en/elasticsearch/reference/2.3/…。但请注意,它从 2.0.0 开始已被弃用,没有替换。
  • 感谢@andrei-stefan,最好不要通过不推荐使用的方式。另请注意,根据transform 文档“结果未存储在源中”。
  • 您无法更改_source。 _source 包含您放入弹性搜索的数据。您定义的所有映射都在您的输入 json 对象上设置,但它们不会修改 _source。

标签: javascript html elasticsearch


【解决方案1】:

_source 仅存储已编入索引的 JSON。你不能改变它。

现在,如果您想在索引和存储内容之前完全删除 html,您可以使用映射器附件插件 - 当您定义映射时,您可以将 content_type 分类为“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  "
    ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多