因lucene默认采用英文且英文通过空格就可以断句。而中文则是词组,如果不加载中文词库或插件则会变为一个一个字而非词组,因此需要加载中文词库。

不加分词库所看到的中文分词效果。

post _analyze
{
   "text": "中国人民" 
}

结果 变为了1个字一个字的:

{
   "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
      }
   ]
}

词库下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases

https://github.com/medcl/elasticsearch-analysis-ik (readme.txt 阅读安装)

将下载的内容copy到elasticsearch的plugin/ik文件夹下,如果没有则建立此文件夹。重启有效。

ik的作用域

standard

  不需要特别定义(默认)

system

  在es早期版本可通过在yml中配置 index.analysis.analyzer.default.type: ik

  错误 "node settings must not contain any index level settings"

  5.x之后elastic不允许在yml文件中添加以index开头的配置文件,要求这些都必须在es启动后通过接口传递

index

  首先创建index,然后对index设定属性,最后查看。这里使用的是 sense

//创建索引

put /testindex

// 设置analysis

POST /testindex/fulltext/_mapping
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
}

// 测试

post testindex/_analyze
{
"analyzer": "ik_max_word",
   "text": "中国人民" 
}

// 结果

{
   "tokens": [
      {
         "token": "中国人民",
         "start_offset": 0,
         "end_offset": 4,
         "type": "CN_WORD",
         "position": 0
      },
      {
         "token": "中国人",
         "start_offset": 0,
         "end_offset": 3,
         "type": "CN_WORD",
         "position": 1
      },
      {
         "token": "中国",
         "start_offset": 0,
         "end_offset": 2,
         "type": "CN_WORD",
         "position": 2
      },
      {
         "token": "国人",
         "start_offset": 1,
         "end_offset": 3,
         "type": "CN_WORD",
         "position": 3
      },
      {
         "token": "人民",
         "start_offset": 2,
         "end_offset": 4,
         "type": "CN_WORD",
         "position": 4
      }
   ]
}