Elasticsearch7.x——分析器Analyzer与分析接口Analyze API详解

目录

Analyzer & Analyze API

1、Analyzer的组成

分词器的作用是当一个文档被索引的时候,分词器从文档中提取出若干词元来支持索引的存储和搜索。

分析器(analyzer)都由三种构件组成的:character filters , tokenizers , token filters。

  • Character filters(字符过滤器)
    字符过滤器接收原始文本作为字符流,并可以通过添加、删除和更改字符来转换流。例如,可以使用字符过滤器将印度-阿拉伯数字(0123456789)转换成阿拉伯-拉丁语中的(0123456789),或从流中剥离<b>等HTML元素。分析器可能有零个或多个字符过滤器,它们会按顺序应用。
  • Tokenizer(分词器)
    分词器接收到一串字符,将其分解为单独的词语(通常是单个单词),并输出词语流。例如,空白分词器每当看到任何空格时,将文本分解成词语。它将文本"Quick brown fox" 转换为词语[Quick,brown,fox!]。分词器还负责记录每个词语的顺序或位置以及体现词语的原始单词开始和结束字符偏移量。分析器必须只有一个分词器。
  • Token filters(token过滤器)
    词语过滤器接收词语流,并可以添加,删除和修改词语。例如,小写词语过滤器将所有词语转换为小写,停止词语过滤器会从词语流中删除常用字(停止字),同义词词语过滤器会将同义词引入到词语流中。词语过滤器不允许改变每个词语的位置或字符偏移量。分析器可以具有零个或多个词语过滤器,它们会按顺序应用。

三者顺序: Character Filters—>Tokenizer—>Token Filter

三者个数:Character Filters(0个或多个) + Tokenizer + Token Filters(0个或多个)

2、内置的Character Filters

2.1、HTML strip 字符过滤器

html_strip 字符过滤器去除像 <b> 这样的 HTML 元素,替换像&amp&

示例:

GET /_analyze
{
  "tokenizer": "keyword",
  "char_filter": [
    "html_strip"
  ],
  "text": "<p>I&apos;m so <b>happy</b>!</p>"
}

返回:

{
  "tokens" : [
    {
      "token" : "\nI'm so happy!\n",
      "start_offset" : 0,
      "end_offset" : 32,
      "type" : "word",
      "position" : 0
    }
  ]
}

示例:创建索引时使用html_scrip

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "char_filter": [
            "html_strip"
          ]
        }
      }
    }
  }
}

示例:自定义html_strip,跳过对<b>的过滤

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "char_filter": [
            "my_custom_html_strip_char_filter"
          ]
        }
      },
      "char_filter": {
        "my_custom_html_strip_char_filter": {
          "type": "html_strip",
          "escaped_tags": [
            "b"
          ]
        }
      }
    }
  }
}

2.2、Mapping 字符过滤器

Mapping 字符过滤器用指定的替换替换任何出现的指定字符串。

映射字符过滤器接受键和值的映射。每当它遇到与键相同的字符串时,它都会用与该键相关联的值替换它们。

注意:匹配是贪婪的

示例:

GET /_analyze
{
  "tokenizer": "keyword",
  "char_filter": [
    {
      "type": "mapping",
      "mappings": [
        "٠ => 0",
        "١ => 1",
        "٢ => 2",
        "٣ => 3",
        "٤ => 4",
        "٥ => 5",
        "٦ => 6",
        "٧ => 7",
        "٨ => 8",
        "٩ => 9"
      ]
    }
  ],
  "text": "My license plate is ٢٥٠١٥"
}

返回:

{
  "tokens" : [
    {
      "token" : "My license plate is 25015",
      "start_offset" : 0,
      "end_offset" : 25,
      "type" : "word",
      "position" : 0
    }
  ]
}

示例:自定义Mappp过滤器

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_mappings_char_filter"
          ]
        }
      },
      "char_filter": {
        "my_mappings_char_filter": {
          "type": "mapping",
          "mappings": [
            ":) => _happy_",
            ":( => _sad_"
          ]
        }
      }
    }
  }
}

GET /my_index/_analyze
{
  "tokenizer": "keyword",
  "char_filter": [ "my_mappings_char_filter" ],
  "text": "I'm delighted about it :("
}

返回:

{
  "tokens" : [
    {
      "token" : "I'm delighted about it _sad_",
      "start_offset" : 0,
      "end_offset" : 25,
      "type" : "word",
      "position" : 0
    }
  ]
}

示例:使用文本文件拓展映射

在实际的使用中,如果我们的 mapping 列表比较长,而且放在命令中不容易维护。我们可以在 Elasticsearch 的安装目录下的 config 子目录下创建一个叫做 analysis 的目录。

$ pwd
/Users/liuxg/elastic/elasticsearch-8.6.1/config
$ mkdir analysis
$ cd analysis

我们在这个目录下创建一个叫做 mappings.txt 的文件:

$ pwd
/Users/liuxg/elastic/elasticsearch-8.6.1/config
$ mkdir analysis
$ cd analysis
$ vi mappings.txt
$ cat analysis/mappings.txt
LOL => laughing out loud
BRB => be right back
OMG => oh my god

指定映射文件:

PUT index_with_mapping_char_filter_file
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_social_abbreviations_analyzer": { 
          "tokenizer": "keyword",
          "char_filter": [
            "my_social_abbreviations" 
          ]
        }
      },
      "char_filter": {
        "my_social_abbreviations": { 
          "type": "mapping",
          "mappings_path": "analysis/mappings.txt"
        }
      }
    }
  }
}

注意:mappings_path 是包含 key => value 映射的文件的路径。此路径必须是绝对路径或相对于 config 位置的路径,并且文件必须是 UTF-8 编码的。 文件中的每个映射必须用换行符分隔。必须指定 mappings_path 或 mappings 参数。

2.3、Patter replace 字符过滤器

pattern_replace 字符过滤器用指定的替换替换与正则表达式匹配的任何字符。

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_char_filter"
          ]
        }
      },
      "char_filter": {
        "my_char_filter": {
          "type": "pattern_replace",
          "pattern": "(\\d+)-(?=\\d)",
          "replacement": "$1_"
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "My credit card is 123-456-789"
}

返回:

{
  "tokens" : [
    {
      "token" : "My",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "credit",
      "start_offset" : 3,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "card",
      "start_offset" : 10,
      "end_offset" : 14,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "is",
      "start_offset" : 15,
      "end_offset" : 17,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "123",
      "start_offset" : 18,
      "end_offset" : 21,
      "type" : "<NUM>",
      "position" : 4
    },
    {
      "token" : "456",
      "start_offset" : 22,
      "end_offset" : 25,
      "type" : "<NUM>",
      "position" : 5
    },
    {
      "token" : "789",
      "start_offset" : 26,
      "end_offset" : 29,
      "type" : "<NUM>",
      "position" : 6
    }
  ]
}

3、内置的Tokenizer

3.1、Standard Tokenizer(标准分词器)

standard(标准)分析器是默认分析器,如果没有指定分析器,则使用该分析器。

配置参数:

  • max_token_length:最大词语长度。如果看到超过此长度的词语,则以max_token_length分割。默认为255。

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "standard",
          "max_token_length": 5
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ The, 2, QUICK, Brown, Foxes, jumpe, d, over, the, lazy, dog's, bone ]

3.2、Simple Pattern Tokenizer(简单正则分词器)

使用正则表达式捕获匹配文本作为术语。它支持的正则表达式功能集必Pattern Tokenizer更有限,但标记化通常更快。

配置参数:

  • pattern:用于匹配的正则表达式

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "simple_pattern",
          "pattern": "[0123456789]{3}"
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "fd-786-335-514-x"
}

返回的词项:

[ 786, 335, 514 ]

3.3、Simple Pattern Split Tokenizer(简单正则拆分分词器)

使用正则表达式在模式匹配时将输入拆分成词项。它支持的正则表达式功能集比模式标记器更有限,但标记化通常更快。

配置参数:

  • pattern:用于匹配的正则表达式

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "simple_pattern_split",
          "pattern": "_"
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "an_underscored_phrase"
}

返回的词项:

[ an, underscored, phrase ]

3.4、Pattern Tokenizer(正则分词器)

当文本与单词分隔符匹配时,使用正则表达式将文本拆分为词项,或者将匹配的文本捕获为词项。

默认模式是\W+,每当遇到非单词字符时都会拆分文本。

配置参数:

  • pattern:用于匹配的正则表达式
  • flags:正则表达式标志。竖线分隔,例如CASE_INSENSITIVE|COMMENTS
  • group:将哪个捕获组提取为词项。默认为-1(拆分)。

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "pattern",
          "pattern": ","
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "comma,separated,values"
}

返回的词项:

[ comma, separated, values ]

3.5、Whitespace Tokenizer(空格分词器)

每当文本遇到空格字符时,空格标记器都会将文本分解为词项。

示例:

