ElasticSearch学习(四)—— 中文按拼音排序拼音检索
- 使用pinyin分词器
- 下载拼音分词器https://github.com/medcl/elasticsearch-analysis-pinyin
- 解压进入目录elasticsearch-analysis-pinyin
- 修改pom.xml中es版本为自己使用的版本
- 命令行mvn package 打包
- 进入elasticsearch-analysis-pinyin-master\target\releases解压elasticsearch-analysis-pinyin-7.7.0.zip文件
- 将解压后的文件拷贝到es安装目录下的 plugins/pinyin 中
- 重启es
- 修改报错的地方,重复步骤4567
- 索引设置
PUT /book
{"settings": {"index": {"analysis": {"analyzer": {"pinyin_analyzer": {"tokenizer": "my_pinyin"}}, "tokenizer": {"my_pinyin": {"type": "pinyin", "keep_none_chinese": false,"keep_full_pinyin": false,"keep_joined_full_pinyin": true,"keep_none_chinese_in_joined_full_pinyin": true,"keep_first_letter": false,"keep_none_chinese_in_first_letter": false,"none_chinese_pinyin_tokenize": false}}}}}
}
'keep_none_chinese' => false, // 对非中文不拆分词
'keep_full_pinyin' => false, // 关闭: 刘德华 -> liu, de, hua
'keep_joined_full_pinyin' => true, // 刘德华 -> liudehua
'keep_none_chinese_in_joined_full_pinyin' => true, // 刘德华2016 -> liudehua2016
'keep_first_letter' => true, // 刘德华 -> ldh
'keep_none_chinese_in_first_letter' => true, // 刘德华2016 -> ldh2016
'none_chinese_pinyin_tokenize' => false, // 没有卵用
keep_separate_first_letter :将字母分割,例如:刘德华> l,d,h,default:false。
keep_full_pinyin :包含全拼拼音,例如:刘德华> [ liu,de,hua],default:true。
limit_first_letter_length :设置first_letter结果的最大长度,default:16。
lowercase :小写非中文字母,default:true。
keep_none_chinese : 不在结果中保留非中文字母或数字,default:true。
- 设置字段
POST /book/_mapping
{"properties": {"title": {"type": "text", "fields": { "keyword": { "type": "keyword","ignore_above": 256},"sort": {"type": "text","analyzer": "pinyin_analyzer"}}},"author": { "type": "text", "fields": { "keyword": { "type": "keyword","ignore_above": 256},"sort": {"type": "text","analyzer": "pinyin_analyzer"}}}}
}
注意:Only text fields support the analyzer mapping parameter.只有text可以设置分词器
- 检索
GET /book/_search
{"query": {"match": {"title": "测试"}},"from": 0, "size": 20,"sort": {"title.sort" : "asc"}
}
- 拼音检索
- 安装ik分词器步骤https://github.com/medcl/elasticsearch-analysis-ik同拼音分词器
- 安装完成需要重启es
- 索引设置
PUT /book
{"settings": {"index": {"analysis": {"analyzer": {"ik_smart_pinyin": {"type": "custom","tokenizer": "ik_smart","filter": "my_pinyin_filter"},"ik_max_word_pinyin": {"type": "custom","tokenizer": "ik_max_word","filter": "my_pinyin_filter"},"pinyin_analyzer": {"tokenizer": "my_pinyin_tokenizer"}}, "tokenizer": {"my_pinyin_tokenizer": {"type": "pinyin", "keep_first_letter": false, "keep_full_pinyin": false, "keep_joined_full_pinyin": true, "keep_none_chinese_in_first_letter": true, "none_chinese_pinyin_tokenize": false, "lowercase": true, "with_tone_number": true}},"filter": {"my_pinyin_filter": {"type": "pinyin", "keep_first_letter": false, "keep_full_pinyin": false, "keep_joined_full_pinyin": true, "keep_none_chinese_in_first_letter": true, "none_chinese_pinyin_tokenize": false, "lowercase": true, "with_tone_number": true}}}}}
}
- 字段设置
POST /ancientbook/_mapping
{"properties": {"title": { "type": "text", "analyzer": "ik_max_word_pinyin", "fields": { "keyword": { "type": "keyword","ignore_above": 256}}},"author": { "type": "text", "analyzer": "ik_max_word_pinyin", "fields": { "keyword": { "type": "keyword","ignore_above": 256}}}}
}
该配置可以实现中文/拼音检索,但无法按拼音排序。
- 使用icu分词器
- 安装插件
- es安装目录下查看插件./bin/elasticsearch-plugin list
- es安装目录下./bin/elasticsearch-plugin install analysis-icu
- 重启es
- 索引设置
- 安装插件
PUT /bookPOST /book/_mapping
{"properties": {"title": { "type": "text", "analyzer": "icu_analyzer", "fields": { "keyword": { "type": "keyword","ignore_above": 256},"sort": { "type": "icu_collation_keyword","index": false,"language": "zh","country": "CN"}}},"author": { "type": "text", "analyzer": "icu_analyzer", "fields": { "keyword": { "type": "keyword","ignore_above": 256},"sort": { "type": "icu_collation_keyword","index": false,"language": "zh","country": "CN"}}}}
}
- 检索
GET /book/_search
{"query": {"bool": {"must": [{"match_all": {}}]}}, "from": 0, "size": 10, "sort": [{"title.sort": "asc"}]
}
- 同时需要分词检索和按拼音排序使用的是icu分词器
1.es安装目录下查看插件./bin/elasticsearch-plugin list
2.es安装目录下./bin/elasticsearch-plugin install analysis-icu
3.重启es
ElasticSearch学习(四)—— 中文按拼音排序拼音检索相关推荐
- Elasticsearch学习(四) - 查询①
title: Elasticsearch学习(四)-查询① date: 2020-10-29 tags: Elasticsearch Elasticsearch学习(四)-查询① categories ...
- ElasticSearch学习(四)——Linux 单节点部署
文章名称 地址 ElasticSearch学习(一)--概述 前往 ElasticSearch学习(二)--索引.文档简单操作 前往 ElasticSearch学习(三)--Windows 集群部署 ...
- java 汉字拼音排序_Java汉字排序(2)按拼音排序
1.前言 对于包含汉字的字符串来说,排序的方式主要有两种: 一种是拼音,一种是笔画. 本文就讲述如何实现按拼音排序的比较器(Comparator). 作者:Jeff 发表于:2007年12月21日 1 ...
- ElasticSearch学习(三)——Windows集群部署
文章名称 地址 ElasticSearch学习(一)--概述 前往 ElasticSearch学习(二)--索引.文档简单操作 前往 ElasticSearch学习(三)--Windows 集群部署 ...
- ElasticSearch学习笔记(8)· ES集群的搭建
目录 十三.集群的实现 1.相关概念 集群(cluster) 节点(node) 分配和复制(shards & replicas) 2.快速搭建集群 3.安装head插件 十三.集群的实现 1. ...
- ElasticSearch 2.4.X实现中文拼音排序
前言 最近接到一个需求,要求实现搜索框的搜索结果可以按照中文排序,本人灵机一动,那不很简单吗,直接按照es自带的sort功能处理下不就行了吗?两分钟的代码量,半天的喝茶时间,白赚半天的故事点,嘿嘿. ...
- ElasticSearch学习笔记-ngram、中文拼音、简繁体搜索记录
ElasticSearch版本:elasticsearch-7.3.0 ElasticSearch相关插件安装可以参考: ElasticSearch学习笔记-插件安装记录_人生偌只如初见的博客-CSD ...
- iOS--中文排序、全拼音排序
本文示例Demo在GitHub:https://github.com/gaussli/ChineseSortDemo 在做项目的时候,总遇到排序问题,英文排序是很简单的,直接使用compare方法就可 ...
- mysql root dengru_Mysql学习Mysql中文汉字转拼音的实现(每个汉字转换全拼)
<Mysql学习Mysql中文汉字转拼音的实现(每个汉字转换全拼)>要点: 本文介绍了Mysql学习Mysql中文汉字转拼音的实现(每个汉字转换全拼),希望对您有用.如果有疑问,可以联系我 ...
最新文章
- Android 定制RadioButton样式
- 油气储运工程中计算机的应用,中国石油大学(北京) 油气储运工程专业介绍
- 启动elasticsearch命令_快速安装ElasticSearch
- c语言写贪吃蛇什么水平_C语言实现贪吃蛇游戏(VC6.0)
- asp.net mvc的初学
- python中的doc_在windows 10上读取python中的.doc文件
- YbtOJ#20081-[NOIP2020模拟赛B组Day8]树上排列【组合数,树形dp】
- 聪明的质监员 2011年NOIP全国联赛提高组(二分+前缀和)
- internal java compiler error_Java异常处理总结
- Mr.J-- jQuery学习笔记(十三)--选项Tab卡
- main run方法没用_多线程:解决Runnable接口无start()方法的问题
- python3-字符串常用操作
- unity的函数生命周期
- 【树莓派】树莓派系统安装
- 血淋淋的BUG:波音在软件开发上错在哪里?
- 云班课蓝墨云网页版资源如何下载
- mysql产品分类_MySQL习题1 一对多实例 产品和分类
- ios屏幕尺寸和分辨率
- 遥感建筑物提取数据集
- STM32F030F4P6 HAL库 PPM信号输出 24L01 PPM接收机制作