博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticsearch-ik
阅读量:4334 次
发布时间:2019-06-07

本文共 3852 字,大约阅读时间需要 12 分钟。

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

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

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

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

{   "tokens": [      {         "token": "中",         "start_offset": 0,         "end_offset": 1,         "type": "
", "position": 0 }, { "token": "国", "start_offset": 1, "end_offset": 2, "type": "
", "position": 1 }, { "token": "人", "start_offset": 2, "end_offset": 3, "type": "
", "position": 2 }, { "token": "民", "start_offset": 3, "end_offset": 4, "type": "
", "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

  目前ik的analysis只能通过挂载在index下,对指定的属性使用。如果新加属性要使用ik,则先到map中进行维护增加属性要使用的analysis。

 //创建索引

put /testindex

 

 // 设置analysis 注意_mapping中一个索引对属性创建的map,一旦建立后不能修改,只能新增。

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

 以上可以看出仅content属性具有ik索词效果,其它属性不具备。

mapping添加 address错误: Mapper for [address] conflicts with existing mapping in other types:\n[mapper [address] has different [analyzer]] 

 这是因为已经建立了一个属性数据,而这个属性数据在建立时会自动给分配一个mapping映射,因此在建立mapper时说已经存在有一个不同类型的属性。即使删除这笔数也不行,因为属性是只能增加不能修改。

切记切记!!

ik_max_word 以最大的分词形式进行分词,精细粒度

ik_smart 以最敏捷的分词形式分词,粗粒度

注意:原先老版本的ik已经被ik_max_word,ik_smart取代

 // 测试  不要加type不然以为是create数据

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

 同时可通过建立索引模板来创建索引统一格式。

DELETE _template/temp_ikPOST _template/temp_ik{  "index_patterns": ["ik_*", "*_ik"],  "settings": {    "number_of_shards": 2  },  "mappings": {    "type1": {      "_source": {        "enabled": true      },      "properties": {        "title": {          "type": "text",            "analyzer": "ik_max_word",                "search_analyzer": "ik_max_word"        },        "name":{            "type": "text",            "analyzer": "ik_max_word",                "search_analyzer": "ik_max_word"         },        "content":{            "type": "text",             "analyzer": "ik_max_word",                "search_analyzer": "ik_max_word"        },        "create_date": {          "type": "date",          "format": "EEE MMM dd HH:mm:ss Z YYYY"        }      }    }  }}PUT ik_testPOST ik_test/type1/{    "title": "人民银行",    "name":"7月金融数据传政策暖意",    "content":"社会融资规模增速等数据也传递出这样的信息"}POST ik_test/type1/_search?pretty=true{    "query": {"match": {       "title": "人民"    }}}

 

转载于:https://www.cnblogs.com/DennyZhao/p/9442853.html

你可能感兴趣的文章
html基础
查看>>
Java troubleshooting guide
查看>>
iOS 实现不定参数方法
查看>>
电脑快捷键
查看>>
十五周 苏浪浪 201771010120
查看>>
Win10 Anaconda下TensorFlow-GPU环境搭建详细教程(包含CUDA+cuDNN安装过程)(转载)...
查看>>
String常用方法总结
查看>>
判断浏览器的版本
查看>>
ASM:《X86汇编语言-从实模式到保护模式》第10章:32位x86处理器的编程架构
查看>>
Scalable IO in Java
查看>>
ajax 请求如何解决乱码
查看>>
类库探源——System.Exception
查看>>
[转自脚本之家] Javascript cookie 详解
查看>>
linux压缩和解压缩类命令|--zip/unzip指令
查看>>
约瑟夫问题
查看>>
地址空间和虚拟内存(转载)http://topic.csdn.net/u/20090619/10/4c62a13b-536b-4b0a-af09-2271c6a104e1.html...
查看>>
Unity3d 简单的小球沿贝塞尔曲线运动(适合场景漫游使用)
查看>>
Redis实现之数据库(三)
查看>>
【BZOJ】1070: [SCOI2007]修车
查看>>
mybatis的foreach的使用
查看>>