POST _analyze
{
  "tokenizer": "whitespace",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog's, bone. ]

3.6、Char Group Tokenizer(字符组分词器)

只要文本遇到已定义集合中的字符,char_group分词器就会将文本分解为多个词项。在需要简单的定制标记化,并且使用Pattern tokenizer的开销是不可接受的情况下,它最有用。

配置参数:

  • tokenize_on_chars:包含要将字符串标记化的字符列表的列表。每当遇到此列表中的字符时,都会开启一个新的词项。它接受单个字符,如-whitespaceletterdigitpunctuationsymbol

示例:

POST _analyze
{
  "tokenizer": {
    "type": "char_group",
    "tokenize_on_chars": [
      "whitespace",
      "-",
      "\n"
    ]
  },
  "text": "The QUICK brown-fox"
}

返回:

{
  "tokens" : [
    {
      "token" : "The",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "QUICK",
      "start_offset" : 4,
      "end_offset" : 9,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "brown",
      "start_offset" : 10,
      "end_offset" : 15,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : "fox",
      "start_offset" : 16,
      "end_offset" : 19,
      "type" : "word",
      "position" : 3
    }
  ]
}

3.7、Classic Tokenizer(经典分词器)

经典分词器是一种基于语法的标记器,适用于英语文档。该标记器具有用于特殊处理首字母缩写、公司名称、电子邮件地址和互联网主机名的启发式。然而,这些规则并不总是有效的,对于除英语以外的大多数语言都不能很好地工作。

配置参数:

  • max_token_length:最大词语长度。如果看到超过此长度的词语,则以max_token_length分割。默认为255。

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "classic",
          "max_token_length": 5
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ The, 2, QUICK, Brown, Foxes, jumpe, d, over, the, lazy, dog's, bone ]

3.8、Edge n-gram tokenizer(边缘n元语法分词器)

无论何时遇到指定字符列表中的一个字符,edge_ngram分词器都会首先将文本分解为单词,然后发出每个单词的N-gram,其中N-gram的开头固定在单词的开头。

配置参数:

  • min_gram:单个词源的最小字符长度,默认为1
  • max_gram:单个词源的最大字符长度,默认为2
  • token_chars:应包含在词项中的字符类。ElasticSearch将拆分不属于指定类的字符。默认为。字符类可以是以下任一类型:
    • letter:例如:a,b, ï or
    • digit:例如:3or7
    • whitespace:例如:" "or "\n"
    • punctuation:例如:!or''
    • symbol:例如:$or
    • custom:使用custom_token_chars定制
  • custom_token_chars:自定义字符。例如,将其设置为+-_将使记号生成器将加号、减号和下划线符号视为记号的一部分。

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "2 Quick Foxes."
}

返回的词项:

[ Qu, Qui, Quic, Quick, Fo, Fox, Foxe, Foxes ]

3.9、Keyword Tokenizer(关键词分词器)

关键字分词器是一个“noop”标记器,它接受给定的任何文本,并输出与单个术语完全相同的文本。

示例:

POST _analyze
{
  "tokenizer": "keyword",
  "filter": [ "lowercase" ],
  "text": "john.SMITH@example.COM"
}

返回:

{
  "tokens" : [
    {
      "token" : "john.smith@example.com",
      "start_offset" : 0,
      "end_offset" : 22,
      "type" : "word",
      "position" : 0
    }
  ]
}

3.10、Letter Tokenizer(字母分词器)

每当遇到非字母字符时,字母分词器都会将文本拆分成多个词项。多用于英语,不适用与汉语。

示例:

POST _analyze
{
  "tokenizer": "letter",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回词项:

[ The, QUICK, Brown, Foxes, jumped, over, the, lazy, dog, s, bone ]

3.11、Lowercase Tokenizer(小写分词器)

小写分词器与字母分词器一样,在文本遇到非字母字符时将文本分解为词项,但它也会将所有词项小写。它在功能上等同于字母分词器和lowcase token filter的组合,但效率更高,因为它一次执行这两个步骤。

示例:

POST _analyze
{
  "tokenizer": "lowercase",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]

3.12、N-gram tokenizer(N元语法分词器)

每当遇到指定字符列表中的一个字符时,n-gram分词器首先将文本分解为单词,然后发出指定长度的每个单词的N个单词。

N元语法就像一个滑动窗口,在单词上移动——指定长度的连续字符序列。它们对于查询不使用空格或具有长复合词的语言很有用,比如德语。

配置参数:

  • min_gram:单个词源的最小字符长度,默认为1
  • max_gram:单个词源的最大字符长度,默认为2
  • token_chars:应包含在词项中的字符类。ElasticSearch将拆分不属于指定类的字符。默认为。字符类可以是以下任一类型:
    • letter:例如:a,b, ï or
    • digit:例如:3or7
    • whitespace:例如:" "or "\n"
    • punctuation:例如:!or''
    • symbol:例如:$or
    • custom:使用custom_token_chars定制
  • custom_token_chars:自定义字符。例如,将其设置为+-_将使记号生成器将加号、减号和下划线符号视为记号的一部分。

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 3,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "2 Quick Foxes."
}

返回词项:

[ Qui, uic, ick, Fox, oxe, xes ]

3.13、Path Hierarchy Tokenizer(路径层次分词器)

Path_Hierarchy分词器采用类似于文件系统路径的层级值,在路径分隔符上拆分,并为树中的每个组件发起一个词项。

配置参数:

  • delimiter:路径分隔符,默认是/
  • replacement:用于分隔符的可选替换字符。默认为分隔符。
  • buffer_size:单次读入字词缓冲区的字符数。默认为1024。词项缓冲区将以此大小增长,直到所有文本都被使用完。建议不要更改此设置。
  • reverse:如果设置为True,则以相反的顺序发送token。默认为False。
  • skip:要跳过的初始token数。默认为0。

示例:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "-",
          "replacement": "/",
          "skip": 2
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "one-two-three-four-five"
}

返回的词项:

[ /three, /three/four, /three/four/five ]

如果reverse=true:

[ one/two/three/, two/three/, three/ ]

3.14、Thai Tokenizer(泰语分词器)

使用Java附带的泰语分割算法将泰语文本分割成单词。一般情况下,其他语言的文本将被视为与standard tokenizer相同。

示例:

POST _analyze
{
  "tokenizer": "thai",
  "text": "การที่ได้ต้องแสดงว่างานดี"
}

返回的词项:

[ การ, ที่, ได้, ต้อง, แสดง, ว่า, งาน, ดี ]

3.15、UAX URL Email Tokenizer(URL、Email分词器)

类似于standard tokenizer,不同之处在于它将URL和电子邮件地址识别为单个token。

配置参数:

  • max_token_length:最大词语长度。如果看到超过此长度的词语,则以max_token_length分割。默认为255。

示例:

POST _analyze
{
  "tokenizer": "uax_url_email",
  "text": "Email me at john.smith@global-international.com"
}

返回的词项:

[ Email, me, at, john.smith@global-international.com ]

4、内置的Token Filters

4.1、Apostrophe token filter(撇号词元过滤器)

去掉撇号'后面的所有字符,包括撇号本身。

此过滤器包含在Elasticearch的内置土耳其语分析器中。它使用Lucene的ApostropheFilter,这是为土耳其语构建的。

示例:

GET /_analyze
{
  "tokenizer" : "standard",
  "filter" : ["apostrophe"],
  "text" : "Istanbul'a veya Istanbul'dan"
}

返回的词项:

[ Istanbul, veya, Istanbul ]

加入一个分析器中:

PUT /apostrophe_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "standard_apostrophe" : {
                    "tokenizer" : "standard",
                    "filter" : ["apostrophe"]
                }
            }
        }
    }
}

4.2、ASCII folding token filter(ASCII 折叠词元过滤器)

将不在基本拉丁Unicode块中的字母、数字和符号字符(前127个ASCII字符)转换为其等效的ASCII字符(如果存在),例如将à转换为a

示例:

GET /_analyze
{
  "tokenizer" : "standard",
  "filter" : ["asciifolding"],
  "text" : "açaí à la carte"
}

返回的词项:

[ acai, a, la, carte ]

加入到分析器:

PUT /asciifold_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "standard_asciifolding" : {
                    "tokenizer" : "standard",
                    "filter" : ["asciifolding"]
                }
            }
        }
    }
}

4.3、CJK bigram token filter(中日韩二元组词元过滤器)

从中日韩(中文、日语和韩语)token中形成二元语法。

此过滤器包含在Elasticearch的内置CJK语言分析器中。它使用Lucene的CJKBigramFilter。

示例:

GET /_analyze
{
  "tokenizer" : "standard",
  "filter" : ["cjk_bigram"],
  "text" : "東京都は、日本の首都であり"
}

返回的词项:

[ 東京, 京都, 都は, 日本, 本の, の首, 首都, 都で, であ, あり ]

添加到分析器:

PUT /cjk_bigram_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "standard_cjk_bigram" : {
                    "tokenizer" : "standard",
                    "filter" : ["cjk_bigram"]
                }
            }
        }
    }
}

4.4、CJK width token filter(中日韩宽度词元过滤器)

标准化CJK(中文、日文和韩文)字符的宽度差异,如下所示:

  • 将全角ASCII字符变体合并为等效的基本拉丁字符。
  • 将半角片假名字符变体合并为等效的假名字符

示例:

GET /_analyze
{
  "tokenizer" : "standard",
  "filter" : ["cjk_width"],
  "text" : "シーサイドライナー"
}

返回:

シーサイドライナー

加入到分析器:

PUT /cjk_width_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "standard_cjk_width" : {
                    "tokenizer" : "standard",
                    "filter" : ["cjk_width"]
                }
            }
        }
    }
}

4.5、Classic token filter(经典词元过滤器)

对classic tokenizer生成的词项执行可选的后处理。

这个过滤器去掉词尾的英语所有格(‘s),去掉缩略词中的圆点(.)。它使用Lucene的ClassicFilter。

示例:

GET /_analyze
{
  "tokenizer" : "classic",
  "filter" : ["classic"],
  "text" : "The 2 Q.U.I.C.K. Brown-Foxes jumped over the lazy dog's bone."
}

