欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ElasticSearch ik分词器基础知识analyzer

程序员文章站 2022-03-11 08:29:04
...

概述

有两种analyzer,你根据自己的需要自己选吧,但是一般是选用ik_max_word
如果是ik_max_word的话: 会将文本做最细粒度的拆分,比如会将“*国歌”拆分为“*,中华人民,中华,华人,人民*,人民,人,民,*,共和,和,国国,国歌”,会穷尽各种可能的组合;

如果使用ik_smart的话 会做最粗粒度的拆分,比如会将“*国歌”拆分为“*,国歌”。
如果我们搜索"*" 搜到吗? 这样就搜不到的.

结论

一般我们是使用ik_max_word,因为拆分比较细粒度,这样搜索效果会更好.

ik分词器的使用

指定my_type的analyzer 为 ik_max_word

PUT /my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "text": {
          "type": "text",
          "analyzer": "ik_max_word"
        }
      }
    }
  }
}

添加数据

POST /my_index/my_type/_bulk
{ "index": { "_id": "1"} }
{ "text": "男子偷上万元发红包求交女友 被抓获时仍然单身" }
{ "index": { "_id": "2"} }
{ "text": "16岁少女为结婚“变”22岁 7年后想离婚被法院拒绝" }
{ "index": { "_id": "3"} }
{ "text": "深圳女孩骑车逆行撞奔驰 遭索赔被吓哭(图)" }
{ "index": { "_id": "4"} }
{ "text": "女人对护肤品比对男票好?网友神怼" }
{ "index": { "_id": "5"} }
{ "text": "为什么国内的街道招牌用的都是红黄配?" }

测试一下搜索效果

查看分词效果

GET /my_index/_analyze
{
"text": "男子偷上万元发红包求交女友 被抓获时仍然单身",
"analyzer": "ik_max_word"
}

结果: 下面分词那么多了

{
  "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
    },
    {
      "token": "上万",
      "start_offset": 3,
      "end_offset": 5,
      "type": "CN_WORD",
      "position": 2
    },
    {
      "token": "万元",
      "start_offset": 4,
      "end_offset": 6,
      "type": "CN_WORD",
      "position": 3
    },
    {
      "token": "万",
      "start_offset": 4,
      "end_offset": 5,
      "type": "TYPE_CNUM",
      "position": 4
    },
    {
      "token": "元",
      "start_offset": 5,
      "end_offset": 6,
      "type": "COUNT",
      "position": 5
    },
    {
      "token": "发红包",
      "start_offset": 6,
      "end_offset": 9,
      "type": "CN_WORD",
      "position": 6
    },
    {
      "token": "发红",
      "start_offset": 6,
      "end_offset": 8,
      "type": "CN_WORD",
      "position": 7
    },
    {
      "token": "红包",
      "start_offset": 7,
      "end_offset": 9,
      "type": "CN_WORD",
      "position": 8
    },
    {
      "token": "求",
      "start_offset": 9,
      "end_offset": 10,
      "type": "CN_CHAR",
      "position": 9
    },
    {
      "token": "交",
      "start_offset": 10,
      "end_offset": 11,
      "type": "CN_CHAR",
      "position": 10
    },
    {
      "token": "女友",
      "start_offset": 11,
      "end_offset": 13,
      "type": "CN_WORD",
      "position": 11
    },
    {
      "token": "被",
      "start_offset": 14,
      "end_offset": 15,
      "type": "CN_CHAR",
      "position": 12
    },
    {
      "token": "抓获",
      "start_offset": 15,
      "end_offset": 17,
      "type": "CN_WORD",
      "position": 13
    },
    {
      "token": "时",
      "start_offset": 17,
      "end_offset": 18,
      "type": "CN_CHAR",
      "position": 14
    },
    {
      "token": "仍然",
      "start_offset": 18,
      "end_offset": 20,
      "type": "CN_WORD",
      "position": 15
    },
    {
      "token": "单身",
      "start_offset": 20,
      "end_offset": 22,
      "type": "CN_WORD",
      "position": 16
    }
  ]
}

测试查询数据

_score值越高的排前面.

GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "text": "16岁少女结婚好还是单身好?"
    }
  }
}

结果

{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 2.6093416,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "2",
        "_score": 2.6093416,
        "_source": {
          "text": "16岁少女为结婚“变”22岁 7年后想离婚被法院拒绝"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "4",
        "_score": 1.3300087,
        "_source": {
          "text": "女人对护肤品比对男票好?网友神怼"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "1",
        "_score": 0.26301134,
        "_source": {
          "text": "男子偷上万元发红包求交女友 被抓获时仍然单身"
        }
      }
    ]
  }
}