指纹分词器

指纹分词器 #

指纹(fingerprint)分词器会创建一个文本指纹。该分词器对从输入生成的词项(词元)进行排序和去重,然后使用一个分隔符将它们连接起来。它通常用于数据去重,对于包含相同单词的相似输入,无论单词顺序如何,它都可以产生相同的输出结果。

指纹分词器由以下组件组成:

  1. 标准分词生成器
  2. 词元小写化过滤器
  3. ASCII 词元过滤器
  4. 停用词词元过滤器
  5. 指纹词元过滤器

参数说明 #

指纹分词器可以使用以下参数进行配置。

参数必填/可选数据类型描述
separator可选字符串在对词项进行词元生成、排序和去重后,用于连接这些词项的字符。默认值为一个空格( )。
max_output_size可选整数定义输出词元的最大大小。如果连接后的指纹超过此大小,将被截断。默认值为 255
stopwords可选字符串或字符串列表自定义的停用词列表。默认值为 _none_
stopwords_path可选字符串包含停用词列表的文件的路径(相对于配置目录的绝对路径或相对路径)。

参考样例 #

以下命令创建一个名为 my_custom_fingerprint_index 带有指纹分词器的索引:

PUT /my_custom_fingerprint_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_fingerprint_analyzer": {
          "type": "fingerprint",
          "separator": "-",
          "max_output_size": 50,
          "stopwords": ["to", "the", "over", "and"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "my_field": {
        "type": "text",
        "analyzer": "my_custom_fingerprint_analyzer"
      }
    }
  }
}

产生的词元 #

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

POST /my_custom_fingerprint_index/_analyze
{
  "analyzer": "my_custom_fingerprint_analyzer",
  "text": "The slow turtle swims over to the dog"
}

返回内容中包含了生成的词元:

{
  "tokens": [
    {
      "token": "dog-slow-swims-turtle",
      "start_offset": 0,
      "end_offset": 37,
      "type": "fingerprint",
      "position": 0
    }
  ]
}

深度定制 #

如果需要深度定制,你可以定义一个包含指纹分词器组件的分词器:

PUT /custom_fingerprint_analyzer
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_fingerprint": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding",
            "fingerprint"
          ]
        }
      }
    }
  }
}