返回:

[ The, 2, QUICK, Brown, Foxes, jumped, over, the, lazy, dog, bone ]

加入到分析器:

PUT /classic_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "classic_analyzer" : {
                    "tokenizer" : "classic",
                    "filter" : ["classic"]
                }
            }
        }
    }
}

4.6、Common grams token filter(近义词词元过滤器)

此过滤器,用于为经常出现的术语生成二进制码。 单项仍被索引。 当我们不想完全忽略常用术语时,它可以用作“ 停止令牌过滤器”的替代方法。如"the quick brown is a fox",将被过滤成 “the”, “the_quick”, “quick”, “brown”, “brown_is”, “is_a”, “a_fox”, “fox”。假设“the”,“is”和“a”是常用词。当启用query_mode时,过滤器将删除常用单词和单个术语,后跟通用单词。 应在搜索分析器中启用此参数。如,“the quick brown is a fox” 将被过滤成 “the_quick”, “quick”, “brown_is”, “is_a”, “a_fox”, “fox”。

配置参数:

  • common_words:(必选*,字符串数组)要使用的常用词列表。此参数或COMMON_WORDS_PATH参数是必需的。
  • common_words_path:(必选*,字符串)路径(相对于config位置,或绝对)到常用单词列表。 每个单词应该在自己的“行”(用换行符分隔)。 该文件必须是UTF-8编码。此参数或COMMON_WORDS参数是必需的。
  • ignore_case:(可选,布尔值)如果为True,则常见单词匹配不区分大小写。默认为False。
  • query_mode:生成二进制,然后删除常用单词和单个术语,后跟一个通用单词(默认为false )

示例:

GET /_analyze
{
  "tokenizer" : "whitespace",
  "filter" : [
    {
      "type": "common_grams",
      "common_words": ["is", "the"]
    }
  ],
  "text" : "the quick fox is brown"
}

返回:

[ the, the_quick, quick, fox, fox_is, is, is_brown, brown ]

加入到分析器:

PUT /common_grams_example
{
    "settings": {
        "analysis": {
            "analyzer": {
              "index_grams": {
                  "tokenizer": "whitespace",
                  "filter": ["common_grams"]
              }
            },
            "filter": {
              "common_grams": {
                  "type": "common_grams",
                  "common_words": ["a", "is", "the"]
              }
            }
        }
    }
}

4.7、Conditional token filter(脚本词元过滤器)

使用脚本过滤词元:

配置参数:

  • filter:(必选,词元过滤器数组)这些过滤器将按顺序应用于token。
  • script:脚本

示例:

GET /_analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "condition",
      "filter": [ "lowercase" ],
      "script": {
        "source": "token.getTerm().length() < 5"
      }
    }
  ],
  "text": "THE QUICK BROWN FOX"
}

返回的词元:

[ the, QUICK, BROWN, fox ]

自定义并加入分析器:

PUT /palindrome_list
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_reverse_first_token": {
          "tokenizer": "whitespace",
          "filter": [ "reverse_first_token" ]
        }
      },
      "filter": {
        "reverse_first_token": {
          "type": "condition",
          "filter": [ "reverse" ],
          "script": {
            "source": "token.getPosition() === 0"
          }
        }
      }
    }
  }
}

4.8、Decimal digit token filter(十进制数字词元过滤器)

将Unicode Decimal_Number常规类别中的所有数字转换为0-9。

示例:

GET /_analyze
{
  "tokenizer" : "whitespace",
  "filter" : ["decimal_digit"],
  "text" : "१-one two-२ ३"
}

返回的词项:

[ 1-one, two-2, 3]

加入到分析器:

PUT /decimal_digit_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "whitespace_decimal_digit" : {
                    "tokenizer" : "whitespace",
                    "filter" : ["decimal_digit"]
                }
            }
        }
    }
}

4.9、Delimited payload token filter(负载分隔词元过滤器)

根据指定的分隔符将令牌流分隔为令牌和负载。例如,可以使用|分隔符,将the|1 quick|2 fox|3拆分为the、quick、fox,有效负载为1、2、3

有效负载是与token位置相关联的用户定义的二进制数据,并存储为Base64编码的字节。默认情况下,ElasticSearch不存储token有效负载。

配置参数:

  • delimiter:(可选)分隔符,默认为|
  • encoding:(可选)存储的负载的数据类型,有效值包括:
    • float:默认,Float
    • identity:Characters
    • int:Integer

示例:

GET _analyze
{
  "tokenizer": "whitespace",
  "filter": ["delimited_payload"],
  "text": "the|0 brown|10 fox|5 is|0 quick|10"
}

返回的词项:

[ the, brown, fox, is, quick ]

加入到分析器:

PUT delimited_payload
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_delimited_payload": {
          "tokenizer": "whitespace",
          "filter": [ "delimited_payload" ]
        }
      }
    }
  }
}

返回存储的负载:

PUT text_payloads
{
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "term_vector": "with_positions_payloads",
        "analyzer": "payload_delimiter"
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "payload_delimiter": {
          "tokenizer": "whitespace",
          "filter": [ "delimited_payload" ]
        }
      }
    }
  }
}

POST text_payloads/_doc/1
{
  "text": "the|0 brown|3 fox|4 is|0 quick|10"
}

GET text_payloads/_termvectors/1
{
  "fields": [ "text" ],
  "payloads": true
}
{
  "_index" : "text_payloads",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "took" : 3,
  "term_vectors" : {
    "text" : {
      "field_statistics" : {
        "sum_doc_freq" : 5,
        "doc_count" : 1,
        "sum_ttf" : 5
      },
      "terms" : {
        "brown" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 1,
              "payload" : "QEAAAA=="
            }
          ]
        },
        "fox" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 2,
              "payload" : "QIAAAA=="
            }
          ]
        },
        "is" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 3,
              "payload" : "AAAAAA=="
            }
          ]
        },
        "quick" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 4,
              "payload" : "QSAAAA=="
            }
          ]
        },
        "the" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "payload" : "AAAAAA=="
            }
          ]
        }
      }
    }
  }
}

4.10、Dictionary decompounder token filter(词典拆分词元过滤器)

在大多数情况下,我们建议使用速度更快的hyphenation_decompounder token filter来代替此过滤器。但是,您可以使用该过滤器检查单词列表的质量,然后再在hyphenation_decompounder token filter中实现它。

使用指定的单词列表和暴力方法在复合词中查找子词。如果找到,这些子词将包括在令牌输出中。

该过滤器使用Lucene的DictionaryCompoundWordTokenFilter,它是为日耳曼语言构建的。

示例:

GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "dictionary_decompounder",
      "word_list": ["Donau", "dampf", "meer", "schiff"]
    }
  ],
  "text": "Donaudampfschiff"
}

返回的词项:

[ Donaudampfschiff, Donau, dampf, schiff ]

4.11、Edge n-gram token filter(边缘N元语法词元过滤器)

从标记的开头开始形成指定长度的n元语法。

配置参数:

  • max_gram:单个词源的最大字符长度,对于自定义过滤器是默认为2,内置过滤器为1.
  • min_gram:单个词源的最小字符长度,默认为1
  • side:已弃用,front/back,默认front

示例:

GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    { "type": "edge_ngram",
      "min_gram": 1,
      "max_gram": 2
    }
  ],
  "text": "the quick brown fox jumps"
}

返回的词项:

[ t, th, q, qu, b, br, f, fo, j, ju ]

加入到分析器:

PUT edge_ngram_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_edge_ngram": {
          "tokenizer": "standard",
          "filter": [ "edge_ngram" ]
        }
      }
    }
  }
}

4.12、Elision token filter(元音词元过滤器)

此过滤器可以过滤省略元音。如,“l’avion” (the plane) 将会被过滤成 “avion” (plane)。

如果未自定义,默认情况下,筛选器会删除以下法语省略:l', m', t', qu', n', s', j', d', c', jusqu', quoiqu', lorsqu', puisqu'

该过滤器被使用在:

  • Catalan analyzer
  • French analyzer
  • Irish analyzer
  • Italian analyzer

示例:

GET _analyze
{
  "tokenizer" : "standard",
  "filter" : ["elision"],
  "text" : "j’examine près du wharf"
}

返回的词项:

[ examine, près, du, wharf ]

加入到分析器:

PUT /elision_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "whitespace_elision" : {
                    "tokenizer" : "whitespace",
                    "filter" : ["elision"]
                }
            }
        }
    }
}

4.13、Fingerprint token filter(指纹词元过滤器)

fingerprint过滤器发出单个token,该token对于指纹身份的文本和/或提供可以被聚类的token是有用的。 它通过排序token,重复数据删除,然后将它们连接回单个token来实现。

如文本:[“the”, “quick”, “quick”, “brown”, “fox”, “was”, “very”, “brown”],将会被转化为单个token: “brown fox quick the very was”,注意token是按字母顺序排列的,并且只有一个"quick" 。

配置参数:

  • max_output_size:输出token的最大字符长度,默认为255 。
  • separator:用于连接token流输入的字符,默认为空格。

示例:

GET _analyze
{
  "tokenizer" : "whitespace",
  "filter" : ["fingerprint"],
  "text" : "zebra jumps over resting resting dog"
}

返回的词项:

[ dog jumps over resting zebra ]

加入到分析器:

PUT fingerprint_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_fingerprint": {
          "tokenizer": "whitespace",
          "filter": [ "fingerprint" ]
        }
      }
    }
  }
}

