ElasticSearch-7.11.1 安装与学习 第三篇
第八讲 IK分词器
1、脚本准备
请求方式:PUT
请求脚本:http://127.0.0.1:9200/mgx
请求命令:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": {
"type": "long",
"store":true
},
"title": {
"type": "text",
"store":true
},
"age": {
"type": "integer",
"store":true
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
2、修改分词器
1)已经创建的索引
2) 对应的索引信息如下
{
"version":6,
"mapping_version":2,
"settings_version":1,
"aliases_version":1,
"routing_num_shards":768,
"state":"open",
"settings":{
"index":{
"routing":{
"allocation":{
"include":{
"_tier_preference":"data_content"
}
}
},
"number_of_shards":"3",
"provided_name":"mgx",
"creation_date":"1615032495962",
"number_of_replicas":"1",
"uuid":"pD9uI3fJSJ-u8eUJt0oEDw",
"version":{
"created":"7110199"
}
}
},
"mappings":{
"_doc":{
"properties":{
"date":{
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"type":"date"
},
"settings":{
"properties":{
"analysis":{
"properties":{
"analyzer":{
"properties":{
"default":{
"properties":{
"type":{
"type":"text",
"fields":{
"keyword":{
"ignore_above":256,
"type":"keyword"
}
}
}
}
}
}
}
}
}
}
},
"id":{
"store":true,
"type":"long"
},
"title":{
"store":true,
"type":"text"
},
"age":{
"store":true,
"type":"integer"
}
}
}
},
"aliases":[
],
"primary_terms":{
"0":1,
"1":1,
"2":1
},
"in_sync_allocations":{
"0":[
"_kuga7Q3TWq4T5aZzVPU4g"
],
"1":[
"wtiu58iCQ5m1wTnj4L5YYg"
],
"2":[
"R4Rg8YtrQi-DSxoHODCqWQ"
]
},
"rollover_info":{
},
"system":false,
"timestamp_range":{
"unknown":true
}
}
- 给index指定IK分词器:
-
执行脚本截图
-
执行指定分词器脚本:
http://127.0.0.1:9200/mgx/_doc/pD9uI3fJSJ-u8eUJt0oEDw/
{
“settings”: {
“analysis”: {
“analyzer”: {“default”:{“type”:“ik_max_word”}}
}
}
} -
返回脚本截图:
{
“_index”: “mgx”,
“_type”: “_doc”,
“_id”: “pD9uI3fJSJ-u8eUJt0oEDw”,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 0,
“_primary_term”: 1
} -
成功截图:
3、给IK分词器添加数据
1)
请求方式:PUT
请求脚本:http://127.0.0.1:9200/mgx1
请求命令:
http://127.0.0.1:9200/mgx1
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"title": {
"type": "integer"
},
"age": {
"type": "integer"
}
}
}
}
2) 指定IK分词器
请求方式:POST
请求命令:
http://127.0.0.1:9200/mgx/_doc/3txwVdliRua0H8CyiR7JBw/
{
"settings": {
"analysis": {
"analyzer": {"default":{"type":"ik_max_word"}}
}
}
}
3)索引信息
{
"version":5,
"mapping_version":1,
"settings_version":1,
"aliases_version":1,
"routing_num_shards":768,
"state":"open",
"settings":{
"index":{
"routing":{
"allocation":{
"include":{
"_tier_preference":"data_content"
}
}
},
"number_of_shards":"3",
"provided_name":"mgx1",
"creation_date":"1615034156708",
"number_of_replicas":"2",
"uuid":"3txwVdliRua0H8CyiR7JBw",
"version":{
"created":"7110199"
}
}
},
"mappings":{
"_doc":{
"properties":{
"id":{
"type":"integer"
},
"title":{
"type":"integer"
},
"age":{
"type":"integer"
}
}
}
},
"aliases":[
],
"primary_terms":{
"0":1,
"1":1,
"2":1
},
"in_sync_allocations":{
"0":[
"9cQTHtAASGG0PeS86PcImw"
],
"1":[
"gPs8rfVRTr2LgB36ZOeW2g"
],
"2":[
"ZUvFv7HDQQ-S4RqTUs3YTQ"
]
},
"rollover_info":{
},
"system":false,
"timestamp_range":{
"unknown":true
}
}
4) 添加IK数据
请求方式: POST
请求命令:
http://127.0.0.1:9200/mgx1/_doc/1
{
"id":"1",
"title":"6",
"age":"1949"
}
执行效果:
注意提交请求的数据类型: 待解决问题
自己测试数据提交失败,问题原因是什么暂不清楚;好像是不是同一类型的数据不能提交。
{
“id”:“1”,
“title”:{
“中华人民共和国”,
“true”
},
“age”:“1949”
}
执行命令参考网址:
IK分词器: https://cloud.tencent.com/developer/article/1474928
其他的常见分词器和分词效果汇总:https://segmentfault.com/a/1190000011065897