elasticsearch版本:7.17.3
目標(biāo):實(shí)現(xiàn)對(duì)類型為text字段的中文排序
一、用icu分詞器對(duì)中文排序
注意:
- 如果字段中既有中文又有英文,會(huì)先把中文按字母順序排序,再排英文
1、安裝icu分詞器
執(zhí)行下面命令后,重啟es即可
sudo bin/elasticsearch-plugin install analysis-icu
有下面圖中內(nèi)容代表安裝成功
2、創(chuàng)建索引時(shí)增加sort排序內(nèi)容
下面內(nèi)容是創(chuàng)建了一個(gè)名為es_test的索引內(nèi)容,其中包含名為fileName字段,以ik分詞器分詞,排序內(nèi)容以icu分詞器排序
PUT /es_test
{
"mappings":{
"properties":{
"fileName":{
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart",
"fields": {
"sort": {
"type": "icu_collation_keyword",
"index": false,
"language": "zh",
"country": "pinyin"
}
}
}
}
}
}
3、es命令方式排序
GET es_test/_search
{
"sort": [
{
"fileName.sort": {
"order": "desc"
}
}
]
}
4、java調(diào)用的方式排序
這里只寫(xiě)client.search中的內(nèi)容,要想看如何完整的調(diào)用,可以看我此系列的其他文章
SortOptions sortOption = SortOptions.of(_1 -> _1.field(_2 -> _2.field("fileName.sort").order(SortOrder.Desc)));
SearchResponse<Map> search = client.search(_1 -> _1
.index(indexName)
//es默認(rèn)返回10000條數(shù)據(jù),加上此條配置才能返回全部條數(shù)
.trackTotalHits(_2 -> _2.enabled(true))
//查詢參數(shù)
.query(queryBuilder.build()._toQuery())
.from(pageBegin)
.size(dto.getPageSize())
.sort(sortOption)
, Map.class);
二、用pinyin分詞器實(shí)現(xiàn)中文排序
注意
- pinyin對(duì)數(shù)字的排序可能不會(huì)太理想
- 由于fielddata會(huì)在內(nèi)存中加載并存儲(chǔ)每個(gè)文檔的字段數(shù)據(jù),以便在排序和聚合等操作中使用,此方式占用內(nèi)存要更大,尤其是啟用了fielddata選項(xiàng)時(shí)
- 可以將"fileName.pinyin"字段的數(shù)據(jù)類型更改為"keyword"類型,就可以直接對(duì)字段進(jìn)行聚合和排序,而無(wú)需使用fielddata(需要注意keyword類型和text類型的區(qū)別,根據(jù)自己需要確定)
1、安裝pinyin分詞器
執(zhí)行以下命令,根據(jù)自己的版本對(duì)應(yīng)的修改
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.17.3/elasticsearch-analysis-pinyin-7.17.3.zip
2、創(chuàng)建索引時(shí)增加sort相關(guān)內(nèi)容
下面內(nèi)容是創(chuàng)建了一個(gè)名為es_test的索引內(nèi)容,其中包含名為fileName字段,以ik分詞器分詞,相當(dāng)于還創(chuàng)建了一個(gè)名為pinyin的字段,用pinyin分詞器支持排序功能
PUT /es_test
{
"settings": {
"analysis": {
"analyzer": {
"ik_max_word": {
"tokenizer": "ik_max_word"
},
"ik_smart": {
"tokenizer": "ik_smart"
},
"pinyin_analyzer": {
"tokenizer": "my_pinyin"
}
},
"tokenizer": {
"my_pinyin": {
"type": "pinyin",
"keep_separate_first_letter": false,
"keep_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"lowercase": true,
"remove_duplicated_term": true,
"trim_whitespace": true
}
}
}
},
"mappings":{
"properties":{
"fileName":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart",
"fields": {
"pinyin": {
"type": "text",
"analyzer": "pinyin_analyzer",
"fielddata":true
}
}
}
}
}
}
3、es命令方式排序
POST es_test/_search
{
"sort": [
{
"fileName.pinyin": {
"order": "asc"
}
}
]
}
4、java調(diào)用的方式排序
這里只寫(xiě)client.search中的內(nèi)容,要想看如何完整的調(diào)用,可以看我此系列的其他文章文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-799101.html
SortOptions sortOption = SortOptions.of(_1 -> _1.field(_2 -> _2.field("fileName.pinyin").order(SortOrder.Desc)));
SearchResponse<Map> search = client.search(_1 -> _1
.index(indexName)
//es默認(rèn)返回10000條數(shù)據(jù),加上此條配置才能返回全部條數(shù)
.trackTotalHits(_2 -> _2.enabled(true))
//查詢參數(shù)
.query(queryBuilder.build()._toQuery())
.from(pageBegin)
.size(dto.getPageSize())
.sort(sortOption)
, Map.class);
尾聲
還有其他的支持中文排序的實(shí)現(xiàn)方式,比如用smartcn分詞器或者jieba分詞器,具體大家可以自己多多嘗試嘗試文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-799101.html
到了這里,關(guān)于elasticsearch7.17.3實(shí)現(xiàn)對(duì)中文排序的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!