4.14、Flatten graph token filter(平铺图词元过滤器)

flatten_graph 词元过滤器接受任意图形 **token **流,例如由 Synonym Graph Token Filter 产生的图形 token 流,并将其平坦化为适用于索引的单个线性标记链。

这是一个有损的过程,因为单独的边路径彼此挤压,但是如果您在索引期间必需使用图形 token 流,因为Lucene索引当前无法表示图形。 因此,最好仅在搜索时间应用图形分析器,因为它保留了完整的图形结构,并为近似度查询提供了正确的匹配。

4.15、Hunspell token filter(Hunspell词元过滤器)

Hunspell过滤器是Hunspell的基础。Hunspell字典将从文件系统上的专用hunspell目录( &lt;path.conf&gt;/hunspell )中&lt;path.conf&gt;/hunspell 。 预期每个字典都有自己的目录,以其关联的语言环境(语言)命名。 这个字典目录预计会保存一个*.aff和一个或多个*.dic文件(所有这些文件将自动被读取)。

4.16、Hyphenation decompounder token filter(连字分解词元过滤器)

使用基于XML的连字模式在复合词中查找潜在的子词。然后对照指定的单词列表检查这些子词。不在列表中的子字将从令牌输出中排除。

此筛选器使用Lucene的HyhenationCompoundWordTokenFilter,它是为日耳曼语言构建的。

示例:

GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "hyphenation_decompounder",
      "hyphenation_patterns_path": "analysis/hyphenation_patterns.xml",
      "word_list": ["Kaffee", "zucker", "tasse"]
    }
  ],
  "text": "Kaffeetasse"
}

返回的词项:

[ Kaffeetasse, Kaffee, tasse ]

4.17、Keep types token filter(保留指定类型词元过滤器)

当type为keep_types时,过滤器将只保留包含在预定义集合中的token。

配置参数:

  • types:(必选,字符串数组)要保留或删除的token类型列表。
  • mode:(可选,字符串)指示是保留还是删除指定的token类型。有效值包括:
    • include:(默认)仅保留指定的token类型。
    • exclude:删除指定的token类型。

示例:

GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "keep_types",
      "types": [ "<NUM>" ],
      "mode": "exclude"
    }
  ],
  "text": "1 quick fox 2 lazy dogs"
}

返回的词项:

[ quick, fox, lazy, dogs ]

自定义并加入一个分析器:

PUT keep_types_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [ "extract_alpha" ]
        }
      },
      "filter": {
        "extract_alpha": {
          "type": "keep_types",
          "types": [ "<ALPHANUM>" ]
        }
      }
    }
  }
}

4.18、Keep words token filter(保留字词元过滤器)

当词元过滤器中的type为keep时,表示只保留具有预定义单词集中的文本的token。可以在设置中定义一组单词,或者从包含每行一个单词的文本文件加载。

配置参数:

  • keep_words:要保留的单词列表
  • keep_words_path:一个包含单词列表的文件的路径
  • keep_words_case:一个布尔值,表示是否小写单词(默认为false )

示例:

GET _analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "keep",
      "keep_words": [ "dog", "elephant", "fox" ]
    }
  ],
  "text": "the quick fox jumps over the lazy dog"
}

返回的词项:

[ fox, dog ]

自定义并加入分析器:

PUT keep_words_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_keep_word_array": {
          "tokenizer": "standard",
          "filter": [ "keep_word_array" ]
        },
        "standard_keep_word_file": {
          "tokenizer": "standard",
          "filter": [ "keep_word_file" ]
        }
      },
      "filter": {
        "keep_word_array": {
          "type": "keep",
          "keep_words": [ "one", "two", "three" ]
        },
        "keep_word_file": {
          "type": "keep",
          "keep_words_path": "analysis/example_word_list.txt"
        }
      }
    }
  }
}

4.19、Keyword marker token filter(关键词标记词元过滤器)

保护词语不被词干分析器修改。 必须放置在任何词干过滤器之前。

配置参数:

  • ignore_case:设置为 true 会把词转化为小写. 默认 false.
  • keywords:关键词列表
  • keywords_path:关键词列表文件的路径 (**config **目录的相对路径或者绝对路径).
  • keywods_pattern:必选,用于匹配关键字的正则表达式

示例:

//不加入keyword过滤器
GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [ "stemmer" ],
  "text": "fox running and jumping"
}

//返回
[ fox, run, and, jump ]

//加入keyword过滤器
GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "keyword_marker",
      "keywords": [ "jumping" ]
    },
    "stemmer"
  ],
  "text": "fox running and jumping"
}

//返回
[ fox, run, and, jumping ]

自定义并加入分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "my_custom_keyword_marker_filter",
            "porter_stem"
          ]
        }
      },
      "filter": {
        "my_custom_keyword_marker_filter": {
          "type": "keyword_marker",
          "keywords_path": "analysis/example_word_list.txt"
        }
      }
    }
  }
}

4.20、Keyword repeat token filter(关键词重复词元过滤器)

keyword_repeat 词元过滤器将每个传入两次的词元发送一次作为关键字,一次作为非关键字,以允许将术语的未设置版本与该术语的主题版本并排编入索引。

鉴于此过滤器的性质,未被后续监听器转换的每个词元将被索引两次。 因此,考虑添加 unique过滤器,only_on_same_position 设置为 true 可以丢弃不必要的重复项。

示例:

GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    "keyword_repeat"
  ],
  "text": "fox running and jumping",
  "explain": true,
  "attributes": "keyword"
}

返回:

{
  "detail" : {
    "custom_analyzer" : true,
    "charfilters" : [ ],
    "tokenizer" : {
      "name" : "whitespace",
      "tokens" : [
        {
          "token" : "fox",
          "start_offset" : 0,
          "end_offset" : 3,
          "type" : "word",
          "position" : 0
        },
        {
          "token" : "running",
          "start_offset" : 4,
          "end_offset" : 11,
          "type" : "word",
          "position" : 1
        },
        {
          "token" : "and",
          "start_offset" : 12,
          "end_offset" : 15,
          "type" : "word",
          "position" : 2
        },
        {
          "token" : "jumping",
          "start_offset" : 16,
          "end_offset" : 23,
          "type" : "word",
          "position" : 3
        }
      ]
    },
    "tokenfilters" : [
      {
        "name" : "keyword_repeat",
        "tokens" : [
          {
            "token" : "fox",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "word",
            "position" : 0,
            "keyword" : true
          },
          {
            "token" : "fox",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "word",
            "position" : 0,
            "keyword" : false
          },
          {
            "token" : "running",
            "start_offset" : 4,
            "end_offset" : 11,
            "type" : "word",
            "position" : 1,
            "keyword" : true
          },
          {
            "token" : "running",
            "start_offset" : 4,
            "end_offset" : 11,
            "type" : "word",
            "position" : 1,
            "keyword" : false
          },
          {
            "token" : "and",
            "start_offset" : 12,
            "end_offset" : 15,
            "type" : "word",
            "position" : 2,
            "keyword" : true
          },
          {
            "token" : "and",
            "start_offset" : 12,
            "end_offset" : 15,
            "type" : "word",
            "position" : 2,
            "keyword" : false
          },
          {
            "token" : "jumping",
            "start_offset" : 16,
            "end_offset" : 23,
            "type" : "word",
            "position" : 3,
            "keyword" : true
          },
          {
            "token" : "jumping",
            "start_offset" : 16,
            "end_offset" : 23,
            "type" : "word",
            "position" : 3,
            "keyword" : false
          }
        ]
      }
    ]
  }
}

加入到分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "keyword_repeat",
            "porter_stem",
            "remove_duplicates"
          ]
        }
      }
    }
  }
}

4.21、KStem token filter(KStem词元过滤器)

kstem 词元过滤器是用于英语的高性能过滤器。 所有词必须已经小写(使用 **lowercase **过滤器)才能使此过滤器正常工作。

示例:

GET /_analyze
{
  "tokenizer": "standard",
  "filter": [ "kstem" ],
  "text": "the foxes jumping quickly"
}

返回:

[ the, fox, jump, quick ]

加入到分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "kstem"
          ]
        }
      }
    }
  }
}

4.22、Length token filter(长度词元过滤器)

length 类型的词元过滤器,可以删除流中过长或过短的字词。

配置参数:

  • min:(可选,整数)令牌的最小字符长度。较短的令牌将从输出中排除。默认为0。
  • max:(可选,整数)令牌的最大字符长度。较长的令牌将从输出中排除。默认为整数.MAX_VALUE,即2^31-1或2147483647。

示例:

GET _analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "length",
      "min": 0,
      "max": 4
    }
  ],
  "text": "the quick brown fox jumps over the lazy dog"
}

返回的词项:

[ the, fox, over, the, lazy, dog ]

加入到分析器:

PUT length_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_length": {
          "tokenizer": "standard",
          "filter": [ "length" ]
        }
      }
    }
  }
}

自定义:

PUT length_custom_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_length_2_to_10_char": {
          "tokenizer": "whitespace",
          "filter": [ "length_2_to_10_char" ]
        }
      },
      "filter": {
        "length_2_to_10_char": {
          "type": "length",
          "min": 2,
          "max": 10
        }
      }
    }
  }
}

4.23、Limit token count token filter(限制词元数量过滤器)

限制每个文档和字段索引的token数。

配置参数:

  • max_token_count:每个文档和字段应该索引的token的最大数量。 默认值为1
  • consume_all_tokens:如果设置为true,尽管已经超过max_token_count设定的值,也会最大限度的处理所有的token。默认为false。

