ES使用——分词器


前言

提示:ES 宗旨 专注于搜索,ES也有内置的分词器,但是对于中文分词我们还是比较喜欢使用ik分词器,该文章主要讲述下关于分词器的使用


一、analyzer介绍

analyzer是mapping映射时候使用的,主要是text的属性,text在不设置analyzer的时候,默认使用的是standard分词。

可选内置分词:

whitespace空格分隔
simple
stopstopwords用_english_

非内置的分词ik

ik_max_word细分
ik_smart简单的分词

二、analyzer使用

1.分词查看whitespace

代码如下(示例):

#请求
POST _analyze
{
  "analyzer":"whitespace",
  "text": "茅 台,酒 业酱香"
}

#结果
{
  "tokens" : [
    {
      "token" : "茅",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "台,酒",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "业酱香",
      "start_offset" : 6,
      "end_offset" : 9,
      "type" : "word",
      "position" : 2
    }
  ]
}

2.分词查看simple

代码如下(示例):

#请求
POST _analyze
{
  "analyzer":"simple",
  "text": "茅 台,酒 业酱香"
}

#结果
{
  "tokens" : [
    {
      "token" : "茅",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "台",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "酒",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : "业酱香",
      "start_offset" : 6,
      "end_offset" : 9,
      "type" : "word",
      "position" : 3
    }
  ]
}

3.分词查看standard

代码如下(示例):

#请求
POST _analyze
{
  "analyzer":"simple",
  "text": "茅 台,酒 业酱香"
}

#结果
{
  "tokens" : [
    {
      "token" : "茅",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "台",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "酒",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "业",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "酱",
      "start_offset" : 7,
      "end_offset" : 8,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    },
    {
      "token" : "香",
      "start_offset" : 8,
      "end_offset" : 9,
      "type" : "<IDEOGRAPHIC>",
      "position" : 5
    }
  ]
}


4.分词查看ik_max_word

代码如下(示例):

#请求
POST _analyze
{
  "analyzer":"ik_max_word",
  "text": "茅台酒业"
}

#结果
{
  "tokens" : [
    {
      "token" : "茅台酒",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "茅台",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "茅",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "台",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "酒业",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "酒",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "业",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 6
    }
  ]
}

5.分词查看ik_smart

代码如下(示例):

#请求
POST _analyze
{
  "analyzer":"ik_smart",
  "text": "茅台酒业"
}

#结果
{
  "tokens" : [
    {
      "token" : "茅台",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "酒业",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}


三、注意事项

注意:
【分词analyzer】结果要 包含【查询分词结果search_analyzer】
只设置【分词】就不用注意了

示例代码:

PUT /test_1/test/_mapping
{
  "test": {
    "properties": {
      "application": {
        "type": "keyword",
        "ignore_above": 10
      },
      "contentName": {
        "type": "text",
        #ik_smart “茅台酒业” 分为【茅台 酒业】
        "analyzer": "ik_smart",
        #whitespace 可查询到结果 【茅台 酒业】
        #			查不到【茅台酒业 茅 台 酒 业】
        "search_analyzer": "whitespace"
      },
      "id": {
        "type": "long"
      }
    }
  }
}

总结

例如:以上就是今天要讲的内容,但一定要注意【查询分词】 和 【分词】的包容性质,当然只设置【分词】也能完全使用的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值