1 索引分析

索引分析: 将文本块按照一定的策略分解, 并建立倒排索引的过程. 这个过程由分析器(analyzers)完成.

1.1 分析器的组成

① 字符过滤器(character filter): 去除HTML标签, 或转换"&"为"and"等.

② 分词器(tokenizer): 按照某种规律, 如根据空格、逗号等, 将文本块进行分解.

③ 标记过滤器(token filter): 所有被分词器分解的词都将经过token filters的处理, 它可以修改词(如小写化处理)、去掉词(根据某一规则去掉无意义的词, 如"a", "the", "的"等), 增加词(如同义词"jump"、"leap"等).

注意: 人们一般将分析器通称为分词器, 并不是相等的关系, 而是包含的关系.

1.2 倒排索引核心原理-normalization

建立倒排索引时, 会执行 normalization(正常化)操作 —— 将拆分的各个单词进行相应的处理, 以提高搜索时命中相关联的文档的概率.

normalization的方式有: 时态转换, 单复数转换, 同义词转换, 大小写转换等.

比如: 文档中包含 "mom likes small dogs", 在建立索引时通过normalization处理, 通过 "mother liked little dog" 也能搜索到相关的文档.

2 ES的默认分析器

(1) ES中的默认分析器: standard tokenizer, 标准分析器, 以单词为边界进行分词. 具有如下功能:

standard token filter: 去掉无意义的标签, 如<>, &, - 等.

lowercase token filter: 将所有字母转换为小写字母.

stop token filer(默认被禁用): 移除停用词, 比如"a"、"the"等.

(2) 测试默认分析器:

GET _analyze            // ES引擎中已有standard分词器, 所以可以不指定index
{
    "analyzer": "standard", 
    "text": "There-is a DOG<br/> in house &"
}

3 修改分词器

(1) 创建索引后可以添加新的分词器, 添加之前必须先关闭索引, 添加之后再打开:

// 先关闭索引: 
POST address/_close

// 启用English停用词token filter
PUT address/_settings
{
    "analysis": {
        "analyzer": {
            "my_token_filter": {        // 自定义的分词器名称
                "type": "standard",
                "stopwords": "_english_"
            }
        }
    }
}

// 打开索引: 
POST address/_open

(2) 使用具有停词功能的分词器进行分词:

GET address/_analyze
{
    "analyzer": "my_token_filter", 
    "text": "There-is a DOG<br/> in house &"
}

(3) 返回结果减少了停用词:

{
    "tokens": [
        {
            "token": "dog",
            "start_offset": 11,
            "end_offset": 14,
            "type": "<ALPHANUM>",
            "position": 3
        },
        {
            "token": "br",
            "start_offset": 15,
            "end_offset": 17,
            "type": "<ALPHANUM>",
            "position": 4
        },
        {
            "token": "house",
            "start_offset": 23,
            "end_offset": 28,
            "type": "<ALPHANUM>",
            "position": 6
        }
    ]
}

4 定制分词器

4.1 向索引中添加自定义的分词器

注意: 要先关闭索引, 再添加, 然后再打开索引.

PUT address/_settings
{
    "analysis": {
        "char_filter": {
            "&_to_and": {
                "type": "mapping",
                "mappings": ["&=> and"]
            }
        },
        "filter": {
            "my_stopwords": {
                "type": "stop",
                "stopwords": ["the", "a"]
            }
        },
        "analyzer": {
            "my_analyzer": {            // 自定义的分析器名称
                "type": "custom",
                "char_filter": ["html_strip", "&_to_and"],  // 跳过HTML标签, 将&符号转换为"and"
                "tokenizer": "standard",
                "filter": ["lowercase", "my_stopwords"]     // 转换为小写
            }
        }
    }
}

4.2 测试自定义分析器

GET address/_analyze
{
    "analyzer": "my_analyzer", 
    "text": "There-is a DOG<br/> in house &"
}

响应结果中已经对大写单词、HTML标签, 以及"&"做了处理. 鉴于篇幅所限, 这里省去.

4.3 向映射中添加自定义的分析器

PUT address/_mapping/province
{
    "properties": {
        "content": {
            "type": "text",
            "analyzer": "my_analyzer"
        }
    }
}

此时查看mapping信息:

GET address/_mapping

发现自定义的分析器已经配置到province上了:

{
  "address": {
    "mappings": {
      "province": {
        "properties": {
          "content": {
            "type": "text",
            "analyzer": "my_analyzer"
          },
          "description": {
            "type": "text"
          }
        }
      }
    }
  }
}

版权声明

作者: ma_shoufeng(马瘦风)

出处: 博客园 马瘦风的博客

您的支持是对博主的极大鼓励, 感谢您的阅读.

本文版权归博主所有, 欢迎转载, 但未经博主同意必须保留此段声明, 且在文章页面明显位置给出原文链接, 否则博主保留追究相关人员法律责任的权利.

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!