示例:

GET _analyze
{
  "tokenizer": "standard",
    "filter": [
    {
      "type": "limit",
      "max_token_count": 2
    }
  ],
  "text": "quick fox jumps over lazy dog"
}

返回的词项:

[ quick, fox ]

自定义并加入分析器:

PUT custom_limit_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_five_token_limit": {
          "tokenizer": "whitespace",
          "filter": [ "five_token_limit" ]
        }
      },
      "filter": {
        "five_token_limit": {
          "type": "limit",
          "max_token_count": 5
        }
      }
    }
  }
}

4.24、Lowercase token filter(小写词元过滤器)

lowercase类型的词元过滤器,将词元文本规范化为小写。

Lowercase Token Filter 通过 language 参数支持 Greek(希腊语),Irish (爱尔兰语)和 Turkish(土耳其)小写词元过滤器。

配置参数:

  • language:可选
    • greek
    • irish
    • turkish

示例:

GET _analyze
{
  "tokenizer" : "standard",
  "filter" : ["lowercase"],
  "text" : "THE Quick FoX JUMPs"
}

返回的词项:

[ the, quick, fox, jumps ]

自定义并添加到分析器:

PUT custom_lowercase_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "greek_lowercase_example": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["greek_lowercase"]
        }
      },
      "filter": {
        "greek_lowercase": {
          "type": "lowercase",
          "language": "greek"
        }
      }
    }
  }
}

4.25、MinHash token filter(MinHash词元过滤器)

min_hash 过滤器将token流中每个token一一哈希,并将生成的哈希值分成buckets,以保持每bucket最低值的散列值。 然后将这些哈希值作为token(词元)返回。

配置参数:

  • hash_count:散列token流的散列数。 默认为1 。
  • bucket_count:将minhash分成的bucket数。 默认为512 。
  • hash_set_size:每bucket要保留的最小数量。 默认为1 。
  • with_rotation:是否将空bucket中的第一个非空bucket的值填充到其循环右边。 仅当hash_set_size等于1时才生效。 如果bucket_count大于1,则默认为true ,否则为false 。

示例:

PUT /my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "my_shingle_filter": {      
          "type": "shingle",
          "min_shingle_size": 5,
          "max_shingle_size": 5,
          "output_unigrams": false
        },
        "my_minhash_filter": {
          "type": "min_hash",
          "hash_count": 1,          
          "bucket_count": 512,      
          "hash_set_size": 1,       
          "with_rotation": true     
        }
      },
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "my_shingle_filter",
            "my_minhash_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "fingerprint": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}

4.26、Multiplexer token filter(多路复用器词元过滤器)

多路复用器类型的token过滤器将在相同位置发射多个token,token的每个版本都已通过不同的过滤器。相同位置的相同输出token将被删除。

如果传入token流具有重复token,则多路复用器也会移除这些token。

示例:

PUT /multiplexer_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : [ "my_multiplexer" ]
                }
            },
            "filter" : {
                "my_multiplexer" : {
                    "type" : "multiplexer",
                    "filters" : [ "lowercase", "lowercase, porter_stem" ]
                }
            }
        }
    }
}

POST /multiplexer_example/_analyze
{
  "analyzer" : "my_analyzer",
  "text" : "Going HOME"
}

返回:

{
  "tokens" : [
    {
      "token" : "Going",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "going",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "go",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "HOME",
      "start_offset" : 6,
      "end_offset" : 10,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "home",
      "start_offset" : 6,
      "end_offset" : 10,
      "type" : "<ALPHANUM>",
      "position" : 1
    }
  ]
}

4.27、N-gram token filter(N元语法词元过滤器)

从token中形成指定长度的n元语法。

配置参数:

  • max_gram:默认2
  • min_gram:默认1

示例:

GET _analyze
{
  "tokenizer": "standard",
  "filter": [ "ngram" ],
  "text": "Quick fox"
}

返回的词项:

[ Q, Qu, u, ui, i, ic, c, ck, k, f, fo, o, ox, x ]

加入到分析器:

PUT ngram_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_ngram": {
          "tokenizer": "standard",
          "filter": [ "ngram" ]
        }
      }
    }
  }
}

自定义:

PUT ngram_custom_example
{
  "settings": {
    "index": {
      "max_ngram_diff": 2
    },
    "analysis": {
      "analyzer": {
        "default": {
          "tokenizer": "whitespace",
          "filter": [ "3_5_grams" ]
        }
      },
      "filter": {
        "3_5_grams": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 5
        }
      }
    }
  }
}

4.28、Normalization token filters(标准化词元过滤器)

有几个词元过滤器可用于尝试规范某种语言的特殊字符。

  • Arabic:阿拉伯语,arabic_normalization
  • German:德语,german_normalization
  • Hindi:印地语,hindi_normalization
  • Indic:印度语,indic_normalization
  • Kurdish (Sorani):库尔德语,sorani_normalization
  • Persian:波斯语,persian_normalization
  • Scandinavian:斯堪纳维亚,scandinavian_normalization, scandinavian_folding
  • Serbian:塞尔维亚,serbian_normalization

4.29、Pattern capture token filter(模式捕获词元过滤器)

模式捕获令牌过滤器。
pattern_capture token filter与pattern tokenizer不同,它为正则表达式中的每个捕获组发出一个令牌。模式不固定在字符串的开头和结尾,因此每个模式可以多次匹配,并且允许匹配重叠。

举例如下:

正则:

"(([a-z]+)(\d*))"

待匹配文字:

"abc123def456"

匹配结果:

[ abc123, abc, 123, def456, def, 456 ]

示例:

PUT test
{
   "settings" : {
      "analysis" : {
         "filter" : {
            "code" : {
               "type" : "pattern_capture",
               "preserve_original" : true,
               "patterns" : [
                  "(\\p{Ll}+|\\p{Lu}\\p{Ll}+|\\p{Lu}+)",
                  "(\\d+)"
               ]
            }
         },
         "analyzer" : {
            "code" : {
               "tokenizer" : "pattern",
               "filter" : [ "code", "lowercase" ]
            }
         }
      }
   }
}

GET test/_analyze
{
  "analyzer": "code", 
  "text": "import static org.apache.commons.lang.StringEscapeUtils.escapeHtml"
}

返回的词项:

[ import, static, org, apache, commons, lang, stringescapeutils, string, escape, utils, escapehtml, escape, html ]

4.30、Pattern replace token filter(模式替换词元过滤器)

pattern_replace过滤器可以容易地处理基于正则表达式的字符串替换。 使用pattern参数定义正则表达式,并且可以使用replacement参数(支持引用原始文本,如下所述)提供要替换字符串。

配置参数:

  • all:(可选,布尔值)如果为True,则替换与模式参数的正则表达式匹配的所有子字符串。如果为False,则筛选器仅替换每个令牌中的第一个匹配子字符串。默认为True。
  • pattern:(必选,字符串)正则表达式,用Java的正则表达式语法编写。筛选器用替换参数中的子字符串替换与此模式匹配的令牌子字符串。
  • replacement:(可选,字符串)替换子字符串。默认为空子字符串(“”)。

示例:

GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "pattern_replace",
      "pattern": "(dog)",
      "replacement": "watch$1"
    }
  ],
  "text": "foxes jump lazy dogs"
}

返回的词项:

[ foxes, jump, lazy, watchdogs ]

自定义并加入分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "filter": [
            "my_pattern_replace_filter"
          ]
        }
      },
      "filter": {
        "my_pattern_replace_filter": {
          "type": "pattern_replace",
          "pattern": "[£|€]",
          "replacement": "",
          "all": false
        }
      }
    }
  }
}

4.31、Phonetic token filter(语音词元过滤器)

Phonetic token filter作为分析-语音插件提供。

4.32、Porter stem token filter(Porter词干词元过滤器)

porter_stem 类型的词元过滤器,根据波特干扰算法转换词元流。

请注意,Porter Stem 词元过滤器的输入必须已经是小写,所以您需要使用** Lower Case Token Filter** or Lower Case Tokenizer 在之前的分词器链上,以使其正常工作!例如,使用自定义分词器时,请确保过滤器列表中的 **lowercase **过滤器位于 porter_stem 过滤器之前

示例:

GET /_analyze
{
  "tokenizer": "standard",
  "filter": [ "porter_stem" ],
  "text": "the foxes jumping quickly"
}

返回的词项:

[ the, fox, jump, quickli ]

添加到分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "porter_stem"
          ]
        }
      }
    }
  }
}

4.33、Predicate script token filter(谓词脚本词元过滤器)

采用谓词脚本,并删除与该谓词不匹配的标记。

示例:

PUT /condition_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : [ "my_script_filter" ]
                }
            },
            "filter" : {
                "my_script_filter" : {
                    "type" : "predicate_token_filter",
                    "script" : {
                        "source" : "token.getTerm().length() > 5"  
                    }
                }
            }
        }
    }
}

POST /condition_example/_analyze
{
  "analyzer" : "my_analyzer",
  "text" : "What Flapdoodle"
}

返回:

{
  "tokens" : [
    {
      "token" : "Flapdoodle",
      "start_offset" : 5,
      "end_offset" : 15,
      "type" : "<ALPHANUM>",
      "position" : 1
    }
  ]
}

4.34、Remove duplicates token filter(删除重复词元过滤器)

删除相同位置中的重复token。

示例:

