博主分享免费Java教学视频,B站账号:Java刘哥
ElasticSearch 默认的分词器对英文处理比较好,对中文的话,会将中文分成一个个字。
比如根据文章标题搜索,输入关键字"新中国万岁",会将这五字一个个展开,然后搜索标题中包含这几个字的,这会导致索引引擎的效果不明显。因为我们往往更希望,搜到“新中国”的排在前面,“万岁”的排在前面。而不是“新”排在前面。
采用默认的分析器 standard
结果如下
2.中文分词器
POST http://127.0.0.1:9200/_analyze?pretty=true
采用中文分词器
结果如下
可见效果提高了不少。
一、分词器对比
我们通过 PostMan 发送请求 1.默认的分词器 POST http://127.0.0.1:9200/_analyze?pretty=true- {
- "text":"新中国万岁"
- }
- {
- "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
- },
- {
- "token": "岁",
- "start_offset": 4,
- "end_offset": 5,
- "type": "<IDEOGRAPHIC>",
- "position": 4
- }
- ]
- }
- {
- "analyzer": "ik_max_word",
- "text":"新中国万岁"
- }
- {
- "tokens": [
- {
- "token": "新中国",
- "start_offset": 0,
- "end_offset": 3,
- "type": "CN_WORD",
- "position": 0
- },
- {
- "token": "中国",
- "start_offset": 1,
- "end_offset": 3,
- "type": "CN_WORD",
- "position": 1
- },
- {
- "token": "万岁",
- "start_offset": 3,
- "end_offset": 5,
- "type": "CN_WORD",
- "position": 2
- },
- {
- "token": "万",
- "start_offset": 3,
- "end_offset": 4,
- "type": "TYPE_CNUM",
- "position": 3
- },
- {
- "token": "岁",
- "start_offset": 4,
- "end_offset": 5,
- "type": "COUNT",
- "position": 4
- }
- ]
- }
二、安装中文分词器
我们的 ES 是6.5.0 版本的,对应下载分词器必须也是 6.5.0 版本 1.下载源码包 GitHub 地址:https://github.com/medcl/elasticsearch-analysis-ik 直接下载到本地 2.进入该目录 mvn clean package 打包完毕 在 target/releases 下有一个 elasticsearch-analysis-ik-6.5.0.zip 文件,将其复制,拷贝到 ES 目录下的 plugins 中 3.解压 elasticsearch-analysis-ik-6.5.0.zip,将其重命名为 ik 如图三、重启 ES,创建索引
重启 ElasticSearch 在终端输入 jps 可以查看 java 项目的pid,杀死 elasticsearch 的进程 启动 elasticsearch: bin/elasticsearch -d 创建带中文分词器的索引 PUT 127.0.0.1:9200/blog- {
- "settings": {
- "analysis" : {
- "analyzer" : {
- "ik" : {
- "tokenizer" : "ik_max_word"
- }
- }
- },
- "number_of_shards": 5,
- "number_of_replicas": 1
- },
- "mappings": {
- "post": {
- "properties": {
- "postId": {
- "type": "long"
- },
- "userId": {
- "type": "long"
- },
- "postTitle": {
- "type": "text",
- "analyzer": "ik_max_word"
- },
- "postSummary": {
- "type": "text",
- "analyzer": "ik_max_word"
- },
- "postThumbnail": {
- "type": "keyword"
- },
- "postType": {
- "type": "keyword"
- },
- "postStatus": {
- "type": "integer"
- },
- "postViews": {
- "type": "long"
- },
- "postLikes": {
- "type": "long"
- },
- "commentSize": {
- "type": "long"
- },
- "postDate": {
- "type": "date"
- }
- }
- }
- }
- }
- 微信
- 交流学习,有偿服务
- 博客/Java交流群
- 资源分享,问题解决,技术交流。群号:590480292
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