elasticsearch分词器和自定义分词器

分词器介绍

https://www.elastic.co/guide/en/elasticsearch/reference/8.7/analysis-analyzers.html

查看分词结果(standard-analyzer,es默认)

https://www.elastic.co/guide/en/elasticsearch/reference/8.7/analysis-standard-analyzer.html

post _analyze
{
  "analyzer": "standard",
  "text": "分词的数"
}

中文分词结果

post _analyze
{
  "analyzer": "standard",
  "text": "我喜欢看小说"
}
{
    "tokens": [
        {
            "token": "我",
            "start_offset": 0,
            "end_offset": 1,
            "type": "<IDEOGRAPHIC>",
            "position": 0
        },
        {
            "token": "喜",
            "start_offset": 1,
            "end_offset": 2,
            "type": "<IDEOGRAPHIC>",
            "position": 1
        },
        {
            "token": "欢",
            "start_offset": 2,
            "end_offset": 3,
            "type": "<IDEOGRAPHIC>",
            "position": 2
        },
        {
            "token": "看",
            "start_offset": 3,
            "end_offset": 4,
            "type": "<IDEOGRAPHIC>",
            "position": 3
        },
        {
            "token": "小",
            "start_offset": 4,
            "end_offset": 5,
            "type": "<IDEOGRAPHIC>",
            "position": 4
        },
        {
            "token": "说",
            "start_offset": 5,
            "end_offset": 6,
            "type": "<IDEOGRAPHIC>",
            "position": 5
        }
    ]
}

英文分词结果

post _analyze
{
  "analyzer": "standard",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
{
    "tokens": [
        {
            "token": "the",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "2",
            "start_offset": 4,
            "end_offset": 5,
            "type": "<NUM>",
            "position": 1
        },
        {
            "token": "quick",
            "start_offset": 6,
            "end_offset": 11,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "brown",
            "start_offset": 12,
            "end_offset": 17,
            "type": "<ALPHANUM>",
            "position": 3
        },
        {
            "token": "foxes",
            "start_offset": 18,
            "end_offset": 23,
            "type": "<ALPHANUM>",
            "position": 4
        },
        {
            "token": "jumped",
            "start_offset": 24,
            "end_offset": 30,
            "type": "<ALPHANUM>",
            "position": 5
        },
        {
            "token": "over",
            "start_offset": 31,
            "end_offset": 35,
            "type": "<ALPHANUM>",
            "position": 6
        },
        {
            "token": "the",
            "start_offset": 36,
            "end_offset": 39,
            "type": "<ALPHANUM>",
            "position": 7
        },
        {
            "token": "lazy",
            "start_offset": 40,
            "end_offset": 44,
            "type": "<ALPHANUM>",
            "position": 8
        },
        {
            "token": "dog's",
            "start_offset": 45,
            "end_offset": 50,
            "type": "<ALPHANUM>",
            "position": 9
        },
        {
            "token": "bone",
            "start_offset": 51,
            "end_offset": 55,
            "type": "<ALPHANUM>",
            "position": 10
        }
    ]
}

自定义分词器

https://www.elastic.co/guide/en/elasticsearch/reference/8.7/analysis-custom-analyzer.html

自定义分词器不是全局的,是归索引库

PUT my-index-000001
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type": "custom", 
          "tokenizer": "standard",
          "char_filter": [
            "html_strip"
          ],
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  }
}

POST my-index-000001/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": "Is this <b>déjà vu</b>?"
}
type分析仪类型。接受内置分析器类型。对于自定义分析器,使用custom或省略此参数。
tokenizer内置或定制的分词器。(必需的)https://www.elastic.co/guide/en/elasticsearch/reference/8.7/analysis-tokenizers.html分词器(对字段进行切分)
char_filter一组可选的内置或自定义 字符过滤器https://www.elastic.co/guide/en/elasticsearch/reference/8.7/analysis-charfilters.html字符过滤器(在一段文本进行分词之前,先进行预处理,比如过滤html标签等)
filter可选的内置或自定义 令牌过滤器数组。https://www.elastic.co/guide/en/elasticsearch/reference/8.7/analysis-tokenfilters.htmltoken过滤器(对切分的单词进行加工,如大小写转换等)
position_increment_gap当索引文本值数组时,Elasticsearch 在一个值的最后一项和下一个值的第一项之间插入一个假的“间隙”,以确保短语查询不匹配来自不同数组元素的两个项。默认为100. 查看position_increment_gap更多。

顺序: character filter -> tokenizer -> token filter

支持个数:char_filter(0个或多个)+tokenizer(一个)+filter(0个或多个)

POST /_analyze
{
    "tokenizer": "ik_max_word", 
    "filter": ["lowercase","pinyin","asciifolding"],
    "char_filter":["html_strip"],
    "text": "你知道 Elasticesearch吗?"
}


POST /_analyze
{
    "tokenizer": "ik_max_word", 
    "filter": ["lowercase"],
    "char_filter":["html_strip"],
    "text": "你知道 Elasticesearch吗?"
}

POST /_analyze
{
    "tokenizer": "ik_max_word", 
    "filter": ["pinyin"],
    "char_filter":["html_strip"],
    "text": "你知道 Elasticesearch吗?"
}


POST /_analyze
{
    "tokenizer": "ik_max_word", 
    "filter": ["pinyin","ik_smart"],
    "char_filter":["html_strip"],
    "text": "你知道 Elasticesearch吗?"
}

POST /_analyze
{
    "tokenizer": "pinyin", 
    "char_filter":["html_strip"],
    "text": "你知道 Elasticesearch吗?"
}

POST /_analyze
{
    "tokenizer": "pinyin", 
    "char_filter":["html_strip"],
    "text": "你知道 Elasticesearch吗?</html>"
}

POST /_analyze
{
    "tokenizer": "ik_smart", 
    "char_filter":["html_strip"],
    "text": "你知道 Elasticesearch吗?</html>"
}


#char_filter mappings的使用
GET /_analyze
{
  "tokenizer": "keyword",
  "char_filter": [
    {
      "type": "mapping",
      "mappings": [
        "٠ => 0",
        "١ => 1",
        "٢ => 2",
        "٣ => 3",
        "٤ => 4",
        "٥ => 5",
        "٦ => 6",
        "٧ => 7",
        "٨ => 8",
        "٩ => 9"
      ]
    }
  ],
  "text": "My license plate is ٢٥٠١٥"
}


GET /_analyze
{
  "tokenizer": "ik_smart",
  "char_filter": [
    {
      "type": "mapping",
      "mappings": [
        "٠ => 0",
      ]
    }
  ],
  "text": "My license plate is ٠"
}


GET /_analyze
{
  "tokenizer": "ik_smart",
  "char_filter": [
    {
      "type": "pattern_replace",
      "pattern": "(\\d+)-(?=\\d)",
      "replacement": ""
    }
  ],
  "text": "My credit card is 123-456-789"
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值