//不添加Remove duplicates token filter
GET _analyze
{
  "tokenizer": "whitespace",
  "filter": [
    "keyword_repeat",
    "stemmer"
  ],
  "text": "jumping dog"
}

//返回
{
  "tokens" : [
    {
      "token" : "jumping",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "jump",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "dog",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "dog",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "word",
      "position" : 1
    }
  ]
}

//添加Remove duplicates token filter
GET _analyze
{
  "tokenizer": "whitespace",
  "filter": [
    "keyword_repeat",
    "stemmer",
    "remove_duplicates"
  ],
  "text": "jumping dog"
}

//返回
{
  "tokens" : [
    {
      "token" : "jumping",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "jump",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "dog",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "word",
      "position" : 1
    }
  ]
}

添加到分析器:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "keyword_repeat",
            "stemmer",
            "remove_duplicates"
          ]
        }
      }
    }
  }
}

4.35、Reverse token filter(反向词元过滤器)

反转流中的每个token。例如,您可以使用反向筛选器将cat更改为tac。

反向标记对于基于后缀的搜索非常有用,例如查找以-结尾的单词或按其扩展名搜索文件名。

示例:

GET _analyze
{
  "tokenizer" : "standard",
  "filter" : ["reverse"],
  "text" : "quick fox jumps"
}

返回的词项:

[ kciuq, xof, spmuj ]

添加到分析器:

PUT reverse_example
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "whitespace_reverse" : {
          "tokenizer" : "whitespace",
          "filter" : ["reverse"]
        }
      }
    }
  }
}

4.36、Shingle token filter(单词元过滤器)

shingle 类型的词元过滤器,用于从词元流中构建 shingles (token n-grams)。 换句话说,它创建词元的组合作为单个词元。 例如,句子 “the lazy dog” ,可能会被划分为[ the, the lazy, lazy, lazy dog, dog ]

参数配置:

  • max_shingle_size:最大的 shingle 数量. 默认2
  • min_shingle_size:最小的 shingle 数量. 默认2
  • output_unigrams:如果为true输出将会包含输入的 tokens (unigrams) 以及 shingles. 默认 true.
  • output_unigrams_if_no_shingles:如果output_unigrams为 false ,如果没有可用的 shingles 输出将会包含输入的 tokens (unigrams). 提示 如果output_unigrams为 true 当前设置不生效. 默认 false.
  • token_separator:连接相邻的词元生成词组的字符串. 默认 " ".
  • filler_token:用于替换流中每个没有实际词元的位置的字符串。例如,当 stop 过滤器与 shingle 过滤器一起使用时,如果位置增量大于1. 默认 "_"

示例:

GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [ "shingle" ],
  "text": "quick brown fox jumps"
}
//返回的词项
[ quick, quick brown, brown, brown fox, fox, fox jumps, jumps ]

GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "shingle",
      "min_shingle_size": 2,
      "max_shingle_size": 3
    }
  ],
  "text": "quick brown fox jumps"
}
//返回的词项
[ quick, quick brown, quick brown fox, brown, brown fox, brown fox jumps, fox, fox jumps, jumps ]


GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "shingle",
      "min_shingle_size": 2,
      "max_shingle_size": 3,
      "output_unigrams": false
    }
  ],
  "text": "quick brown fox jumps"
}
//返回的词项
[ quick brown, quick brown fox, brown fox, brown fox jumps, fox jumps ]

添加到分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_shingle": {
          "tokenizer": "standard",
          "filter": [ "shingle" ]
        }
      }
    }
  }
}

自定义:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "en": {
          "tokenizer": "standard",
          "filter": [ "my_shingle_filter" ]
        }
      },
      "filter": {
        "my_shingle_filter": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 5,
          "output_unigrams": false
        }
      }
    }
  }
}

4.37、Snowball token filter(雪球词元过滤器)

使用Snowball生成的词干分析器对单词进行词干处理的过滤器。Language参数使用以下可用值控制词干分析器:亚美尼亚语、巴斯克语、加泰罗尼亚语、丹麦语、荷兰语、英语、芬兰语、法语、德语、德语2、匈牙利语、意大利语、KP、立陶宛语、Lovins、挪威语、波特、葡萄牙语、罗马尼亚语、俄语、西班牙语、瑞典语、土耳其语。

示例:

PUT /my_index
{
    "settings": {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["lowercase", "my_snow"]
                }
            },
            "filter" : {
                "my_snow" : {
                    "type" : "snowball",
                    "language" : "Lovins"
                }
            }
        }
    }
}

4.38、Stemmer token filter(Stemmer词元过滤器)

一个过滤器,通过单个统一接口提供(几乎)所有可用的词干词元过滤器的访问。

配置参数:

  • language:支持的语言

示例:

GET /_analyze
{
  "tokenizer": "standard",
  "filter": [ "stemmer" ],
  "text": "the foxes jumping quickly"
}

返回的词项:

[ the, fox, jump, quickli ]

加入分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "whitespace",
          "filter": [ "stemmer" ]
        }
      }
    }
  }
}

4.39、Stemmer override token filter(Stemmer Override词元过滤器)

通过应用自定义映射来覆盖词干算法,然后保护这些术语不被词干修改。 必须放置在任何词干过滤器之前。

配置参数:

  • rules:映射规则列表.
  • rules_path:映射列表文件的路径 (config目录的相对路径或者绝对路径) to a list of mappings.

示例:

PUT /my_index
{
    "settings": {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["lowercase", "custom_stems", "porter_stem"]
                }
            },
            "filter" : {
                "custom_stems" : {
                    "type" : "stemmer_override",
                    "rules_path" : "analysis/stemmer_override.txt"
                }
            }
        }
    }
}

rule文件:

running => run

stemmer => stemmer

或者直接定义:

PUT /my_index
{
    "settings": {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["lowercase", "custom_stems", "porter_stem"]
                }
            },
            "filter" : {
                "custom_stems" : {
                    "type" : "stemmer_override",
                    "rules" : [
                        "running => run",
                        "stemmer => stemmer"
                    ]
                }
            }
        }
    }
}

4.40、Stop token filter(停止词元过滤器)

stop 类型的词元过滤器,用于从词元流中移除stop words。

如果未自定义,默认情况下,筛选器会删除以下英语停用词:

a, an, and, are, as, at, be, but, by, for, if, in, into, is, it, no, not, of, on, or, such, that, the, their, then, there, these, they, this, to, was, will, with

参数配置:

  • stopwords:停止词列表. 默认 _english_ 无效词. |
  • stopwords_path:无效词配置文件路径(绝对或者相对路径). 每个停止词自占一行 (换行分隔). 文件必须是 UTF-8 编码.
  • ignore_case:设置为true所有词被转为小写. 默认 false.
  • remove_trailing:设置为 false,以便不忽略搜索的最后一个字词,如果它是无效词。 这对于完成建议者非常有用,因为像 green a 一样的查询可以扩展到 green apple ,即使你删除一般的无效词。 默认为 true。

Elasticsearch 提供以下预定义语言列表:

_arabic_, _armenian_, _basque_, _brazilian_, _bulgarian_, _catalan_, _czech_, _danish_, _dutch_, _english_, _finnish_, _french_, _galician_, _german_, _greek_, _hindi_, _hungarian_,_indonesian_, _irish_, _italian_, _latvian_, _norwegian_, _persian_, _portuguese_, _romanian_, _russian_, _sorani_, _spanish_, _swedish_, _thai_, _turkish_.

空的无效词列表(禁用无效词)使用:_none_

示例:

GET /_analyze
{
  "tokenizer": "standard",
  "filter": [ "stop" ],
  "text": "a quick fox jumps over the lazy dog"
}

返回的词项:

[ quick, fox, jumps, over, lazy, dog ]

加入到分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "whitespace",
          "filter": [ "stop" ]
        }
      }
    }
  }
}

自定义:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "default": {
          "tokenizer": "whitespace",
          "filter": [ "my_custom_stop_words_filter" ]
        }
      },
      "filter": {
        "my_custom_stop_words_filter": {
          "type": "stop",
          "ignore_case": true
        }
      }
    }
  }
}

4.41、Synonym token filter(同义词词元过滤器)

同义词标记筛选器允许在分析过程中轻松处理同义词。同义词是使用配置文件配置的。

支持两种同义词格式:Solr,WordNet。

Solr synonyms配置文件格式:

# Blank lines and lines starting with pound are comments.

# Explicit mappings match any token sequence on the LHS of "=>"
# and replace with all alternatives on the RHS.  These types of mappings
# ignore the expand parameter in the schema.
# Examples:
i-pod, i pod => ipod,
sea biscuit, sea biscit => seabiscuit

# Equivalent synonyms may be separated with commas and give
# no explicit mapping.  In this case the mapping behavior will
# be taken from the expand parameter in the schema.  This allows
# the same synonym file to be used in different synonym handling strategies.
# Examples:
ipod, i-pod, i pod
foozball , foosball
universe , cosmos
lol, laughing out loud

# If expand==true, "ipod, i-pod, i pod" is equivalent
# to the explicit mapping:
ipod, i-pod, i pod => ipod, i-pod, i pod
# If expand==false, "ipod, i-pod, i pod" is equivalent
# to the explicit mapping:
ipod, i-pod, i pod => ipod

# Multiple synonym mapping entries are merged.
foo => foo bar
foo => baz
# is equivalent to
foo => foo bar, baz

示例:

