HTML strip 处理器
html_strip
处理器从传入文档的字符串字段中移除 HTML 标签。当索引来自网页或其他可能包含 HTML 标记的来源数据时,此处理器非常有用。HTML 标签将被换行符 (\n
) 替换。
以下是 html_strip
处理器的语法:
{
"html_strip": {
"field": "webpage"
}
}
配置参数
下表列出了 html_strip
处理器所需和可选的参数。
参数 | 必需/可选 | 描述 |
---|---|---|
field(字段) | 必需 | 要从中移除 HTML 标签的字符串字段。 |
target_field(目标字段) | 可选 | 接收剥离 HTML 标签后的纯文本版本的字段。如果未指定,则字段会就地更新。 |
ignore_missing(忽略缺失) | 可选 | 指定处理器是否应忽略不包含指定字段的文档。默认为 false 。 |
description(描述) | 可选 | 处理器的目的或配置说明。 |
if(如果) | 可选 | 指定条件性执行处理器。 |
ignore_failure(忽略失败) | 可选 | 指定忽略处理器失败。参见处理管道失败。 |
on_failure(失败时) | 可选 | 指定处理器在执行期间失败时要运行的处理器列表。这些处理器按照指定的顺序执行。请参阅处理管道失败。 |
tag(标签) | 可选 | 处理器的标识符标签。有助于调试以区分相同类型的处理器。 |
使用处理器
按照以下步骤在管道中使用处理器。
步骤 1:创建管道
以下查询创建一个名为 strip-html-pipeline
的管道,该管道使用 html_strip
处理器从 description 字段中移除 HTML 标签,并将处理后的值存储在一个名为 cleaned_description
的新字段中。
PUT _ingest/pipeline/strip-html-pipeline
{
"description": "A pipeline to strip HTML from description field",
"processors": [
{
"html_strip": {
"field": "description",
"target_field": "cleaned_description"
}
}
]
}
步骤 2(可选):测试管道
建议在摄取文档之前测试您的管道。
要测试管道,请运行以下查询
POST _ingest/pipeline/strip-html-pipeline/_simulate
{
"docs": [
{
"_source": {
"description": "This is a <b>test</b> description with <i>some</i> HTML tags."
}
}
]
}
响应
以下示例响应确认管道按预期工作
{
"docs": [
{
"doc": {
"_index": "_index",
"_id": "_id",
"_source": {
"description": "This is a <b>test</b> description with <i>some</i> HTML tags.",
"cleaned_description": "This is a test description with some HTML tags."
},
"_ingest": {
"timestamp": "2024-05-22T21:46:11.227974965Z"
}
}
}
]
}
步骤 3:摄取文档
以下查询将文档摄取到名为 products
的索引中。
PUT products/_doc/1?pipeline=strip-html-pipeline
{
"name": "Product 1",
"description": "This is a <b>test</b> product with <i>some</i> HTML tags."
}
响应
响应显示请求已将文档索引到 products
索引中,并将索引所有包含 HTML 标签的 description
字段的文档,同时将纯文本版本存储在 cleaned_description
字段中。
{
"_index": "products",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
步骤 4(可选):检索文档
要检索文档,请运行以下查询
GET products/_doc/1
响应
响应同时包含原始的 description
字段和已移除 HTML 标签的 cleaned_description
字段。
{
"_index": "products",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"cleaned_description": "This is a test product with some HTML tags.",
"name": "Product 1",
"description": "This is a <b>test</b> product with <i>some</i> HTML tags."
}
}