es - elasticsearch自定义分析器 - 内建分词过滤器 - 9

106 阅读2分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

自定义分析器 :

  1. Character filters :
    1. 作用 : 字符的增、删、改转换
    2. 数量限制 : 可以有0个或多个
    3. 内建字符过滤器 :
    1. HTML Strip Character filter : 去除html标签
    2. Mapping Character filter : 映射替换
    3. Pattern Replace Character filter : 正则替换
  2. Tokenizer :
    1. 作用 :
    1. 分词
    2. 记录词的顺序和位置(短语查询)
    3. 记录词的开头和结尾位置(高亮)
    4. 记录词的类型(分类)
    2. 数量限制 : 有且只能有一个
    3. 分类 :
    1. 完整分词 :
    1. Standard
    2. Letter
    3. Lowercase
    4. whitespace
    5. UAX URL Email
    6. Classic
    7. Thai
    2. 切词 :
    1. N-Gram
    2. Edge N-Gram
    3. 文本 :
    1. Keyword
    2. Pattern
    3. Simple Pattern
    4. Char Group
    5. Simple Pattern split
    6. Path
  3. Token filters :
    1. 作用 : 分词的增、删、改转换
    2. 数量限制 : 可以有0个或多个
    3. 分类 :
    1. apostrophe
    2. asciifolding
    3. cjk bigram
    4. cjk width
    5. classic
    6. common grams
    7. conditional
    8. decimal digit
    9. delimited payload
    10. dictionary decompounder
    11. edge ngram
    12. elision
    13. fingerprint
    14. flatten_graph
    15. hunspell
    16. hyphenation decompounder
    17. keep types
    18. keep words
    19. keyword marker
    20. keyword repeat
    21. kstem
    22. length
    23. limit token count
    24. lowercase
    25. min_hash
    26. multiplexer
    27. ngram
    28. normalization
    29. pattern_capture
    30. pattern replace
    31. porter stem
    32. predicate script
    33. remove duplicates

今天演示30-33

# pattern replace token filter
# 作用 :
#   1. 匹配替换
# 配置项 :
#   1. all : 是否全部替换,true - 是,false - 只替换第一个,默认true
#   2. pattern
#   3. replacement

GET /_analyze
{
  "tokenizer" : "keyword",
  "filter" : [{
    "type"        : "pattern_replace",
    "pattern"     : "(hello)",
    "replacement" : "good$1",
    "all"         : false
  }],
  "text" : ["hello hello good morning"]
}

# 结果
{
  "tokens" : [
    {
      "token" : "goodhello hello good morning",
      "start_offset" : 0,
      "end_offset" : 24,
      "type" : "word",
      "position" : 0
    }
  ]
}
# porter stem token filter
# 作用 :
#   1. 英语词干提取

GET /_analyze
{
  "tokenizer" : "whitespace",
  "filter"    : ["porter_stem"],
  "text"      : ["hello gooding me luckly"]
}

# 结果
{
  "tokens" : [
    {
      "token" : "hello",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "good",
      "start_offset" : 6,
      "end_offset" : 13,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "me",
      "start_offset" : 14,
      "end_offset" : 16,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : "luckli",
      "start_offset" : 17,
      "end_offset" : 23,
      "type" : "word",
      "position" : 3
    }
  ]
}
# predicate script token filter
# 作用   : 通过脚本过滤掉不符合条件的词
# 配置项 :
#   1. script : 脚本

GET /_analyze
{
  "tokenizer": "whitespace",
  "filter"   : [{
    "type"   : "predicate_token_filter",
    "script" : {
      "source" : """
        token.term.length() > 3
      """
    }
  }],
  "text" : ["hello gooding me 中国人的"]
}

# 结果
{
  "tokens" : [
    {
      "token" : "hello",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "gooding",
      "start_offset" : 6,
      "end_offset" : 13,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "中国人的",
      "start_offset" : 17,
      "end_offset" : 21,
      "type" : "word",
      "position" : 3
    }
  ]
}
# remove duplicates token filter
# 作用 : 去除同一position下重复的词
# 条件 : 与会产生同一位置相同词的过滤器一起使用才能发挥作用
# 如  : "keyword_repeat", "stemmer" 会在同一位置产生重复的"hello"

GET /_analyze
{
  "tokenizer" : "whitespace",
  "filter"    : ["keyword_repeat", "stemmer", "remove_duplicates"],
  "text"      : ["hello gooding"]
}

# 结果
{
  "tokens" : [
    {
      "token" : "hello",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "gooding",
      "start_offset" : 6,
      "end_offset" : 13,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "good",
      "start_offset" : 6,
      "end_offset" : 13,
      "type" : "word",
      "position" : 1
    }
  ]
}