PUT /test_index
{
    "settings": {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "synonym" : {
                        "tokenizer" : "whitespace",
                        "filter" : ["synonym"]
                    }
                },
                "filter" : {
                    "synonym" : {
                        "type" : "synonym",
                        "synonyms_path" : "analysis/synonym.txt"
                    }
                }
            }
        }
    }
}

//直接给过滤器定义同义词
PUT /test_index
{
    "settings": {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "synonym" : {
                        "tokenizer" : "standard",
                        "filter" : ["my_stop", "synonym"]
                    }
                },
                "filter" : {
                    "my_stop": {
                        "type" : "stop",
                        "stopwords": ["bar"]
                    },
                    "synonym" : {
                        "type" : "synonym",
                        "lenient": true,
                        "synonyms" : ["foo, bar => baz"]
                    }
                }
            }
        }
    }
}

WordNet synonyms:

PUT /test_index
{
    "settings": {
        "index" : {
            "analysis" : {
                "filter" : {
                    "synonym" : {
                        "type" : "synonym",
                        "format" : "wordnet",
                        "synonyms" : [
                            "s(100000001,1,'abstain',v,1,0).",
                            "s(100000001,2,'refrain',v,1,0).",
                            "s(100000001,3,'desist',v,1,0)."
                        ]
                    }
                }
            }
        }
    }
}

4.42、Synonym graph token filter(同义词图词元过滤器)

synonym_graph 词元过滤器允许在分析过程中轻松处理同义词,包括多字同义词。

为了正确处理多字同义词,该词元过滤器在处理过程中创建 “graph token stream”。

4.43、Trim token filter(Trim词元过滤器)

trim过滤器将会trim掉token周围的空格。

示例:

GET _analyze
{
  "tokenizer" : "keyword",
  "text" : " fox "
}

返回:

{
  "tokens" : [
    {
      "token" : " fox ",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "word",
      "position" : 0
    }
  ]
}
GET _analyze
{
  "tokenizer" : "keyword",
  "filter" : ["trim"],
  "text" : " fox "
}

返回:

{
  "tokens": [
    {
      "token": "fox",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 0
    }
  ]
}

添加到分析器:

PUT trim_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "keyword_trim": {
          "tokenizer": "keyword",
          "filter": [ "trim" ]
        }
      }
    }
  }
}

4.44、Truncate token filter(截断词元过滤器)

截断超过指定字符限制的token。该限制默认为10,但可以使用长度参数进行自定义。

配置参数:

  • lenght:(可选,整数)每个token的字符限制。超过此限制的token将被截断。默认为10。

示例:

GET _analyze
{
  "tokenizer" : "whitespace",
  "filter" : ["truncate"],
  "text" : "the quinquennial extravaganza carried on"
}

返回的词项:

[ the, quinquenni, extravagan, carried, on ]

添加到分析器:

PUT custom_truncate_example
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "standard_truncate" : {
        "tokenizer" : "standard",
        "filter" : ["truncate"]
        }
      }
    }
  }
}

自定义:

PUT 5_char_words_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "lowercase_5_char": {
          "tokenizer": "lowercase",
          "filter": [ "5_char_trunc" ]
        }
      },
      "filter": {
        "5_char_trunc": {
          "type": "truncate",
          "length": 5
        }
      }
    }
  }
}

4.45、Unique token filter(唯一词元过滤器)

唯一词元过滤器用于在分析期间仅索引唯一的token。 默认情况下,它应用于所有词元流。

配置参数:

  • only_on_same_position:设置为true ,则只会删除相同位置上的重复token。默认为false

示例:

GET _analyze
{
  "tokenizer" : "whitespace",
  "filter" : ["unique"],
  "text" : "the quick fox jumps the lazy fox"
}

//返回的词项
[ the, quick, fox, jumps, lazy ]

添加到分析器:

PUT custom_unique_example
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "standard_truncate" : {
        "tokenizer" : "standard",
        "filter" : ["unique"]
        }
      }
    }
  }
}

自定义:

PUT letter_unique_pos_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "letter_unique_pos": {
          "tokenizer": "letter",
          "filter": [ "unique_pos" ]
        }
      },
      "filter": {
        "unique_pos": {
          "type": "unique",
          "only_on_same_position": true
        }
      }
    }
  }
}

4.46、Uppercase token filter(大写词元过滤器)

将词元文本规范化为大写。

示例:

GET _analyze
{
  "tokenizer" : "standard",
  "filter" : ["uppercase"],
  "text" : "the Quick FoX JUMPs"
}

//返回的词项
[ THE, QUICK, FOX, JUMPS ]

添加到分析器:

PUT uppercase_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "whitespace_uppercase" : {
                    "tokenizer" : "whitespace",
                    "filter" : ["uppercase"]
                }
            }
        }
    }
}

4.47、Word delimiter token filter(单词分隔词元过滤器)

将单词分解为子词,并对子词组进行可选的转换。 词被分为以下规则的子词:

  • 拆分字内分隔符(默认情况下,所有非字母数字字符)。.
  • “Wi-Fi” → “Wi”, “Fi”
  • 按大小写转换拆分: “PowerShot” → “Power”, “Shot”
  • 按字母数字转换拆分: “SD500” → “SD”, “500”
  • 每个子词的前导和尾随的词内分隔符都被忽略: “//hello—there, dude” → “hello”, “there”, “dude”
  • 每个子词都删除尾随的“s”: “O’Neil’s” → “O”, “Neil”

示例:

GET /_analyze
{
  "tokenizer": "keyword",
  "filter": [ "word_delimiter" ],
  "text": "Neil's-Super-Duper-XL500--42+AutoCoder"
}

//返回的词项
[ Neil, Super, Duper, XL, 500, 42, Auto, Coder ]

添加到分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "filter": [ "word_delimiter" ]
        }
      }
    }
  }
}

4.48、Word delimiter graph token filter(单词分隔图词元过滤器)

示例:

GET /_analyze
{
  "tokenizer": "keyword",
  "filter": [ "word_delimiter_graph" ],
  "text": "Neil's-Super-Duper-XL500--42+AutoCoder"
}

//返回的词项
[ Neil, Super, Duper, XL, 500, 42, Auto, Coder ]

添加到分析器:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "filter": [ "word_delimiter_graph" ]
        }
      }
    }
  }
}

5、内置的Analyzer

5.1、Fingerprint Analyzer(指纹分析器)

fingerprint 分析器实现了OpenRefine项目使用的指纹识别算法来协助聚类。

输入文本较低,规范化以删除扩展字符,排序,重复数据删除并连接到单个令牌。 如果配置了一个停用词列表,停止单词也将被删除。

组成部分:

  • Tokenizer:
    • Standard Tokenizer
  • Token Filters (in order):
    • Lower Case Token Filter
    • ASCII folding
    • Stop Token Filter (disabled by default)
    • Fingerprint

配置参数:

  • separator:用于连接terms的字符。默认为空格。
  • max_output_size:要发出的最大token大小。默认为255。大于此大小的token将被丢弃。
  • stopwords:预定义的停用词列表,如_english_或包含停用词列表的数组。默认为_none_
  • stopwords_path:包含停止字的文件的路径。

示例:

POST _analyze
{
  "analyzer": "fingerprint",
  "text": "Yes yes, Gödel said this sentence is consistent and."
}

返回的词项列表:

[ and consistent godel is said sentence this yes ]

使用预定义的英语停用词列表:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_fingerprint_analyzer": {
          "type": "fingerprint",
          "stopwords": "_english_"
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_fingerprint_analyzer",
  "text": "Yes yes, Gödel said this sentence is consistent and."
}

//返回的词项
[ consistent godel said sentence yes ]

5.2、Keyword Analyzer(关键词分析器)

关键字分析器是一个“noop”分析器,它将整个输入字符串作为单个token返回。

示例:

