• ElasticSearch源码解析(二):ES的CharFilter、Tokenizer、TokenizerFilter


    CharFilter

    elasticsearch的CharFilter一共有2种分别是:HTMLStripCharFilter、MappingCharFilter。

    HTMLStripCharFilter

    这是用来去除html标签的filter,主要的逻辑在read方法中:

    public int read() throws IOException {
        //输入字符的字节是否已读完
        if (outputSegment.isRead()) {
          if (zzAtEOF) {
            return -1;
          }
          // 如果已读完则返回当前字符的下一个字符
          int ch = nextChar();
          ++outputCharCount;
          return ch;
        }
        //如果这个textsegment还没有读完则返回已读部分的下一个字符
        int ch = outputSegment.nextChar();
        ++outputCharCount;
        return ch;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    nextChar()函数里面是个包含了一个有限自动机。如果不懂什么是有限自动机的,有限自动机的概念请参考编译原理的词法分析来了解。自动机的状态图我没有深入研究过,而且代码冗长也比较晦涩这里就不做介绍了。

    MappingCharFilter

    它主要是用来做字符串匹配替换的,可以通过NormalizeCharMap.Builder的addPair来添加替换规则。

    Tokenizer

    一共有10种tokenizer,用户需要根据搜索内容的语言特性做出选择,一般中文分词使用第三方的分词器比如ik analyzer,我们这里仅以其中一个举例说明。

    StandardTokenizer是es默认的tokenizer,使用的是字典匹配类型的分词算法,它依据的是unicode编码,比较适应欧洲语言,它同样使用了一个有限自动机来实现字符串前缀匹配算法,将不同的词映射为以下14种类型:

    public static final String [] TOKEN_TYPES = new String [] {
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        ""
      };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    TokenFilter

    一共有三种TokenFilter,也是使用同样的字符串前缀匹配算法来实现功能,这里以ClassicFilter为例说明,它主要用来去除像‘s这样的字母或者首字母缩略词中的点符号。

    和tokenizer一样同样有个incrementToken方法:

    public final boolean incrementToken() throws java.io.IOException {
        if (!input.incrementToken()) {
          return false;
        }
    
        final char[] buffer = termAtt.buffer();
        final int bufferLength = termAtt.length();
        final String type = typeAtt.type();
    
        if (type == APOSTROPHE_TYPE &&      // remove 's
            bufferLength >= 2 &&
            buffer[bufferLength-2] == ''' &&
            (buffer[bufferLength-1] == 's' || buffer[bufferLength-1] == 'S')) {
          // Strip last 2 characters off
          termAtt.setLength(bufferLength - 2);
        } else if (type == ACRONYM_TYPE) {      // remove dots
          int upto = 0;
          for(int i=0;i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

  • 相关阅读:
    VSCode设置中文语言界面(VScode设置其他语言界面)
    LeetCode 6222. 美丽整数的最小增量
    浮点数在内存中的存储
    设计模式(一)| 创建型模式(单例模式、原型模式等)
    C++——类型转换
    前端学习之HTML(第一天)
    电脑重装系统后Word文档如何横向排版
    08项目质量管理
    HDLbits 刷题 -- Kmap3
    硒化镉量子点 CdSe QDs 的产品形态和产品规格描述
  • 原文地址:https://blog.csdn.net/m0_67394002/article/details/126362403