ElasticSearch文本分析(三)

ElasticSearch文本分析(三)

分词过滤器

分词过滤器接收来自分词器的数据流,他可以更新分词(更新为小写)、删除分词(移除通用词)和添加分词(同义词)

撇号(')分词过滤器

去除撇号后的所有字符,包括撇号本身。

这个过滤器包含在Elasticsearch的内置土耳其语言分析器中。它使用Lucene的撇号过滤器,这是为土耳其语而建的。

示例

以下分析 API 请求演示了撇号分词过滤器的工作原理:

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer" : "standard",
  "filter" : ["apostrophe"],
  "text" : "Istanbul\u0027a veya Istanbul\u0027dan"
}
'

过滤器产生以下分词:

[ Istanbul, veya, Istanbul ]
添加到分析上

根据以下创建所有的API请求使用撇号分词过滤器来配置一个自定义的分析器:

curl -X PUT "localhost:9200/apostrophe_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_apostrophe": {
          "tokenizer": "standard",
          "filter": [ "apostrophe" ]
        }
      }
    }
  }
}
'

经典分词过滤器

对经典分词器生成的术语执行可选的后处理。这个过滤器从单词的末尾删除英语's,并从首字母缩写中删除.。它使用LuceneClassicFilter

示例
curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer" : "classic",
  "filter" : ["classic"],
  "text" : "The 2 Q.U.I.C.K. Brown-Foxes jumped over the lazy dog\u0027s bone."
}
'

过滤器产生以下分词:

[ The, 2, QUICK, Brown, Foxes, jumped, over, the, lazy, dog, bone ]
添加到分析器上
curl -X PUT "localhost:9200/classic_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "classic_analyzer": {
          "tokenizer": "classic",
          "filter": [ "classic" ]
        }
      }
    }
  }
}
'

条件分词过滤器

示例

以下分析 API 请求使用条件过滤器匹配 THE QUICK BROWN FOX 中少于 5 个字符的分词。然后将小写过滤器应用于那些匹配到的分词,将它们转换为小写。

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "condition",
      "filter": [ "lowercase" ],
      "script": {
        "source": "token.getTerm().length() < 5"
      }
    }
  ],
  "text": "THE QUICK BROWN FOX"
}
'

过滤器产生以下标记:

[ the, QUICK, BROWN, fox ]
配置参数
  • filter:(必须的,分词过滤器数组)。分词过滤器数组。如果分词与脚本中条件相匹配,则按顺序应用于该分词。这些过滤器可以包括在索引映射中定义的自定义分词过滤器。
  • script:(必须,脚本对象)。应用于分词过滤器的脚本中。如果分词与脚本中的条件匹配则应用到该分词上。
自定义和增加到分析器上
curl -X PUT "localhost:9200/palindrome_list?pretty" -H 'Content-Type: application/json' -d'
{
  "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"
          }
        }
      }
    }
  }
}
'

Delimited payload 分词过滤器

根据指定的分隔符将标记流分隔为分词和有效负载。

例如你可以使用带有|分隔符的delimited_payload过滤器将the|1 quick|2 fox|3分词为the,quick,fox,其各自的有效负载为1,2,3

示例

下面的分析API请求使用带有默认|分隔符的delimited_payload过滤器将the|0 brown|10 fox|5 is|0 quick|10分割为分词和有效负载。

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer": "whitespace",
  "filter": ["delimited_payload"],
  "text": "the|0 brown|10 fox|5 is|0 quick|10"
}
'

这个过滤器产出的分词为:

[ the, brown, fox, is, quick ]

提示:这个API不会返回存储的有效负载。包含返回有效负载的例子请查看Return stored payloads

添加到分析器上

下面的创建索引API请求使用delimited_payload过滤器来配置新的自定义分析器。

curl -X PUT "localhost:9200/delimited_payload?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_delimited_payload": {
          "tokenizer": "whitespace",
          "filter": [ "delimited_payload" ]
        }
      }
    }
  }
}
'
配置
  • delimited:(可选的,字符串)用于分隔分词和有效负载的分隔符。默认为|
  • encoding:(可选的,字符串)存储有效负载的数据类型。有效值为:
    • float:默认。float
    • identity:字符
    • intinteger
自定义和添加分析器上

创建自定义的delimited_payload过滤器,复制它作为创建分词过滤器的基础。你可以使用参数来更新这个过滤器。

例如根据以下创建索引的请求来创建一个自定义的delimited_payload分析器。这个自定义的delimited_payload将使用+分隔符来分隔分词和有效负载。有效负载编码为integer

