Link Search Menu Expand Document Documentation Menu

路径层次结构分词器

path_hierarchy 分词器通过在每个层次级别将文件系统类路径(或类似的分层结构)分解为词元来对其进行分词。此分词器在处理分层数据(例如文件路径、URL 或任何其他带分隔符的路径)时特别有用。

使用示例

以下示例请求创建一个名为 my_index 的新索引,并使用 path_hierarchy 分词器配置一个分析器。

PUT /my_index
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "my_path_tokenizer": {
          "type": "path_hierarchy"
        }
      },
      "analyzer": {
        "my_path_analyzer": {
          "type": "custom",
          "tokenizer": "my_path_tokenizer"
        }
      }
    }
  }
}

生成的词元

使用以下请求检查使用该分析器生成的词元

POST /my_index/_analyze
{
  "analyzer": "my_path_analyzer",
  "text": "/users/john/documents/report.txt"
}

响应包含生成的词元

{
  "tokens": [
    {
      "token": "/users",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 0
    },
    {
      "token": "/users/john",
      "start_offset": 0,
      "end_offset": 11,
      "type": "word",
      "position": 0
    },
    {
      "token": "/users/john/documents",
      "start_offset": 0,
      "end_offset": 21,
      "type": "word",
      "position": 0
    },
    {
      "token": "/users/john/documents/report.txt",
      "start_offset": 0,
      "end_offset": 32,
      "type": "word",
      "position": 0
    }
  ]
}

参数

path_hierarchy 分词器可以通过以下参数进行配置。

参数 必需/可选 数据类型 描述
delimiter(分隔符) 可选 字符串 指定用于分隔路径组件的字符。默认值为 /
replacement(替换字符) 可选 字符串 配置用于替换词元中分隔符的字符。默认值为 /
buffer_size(缓冲区大小) 可选 整数 指定缓冲区大小。默认值为 1024
reverse(反向) 可选 布尔型 如果为 true,则反向生成词元。默认值为 false
skip(跳过) 可选 整数 指定分词时跳过的初始词元(级别)数。默认值为 0

使用 delimiter 和 replacement 参数的示例

以下示例请求配置了自定义 delimiterreplacement 参数。

PUT /my_index
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "my_path_tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "\\",
          "replacement": "\\"
        }
      },
      "analyzer": {
        "my_path_analyzer": {
          "type": "custom",
          "tokenizer": "my_path_tokenizer"
        }
      }
    }
  }
}

使用以下请求检查使用该分析器生成的词元

POST /my_index/_analyze
{
  "analyzer": "my_path_analyzer",
  "text": "C:\\users\\john\\documents\\report.txt"
}

响应包含生成的词元

{
  "tokens": [
    {
      "token": "C:",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": """C:\users""",
      "start_offset": 0,
      "end_offset": 8,
      "type": "word",
      "position": 0
    },
    {
      "token": """C:\users\john""",
      "start_offset": 0,
      "end_offset": 13,
      "type": "word",
      "position": 0
    },
    {
      "token": """C:\users\john\documents""",
      "start_offset": 0,
      "end_offset": 23,
      "type": "word",
      "position": 0
    },
    {
      "token": """C:\users\john\documents\report.txt""",
      "start_offset": 0,
      "end_offset": 34,
      "type": "word",
      "position": 0
    }
  ]
}
剩余 350 字符

有问题?

想做贡献?