• 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

  • 相关阅读:
    数据结构--6.2关键路径
    2023 年您需要了解的 5 大移动应用程序开发趋势
    清华开源ChatGPT自动编程ChatDev项目结构和关键代码解析
    机器学习笔记 - TransUNet 用于医学图像分割的编码器
    java版的上门家政系统和PHP版的上门家政有什么区别?
    芯科蓝牙BG27开发笔记4-SSV5 IDE的使用
    (完整版)《光学教程》(姚启钧)课后习题解答
    1.Python 设计模式
    抓的是周树人,与我鲁迅有什么关系?
    CMU DLSys 课程笔记 2 - ML Refresher / Softmax Regression
  • 原文地址:https://blog.csdn.net/m0_67394002/article/details/126362403