curl -X PUT "localhost:9200/delimited_payload_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_plus_delimited": {
          "tokenizer": "whitespace",
          "filter": [ "plus_delimited" ]
        }
      },
      "filter": {
        "plus_delimited": {
          "type": "delimited_payload",
          "delimiter": "+",
          "encoding": "int"
        }
      }
    }
  }
}
'
返回存储的有效负载
curl -X GET "localhost:9200/text_payloads/_termvectors/1?pretty" -H 'Content-Type: application/json' -d'
{
  "fields": [ "text" ],
  "payloads": true
}
'

edge n-gram分词过滤器

从分词的开始形成一个指定长度的n-gram。例如你可以使用edge_ngram分词过滤器将quick变为qu

当不需要自定义的时候,这个过滤器默认创建一个字符的n-gram

实例

根据以下请求API使用edge_ngram分词过滤器转换quick brown fox jumps为一个字符到两个字符的n-gram

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "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 ]
添加到分析器
curl -X PUT "localhost:9200/edge_ngram_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_edge_ngram": {
          "tokenizer": "standard",
          "filter": [ "edge_ngram" ]
        }
      }
    }
  }
}
'
自定义

要定制edge_ngram过滤器,复制它来创建一个新的自定义分词过滤器的基础。您可以使用过滤器的可配置参数来修改它。

curl -X PUT "localhost:9200/edge_ngram_custom_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "default": {
          "tokenizer": "whitespace",
          "filter": [ "3_5_edgegrams" ]
        }
      },
      "filter": {
        "3_5_edgegrams": {
          "type": "edge_ngram",
          "min_gram": 3,
          "max_gram": 5
        }
      }
    }
  }
}
'

Elision分词过滤器

从分词的开头删除指定的省略符。例如,您可以使用此过滤器将l'avion更改为avion

如果没有自定义,该过滤器默认删除以下法语省略:

l', m', t', qu', n', s', j', d', c', jusqu', quoiqu', lorsqu', puisqu'

这个过滤器的自定义版本包含在几个Elasticsearch的内置语言分析器中:

  • Catalan analyzer
  • French analyzer
  • Irish analyzer
  • Italian analyzer
示例

下面的分析API请求使用省略过滤器从j' examine près du wharf中删除j’:

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer" : "standard",
  "filter" : ["elision"],
  "text" : "j’examine près du wharf"
}
'

过滤器生成出的分词为:

[ examine, près, du, wharf ]
添加到分析器

下面的创建索引API请求使用省略过滤器来配置新的自定义分析器。

curl -X PUT "localhost:9200/elision_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_elision": {
          "tokenizer": "whitespace",
          "filter": [ "elision" ]
        }
      }
    }
  }
}
'
配置参数
  • articles:(必须,字符串列表)。删除elision的列表。要删除省略符,省略符必须位于标记的开头,并立即跟在撇号之后。省略符和撇号都被删除了。对于自定义省略筛选器,必须指定此参数或articles_path

  • articles_path:(必须,字符串)。包含要删除的省略列表的文件路径。该路径必须是绝对的或相对于配置位置的,并且文件必须是UTF-8编码的。文件中的每个省略符必须用换行符分隔。

    要删除省略符,省略符必须位于标记的开头,并立即跟在撇号之后。省略和撇号都被删除了。

    对于自定义省略筛选器,必须指定此参数或项目。

  • articles_case:(可选,Boolean)如果为真,省略匹配不区分大小写。如果为false,省略匹配是区分大小写的。默认值为false。

自定义

若要自定义省略过滤器,请复制它以创建新的自定义令分词过滤器的基础。您可以使用过滤器的可配置参数来修改它。

例如,下面的请求创建了一个自定义不区分大小写的省略符过滤器,用于删除l', m', t', qu', n', s'j'省略符

curl -X PUT "localhost:9200/elision_case_insensitive_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "default": {
          "tokenizer": "whitespace",
          "filter": [ "elision_case_insensitive" ]
        }
      },
      "filter": {
        "elision_case_insensitive": {
          "type": "elision",
          "articles": [ "l", "m", "t", "qu", "n", "s", "j" ],
          "articles_case": true
        }
      }
    }
  }
}
'

指纹分词过滤器

从分词流中对分词进行排序和删除重复的分词,然后将输出的分词流连接成一个分词。

例如,这个过滤器将[the, fox, was, very, very, quick]分词流更改如下:

  • 将分词按字母顺序排序为[ fox, quick, the, very, very, was ]
  • 移除多个重复的分词very
  • 连接分词流并输出一个单独的分词:[fox quick the very was ]

如OpenRefine项目中所述,这个过滤器产生的输出分词对于识别和聚集文本体非常有用。

