ElasticSearch-7.11.1 安装与学习 第三篇

本文详细介绍了如何在Elasticsearch 7.11.1中安装与使用IK分词器,包括设置索引、修改默认分词器、添加数据,并解决数据类型不匹配的问题。通过实例演示了如何为索引指定IK分词器并操作数据,适合初学者学习和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
}

}

  1. 给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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值