URL 解码处理器
urldecode
处理器可用于解码日志数据或其他文本字段中的 URL 编码字符串。这可以使数据更具可读性且更易于分析,尤其是在处理包含特殊字符或空格的 URL 或查询参数时。
以下是 urldecode
处理器的语法:
{
"urldecode": {
"field": "field_to_decode",
"target_field": "decoded_field"
}
}
配置参数
下表列出了 urldecode
处理器所需和可选的参数。
参数 | 必需/可选 | 描述 |
---|---|---|
field | 必需 | 包含要解码的 URL 编码字符串的字段。 |
target_field | 可选 | 存储解码字符串的字段。如果未指定,则解码字符串将存储在与原始编码字符串相同的字段中。 |
ignore_missing | 可选 | 指定处理器是否应忽略不包含指定 field 的文档。如果设置为 true ,则处理器将忽略 field 中的缺失值,并保持 target_field 不变。默认为 false 。 |
description | 可选 | 处理器的简要描述。 |
if | 可选 | 运行处理器的条件。 |
ignore_failure | 可选 | 指定即使处理器遇到错误是否仍继续运行。如果设置为 true ,则忽略失败。默认为 false 。 |
on_failure | 可选 | 处理器失败时要运行的处理器列表。 |
tag | 可选 | 处理器的标识符标签。有助于调试以区分相同类型的处理器。 |
使用处理器
按照以下步骤在管道中使用处理器。
步骤 1:创建管道
以下查询创建一个名为 urldecode_pipeline
的管道,该管道使用 urldecode
处理器解码 encoded_url
字段中的 URL 编码字符串,并将解码后的字符串存储在 decoded_url
字段中:
PUT _ingest/pipeline/urldecode_pipeline
{
"description": "Decode URL-encoded strings",
"processors": [
{
"urldecode": {
"field": "encoded_url",
"target_field": "decoded_url"
}
}
]
}
步骤 2(可选):测试管道
建议在摄取文档之前测试您的管道。
要测试管道,请运行以下查询
POST _ingest/pipeline/urldecode_pipeline/_simulate
{
"docs": [
{
"_source": {
"encoded_url": "https://example.com/search?q=hello%20world"
}
}
]
}
响应
以下示例响应确认管道按预期工作
{
"docs": [
{
"doc": {
"_index": "_index",
"_id": "_id",
"_source": {
"decoded_url": "https://example.com/search?q=hello world",
"encoded_url": "https://example.com/search?q=hello%20world"
},
"_ingest": {
"timestamp": "2024-04-25T23:16:44.886165001Z"
}
}
}
]
}
步骤 3:摄取文档
以下查询将文档摄取到名为 testindex1
的索引中
PUT testindex1/_doc/1?pipeline=url_decode_pipeline
{
"encoded_url": "https://example.com/search?q=url%20decode%20test"
}
响应
上述请求将文档索引到 testindex1
索引中,并索引所有包含 encoded_url
字段的文档,这些文档由 urldecode_pipeline
处理以填充 decoded_url
字段,如下面的响应所示:
{
"_index": "testindex1",
"_id": "1",
"_version": 67,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 68,
"_primary_term": 47
}
步骤 4(可选):检索文档
要检索文档,请运行以下查询
GET testindex1/_doc/1
响应
响应包含原始的 encoded_url
字段和 decoded_url
字段。
{
"_index": "testindex1",
"_id": "1",
"_version": 67,
"_seq_no": 68,
"_primary_term": 47,
"found": true,
"_source": {
"decoded_url": "https://example.com/search?q=url decode test",
"encoded_url": "https://example.com/search?q=url%20decode%20test"
}
}