示例

下面的分析API请求使用fingerprint过滤器通过zebra jumps over resting resting dog文本来创建一个单独的分词。

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer" : "whitespace",
  "filter" : ["fingerprint"],
  "text" : "zebra jumps over resting resting dog"
}
'

这个过滤器查出的分词为:

[ dog jumps over resting zebra ]
添加到分析器中

下面的创建索引API请求使用指纹过滤器来配置新的自定义分析器。

curl -X PUT "localhost:9200/fingerprint_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_fingerprint": {
          "tokenizer": "whitespace",
          "filter": [ "fingerprint" ]
        }
      }
    }
  }
}
'
配置参数
  • max_output_size:(可选的,整数),输出标记的最大字符长度,包括空格。默认为255。连接长度超过此长度的令牌将不会产生任何令牌输出。
  • separator:(可选的,字符串)用于连接标记流输入的字符。默认为空格。
自定义

要自定义指纹筛选器,请复制它以创建新的自定义令牌筛选器的基础。您可以使用过滤器的可配置参数来修改它。

例如,下面的请求创建了一个使用+来连接分词流的自定义指纹过滤器。该过滤器还将输出标记限制为100个字符或更少。

curl -X PUT "localhost:9200/custom_fingerprint_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_": {
          "tokenizer": "whitespace",
          "filter": [ "fingerprint_plus_concat" ]
        }
      },
      "filter": {
        "fingerprint_plus_concat": {
          "type": "fingerprint",
          "max_output_size": 100,
          "separator": "+"
        }
      }
    }
  }
}
'

保留类型分词过滤器

保留或者删除指定类型的分词。例如你想要使用这个过滤器通过仅保留 (字母数字)分词将3 quick foxes改变为quick foxes

提示:分词类型。将字符串转换为分词的时候通过分词器这是分词的类型。例如standard分词器可以生成多种分词类型,其中包含 <ALPHANUM>, <HANGUL>, and <NUM>。跟简单的分析器。例如小写分词器,只生成word分词类型。

某些分词过滤器也可以添加分词类型,例如 synonym 过滤器可以添加分词类型。

包含的示例

以下分析 API 请求使用 keep_types 过滤器仅保留来自 1 quick fox 2 lazy dogs 的 (数字)分词。

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "keep_types",
      "types": [ "<NUM>" ]
    }
  ],
  "text": "1 quick fox 2 lazy dogs"
}
'

过滤器产生以下标记:

[ 1, 2 ]
不包含的示例

以下分析 API 请求使用 keep_types 过滤器从 1 quick fox 2 lazy dogs 中删除 类型的分词。注意 mode 参数设置为 exclude

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "keep_types",
      "types": [ "<NUM>" ],
      "mode": "exclude"
    }
  ],
  "text": "1 quick fox 2 lazy dogs"
}
'

过滤器产生以下标记:

[ quick, fox, lazy, dogs ]
配置参数
  • types:(必须的,字符串数组)删除和保留的分词列表
  • mode:(可选的,字符串),是否保留或者删除指定的分词类型。有效值为:
    • include
    • exclude
自定义和添加到分析器

如果需要自定义keep_types过滤去,请复制下边的语句作为创建keep_types过滤器的基础。你可以使用配置参数修改它。

例如,以下创建索引 API 请求使用自定义 keep_types 过滤器来配置新的自定义分析器。自定义 keep_types 过滤器仅保留 (字母数字)标记。

curl -X PUT "localhost:9200/keep_types_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [ "extract_alpha" ]
        }
      },
      "filter": {
        "extract_alpha": {
          "type": "keep_types",
          "types": [ "<ALPHANUM>" ]
        }
      }
    }
  }
}
'

保留单词分词过滤器

只保留包含在指定单词列表里的分词。

提示:从分词中移除单词请使用stop过滤器

示例

根据以下请求API使用keep过滤器只保留the quick fox jumps over the lazy dog中的foxdog分词。

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "keep",
      "keep_words": [ "dog", "elephant", "fox" ]
    }
  ],
  "text": "the quick fox jumps over the lazy dog"
}
'

这过滤器生产的分词为:

[ fox, dog ]
配置参数
  • keep_words:(必须,字符串数组)。保留单词的列表。在输出结果中只包含在保留列表中匹配到的分词。
  • keep_words_path:(必须,字符串数组)包含保留单词列表的文件路径。这个路径相对于config必须是绝对路径或者相对路径,并且文件的编码为utf-8。每一个单词在文件中必须以换行符分隔。
  • keep_words_case:(可选的,boolean)。如果为true,转换所有单词为小写,默认为false
