对一个字符串类型的字段进行排序通常不准确,因为已经被分词成多个词条了
字段虽然是字符串,但是其实值是整数, 排序按照字符串转成整数排序
解决方式:对字段索引两次,一次索引分词(用于搜索),一次索引不分词(用于排序)
PUT projecy_zcy
{
"settings": {
"refresh_interval": "1s",
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword" ----不分词
},
"keylong": {
"type": "long" --- 整数,用于排序
}
}
},
"projectCode": {
"type": "text"
},
"projectName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"projectStatus": {
"type": "keyword"
}
}
}
}
期望按照字符串排序, 不分词
GET /projecy_zcy/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"id.keyword": {
"order": "desc"
}
}
]
}
文章来源:https://www.toymoban.com/news/detail-668188.html
期望按照数字排序
GET /projecy_zcy/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"id.keylong": {
"order": "desc"
}
}
]
}
文章来源地址https://www.toymoban.com/news/detail-668188.html
到了这里,关于ES:字符串排序,字符串按照数字排序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!