POST _analyze
{
  "analyzer": "keyword",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ The 2 QUICK Brown-Foxes jumped over the lazy dog's bone. ]

5.3、Language Analyzers(语言分析器)

一组旨在分析特定语言文本的分析器。
支持以下类型:阿拉伯语、亚美尼亚语、巴斯克语、孟加拉语、巴西语、保加利亚语、加泰罗尼亚语、CJK、捷克语、丹麦语、荷兰语、英语、爱沙尼亚语、芬兰语、法语、加利西亚语、德语、希腊语、印地语、匈牙利语、印度尼西亚语、爱尔兰语、意大利语、拉脱维亚语、立陶宛语、挪威语、波斯语、葡萄牙语、罗马尼亚语、俄语、索拉尼语、西班牙语、瑞典语、土耳其语、泰语。

arabic, armenian, basque, bengali, brazilian, bulgarian, catalan, cjk, czech, danish, dutch, english, estonian, finnish, french, galician, german, greek, hindi, hungarian, indonesian, irish, italian, latvian, lithuanian, norwegian, persian, portuguese, romanian, russian, sorani, spanish, swedish, turkish, thai.

此处用english analyzer的组成举例:

PUT /english_example
{
  "settings": {
    "analysis": {
      "filter": {
        "english_stop": {
          "type":       "stop",
          "stopwords":  "_english_" 
        },
        "english_keywords": {
          "type":       "keyword_marker",
          "keywords":   ["example"] 
        },
        "english_stemmer": {
          "type":       "stemmer",
          "language":   "english"
        },
        "english_possessive_stemmer": {
          "type":       "stemmer",
          "language":   "possessive_english"
        }
      },
      "analyzer": {
        "rebuilt_english": {
          "tokenizer":  "standard",
          "filter": [
            "english_possessive_stemmer",
            "lowercase",
            "english_stop",
            "english_keywords",
            "english_stemmer"
          ]
        }
      }
    }
  }
}

5.4、Pattern Analyzer(正则模式分析器)

pattern analyzer 使用正则表达式将文本拆分为词语。 正则表达式应该不是token本身匹配 token separators 。 正则表达式默认为\W+(或所有非字符字符)。

组成部分:

  • Tokenizer
    • Pattern Tokenizer
  • Token Filters
    • Lower Case Token Filter
    • Stop Token Filter (disabled by default)

参数配置:

  • pattern:Java正则表达式默认为\ W +。
  • flags:Java正则表达式标志。 标志应分开管道,例如“CASE_INSENSITIVE | COMMENTS”。
  • lowercase:是否小写,默认true。
  • stopwords:预定义的 stop 词列表,如_english_或包含停止词列表的数组。 默认为_none_。
  • stopords_path:包含停止词的文件的路径。

示例:

POST _analyze
{
  "analyzer": "pattern",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

//返回的词项
[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]

示例:拆分电子邮件地址

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_email_analyzer": {
          "type":      "pattern",
          "pattern":   "\\W|_", 
          "lowercase": true
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_email_analyzer",
  "text": "John_Smith@foo-bar.com"
}

//返回的词项
[ john, smith, foo, bar, com ]

5.5、Simple Analyzer(简单分析器)

只要遇到不是字母的字符,simple(简单)分析器将文本分解成词语。所有词语都是小写的。

组成部分:

  • Tokenizer
    • Lower Case Tokenizer

示例:

POST _analyze
{
  "analyzer": "simple",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]

5.6、Standard Analyzer(标准分析器,默认分析器)

standard(标准)分析器是默认分析器,如果没有指定分析器,则使用该分析器。它提供基于语法的标记(基于Unicode文本分段算法,如Unicode标准附件29中所述),并且用于大多数语言。

组成部分:

  • Tokenizer
    • Standard Tokenizer
  • Token Filters
    • Lower Case Token Filter
    • Stop Token Filter (disabled by default)

配置参数:

  • max_token_length:最大词语长度。如果看到超过此长度的词语,则以max_token_length分割。默认为255。
  • stopwords:预定义的停止词列表,如_english_或包含停止词列表的数组。默认为_none_
  • stopwords_path:包含停止词的文件路径。

示例:

POST _analyze
{
  "analyzer": "standard",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog's, bone ]

自定义:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_english_analyzer": {
          "type": "standard",
          "max_token_length": 5,
          "stopwords": "_english_"
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_english_analyzer",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

//返回的词项
[ 2, quick, brown, foxes, jumpe, d, over, lazy, dog's, bone ]

5.7、Stop Analyzer(停止分析器)

停止分析器与简单分析器相同,但增加了对移除停止字的支持。它默认使用_english_停止词。

组成部分:

  • Tokenizer
    • Lower Case Tokenizer
  • Token filters
    • Stop Token Filter

配置参数:

  • stopwords:预定义的停止词列表,如_english_或包含停止词列表的数组。 默认为_english_
  • stopwords_path:包含停止词的文件的路径。此路径是相对于Elasticsearch config目录。

示例:

POST _analyze
{
  "analyzer": "stop",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

//返回的词项
[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]

自定义停止词:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_stop_analyzer": {
          "type": "stop",
          "stopwords": ["the", "over"]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_stop_analyzer",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

//返回的词项
[ quick, brown, foxes, jumped, lazy, dog, s, bone ]

5.8、Whitespace Analyzer(空白分析器)

whitespace analyzer 在遇到空格字符时将文本分解成术语。

组成部分:

  • Tokenizer
    • Whitespace Tokenizer

示例:

POST _analyze
{
  "analyzer": "whitespace",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

返回的词项:

[ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog's, bone. ]

6、自定义分析器

当内置分析器不能满足您的需求时,可以创建一个custom分析器。

组成部分:

  • 零个或多个字符过滤器
  • 一个 分析器
  • 零个或多个token过滤器。

参数配置:

  • tokenizer:内置或定制的分词器
  • char_filter:可选的内置或定制字符过滤器列表
  • filter:可选的内置或定制token过滤器列表
  • position_increment_gap:在索引文本值数组时,Elasticsearch会在一个值的最后一个值和下一个值的第一个项之间插入假的“间隙”,以确保短语查询与不同数组元素的两个术语不匹配。 默认为100.

自定义示例:

字符过滤器

  • HTML Strip Character Filter

分词器

  • Standard Tokenizer
  • Token 分析器

Lowercase Token Filter

  • ASCII-Folding Token Filter
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type":      "custom",
          "tokenizer": "standard",
          "char_filter": [
            "html_strip"
          ],
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": "Is this <b>déjà vu</b>?"
}

//返回的词项
[ is, this, deja, vu ]

yaml自定义分析器:

index :
    analysis :
        analyzer :
            standard :
                type : standard
                stopwords : [stop1, stop2]
            myAnalyzer1 :
                type : standard
                stopwords : [stop1, stop2, stop3]
                max_token_length : 500
            # configure a custom analyzer which is
            # exactly like the default standard analyzer
            myAnalyzer2 :
                tokenizer : standard
                filter : [standard, lowercase, stop]
        tokenizer :
            myTokenizer1 :
                type : standard
                max_token_length : 900
            myTokenizer2 :
                type : keyword
                buffer_size : 512
        filter :
            myTokenFilter1 :
                type : stop
                stopwords : [stop1, stop2, stop3, stop4]
            myTokenFilter2 :
                type : length
                min : 0
                max : 2000

7、Analyze API

对文本字符串执行分析并返回结果

//语法
GET /_analyze

POST /_analyze

GET /<index>/_analyze

POST /<index>/_analyze

路径参数:

  • <index>:(可选,字符串)用于派生分析器的索引。如果指定,分析器或<field>参数将覆盖此值。如果未指定分析器或字段,分析API将使用索引的默认分析器。如果未指定索引或索引没有默认分析器,则分析API将使用标准分析器。

``查询参数:**

  • anayzer:(可选,字符串)应应用于提供的文本的分析器的名称。这可以是内置分析器,也可以是在索引中配置的分析器。如果未指定此参数,分析API将使用在字段映射中定义的分析器。如果未指定任何字段,分析API将使用索引的默认分析器。如果未指定索引,或者索引没有默认分析器,则分析API将使用标准分析器。
  • attributes:(可选,字符串数组)用于筛选EXPLAIN参数输出的标记属性数组。
  • char_filter:(可选,字符串数组)用于在标记器之前对字符进行预处理的字符筛选器数组。
  • explain:(可选,布尔值)如果为True,则响应包括令牌属性和其他详细信息。缺省值为False
  • field:(可选,字符串)用于派生分析器的字段。若要使用此参数,必须指定索引。如果指定,分析器参数将覆盖此值。如果未指定任何字段,分析API将使用索引的默认分析器。如果未指定索引或索引没有默认分析器,则分析API将使用标准分析器。
  • filter:(可选,字符串数组)用于在生成token之后应用的token过滤器的数组。
  • normalizer:(可选,字符串)用于将文本转换为单个标记的规格化程序。
  • text:(必填、字符串或字符串数组)要分析的文本。如果提供字符串数组,则将其作为多值字段进行分析。
  • tokenizer:(可选,字符串)用于将文本转换为标记的分词器。

示例:不指定索引

GET /_analyze
{
  "analyzer" : "standard",
  "text" : "this is a test"
}

示例:指定多个文本

GET /_analyze
{
  "analyzer" : "standard",
  "text" : ["this is a test", "the second text"]
}

示例:定制分析器

GET /_analyze
{
  "tokenizer" : "keyword",
  "filter" : ["lowercase"],
  "text" : "this is a test"
}

GET /_analyze
{
  "tokenizer" : "keyword",
  "filter" : ["lowercase"],
  "char_filter" : ["html_strip"],
  "text" : "this is a <b>test</b>"
}

GET /_analyze
{
  "tokenizer" : "whitespace",
  "filter" : [
    "lowercase", 
    {
      "type": "stop", 
      "stopwords": ["a", "is", "this"]
    }
  ],
  "text" : "this is a test"
}

示例:指定索引

GET /analyze_sample/_analyze
{
  "text" : "this is a test"
}


//上面的代码将使用与Analyze_Sample索引相关联的默认索引分析器对“This is a test”文本运行分析。还可以提供分析器以使用不同的分析器:
GET /analyze_sample/_analyze
{
  "analyzer" : "whitespace",
  "text" : "this is a test"
}

示例:从字段映射派生分析器

//将根据obj1.field1的映射中配置的分析器进行分析(如果不是,则使用默认的索引分析器)。
GET /analyze_sample/_analyze
{
  "field" : "obj1.field1",
  "text" : "this is a test"
}

示例:可以为具有与ANALYSE_SAMPLE索引相关联的规格化器的关键字字段提供规格化器。

GET /analyze_sample/_analyze
{
  "normalizer" : "my_normalizer",
  "text" : "BaR"
}

示例:设置token数量限制

生成过多的令牌可能会导致节点内存不足。以下设置允许限制可以生成的令牌数:
Index.analyze.max_Token_count
可以使用分析API生成的最大令牌数。默认值为10000。如果生成的令牌数量超过此限制,则会抛出错误。没有指定索引的_Analyze终结点将始终使用10000值作为限制。此设置允许您控制特定索引的限制:

PUT /analyze_sample
{
  "settings" : {
    "index.analyze.max_token_count" : 20000
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值