自定义和添加到分析器上

如果需要自定义keep过滤去,请复制下边的语句作为创建keep过滤器的基础。你可以使用配置参数修改它。

curl -X PUT "localhost:9200/keep_words_example?pretty" -H 'Content-Type: application/json' -d'
{
  "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"
        }
      }
    }
  }
}
'

keyword标记分词过滤器

标记指定的分词作为keyword使之不能被词干提取。

keyword_marker过滤器为指定的分词设置keyword属性为true。提取词干的过滤器,例如stemmer或者proter_stem会忽略keyword属性为true的分词。

提示:如果想要让keyword_marker过滤器生效,需要在stemmer过滤器之前列出。

示例

要查看keyword_marker过滤器的工作原理,你首先需要生产出被词干提取的分词的分词流。

根据以下请求API使用stemmer过滤器为fox running and jumping创建词干分词。

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer": "whitespace",
  "filter": [ "stemmer" ],
  "text": "fox running and jumping"
}
'

以上的请求生成出以下分词。提示,running被词干为runjumping被词干为jump

[ fox, run, and, jump ]

为了避免jumping被提取词干,添加keyword_marker过滤器到stemmer过滤器之前。在keyword_marker过滤器的keyword参数为jumping

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "keyword_marker",
      "keywords": [ "jumping" ]
    },
    "stemmer"
  ],
  "text": "fox running and jumping"
}
'

这个请求生成出以下分词。running仍然被提取词干为run,但是jumping没有被提取词干。

[ fox, run, and, jumping ]
配置参数
  • ignore_case:(可选,boolean),如果为true,则keywordskeywords_path匹配会忽略大小写。默认为false

  • keywords:(必须,字符串列表)。关键字列表。分词匹配到这些关键字将不会被提取词干。必须指定这个参数、keywords_pathkeywords_pattern。您不能同时指定此参数和 keyword_pattern

  • keywords_path:(必需*,字符串)包含关键字列表的文件的路径。与这些关键字匹配的分词不会被提取词干。必须指定这个参数、keywords_path 或keywords_pattern。您不能同时指定此参数和 keyword_pattern

  • keywords_pattern:(必需*,字符串)用于匹配分词的 Java 正则表达式。与此表达式匹配的分词被标记为关键字而不是词干。必须指定这个参数、keywordskeywords_path。您不能同时指定此参数和 keywords_pathkeywords

自定义并且新增到分析器上

要自定义 keyword_marker 过滤器,请复制它以创建新的自定义分词过滤器的基础。您可以使用过滤器的可配置参数来修改过滤器。

例如,以下创建索引 API 请求使用自定义 keyword_marker 过滤器和 porter_stem 过滤器来配置新的自定义分析器。

自定义 keyword_marker 过滤器将 analysis/example_word_list.txt 文件中指定的分词标记为关键字。 porter_stem 过滤器不会提取词干这些标记。

curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d'
{
  "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"
        }
      }
    }
  }
}
'

长度分词过滤器

删除短于或长于指定字符长度的分词。例如,您可以使用长度过滤器来排除短于 2 个字符的分词和长于 5 个字符的分词。

提示:长度分词过滤器删除的是整个分词。如果你希望分词缩短到特定的长度,请使用truncate过滤器。

示例

根据以下示例,利用长度分词过滤器删除超过4个字符的分词:

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "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 ]
添加到分析器上
curl -X PUT "localhost:9200/length_example?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_length": {
          "tokenizer": "standard",
          "filter": [ "length" ]
        }
      }
    }
  }
}
'
配置参数
  • min:(可选,integer),分词的最小字符长度。较短的分词从输出中排除。默认为 0。
  • max:(可选,integer),分词的最大字符长度。更长的分词被排除在输出之外。默认为 Integer.MAX_VALUE,即 2^31-1 或 2147483647。
自定义

要自定义长度过滤器,请复制它以创建新的自定义标记过滤器的基础。您可以使用过滤器的可配置参数来修改过滤器。

例如,以下请求创建了一个自定义长度过滤器,用于删除短于 2 个字符的分词和长于 10 个字符的分词:

curl -X PUT "localhost:9200/length_custom_example?pretty" -H 'Content-Type: application/json' -d'
{
  "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
        }
      }
    }
  }
}
'

小写分词过滤器

模式捕获分词过滤器

模式替换分词过滤器

脚本分词过滤器

移除重复分词过滤器

反向分词过滤器

提取词干分词过滤器

提取词干覆盖分词过滤器

停用词分词过滤器

trim分词过滤器

截断分词过滤器

唯一分词过滤器

大写分词过滤器

单词分隔符分词过滤器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值