你不会是程序猿吧?

数值的keyword字段排序

匿名 | 发布于2019年10月10日 | 阅读数:2452

请教java怎么对es数值的keyword字段排序
已邀请:

trycatchfinal

赞同来自: xiaohuang

{
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"tag": {
"type": "keyword",
"fields": {
"num": {
"type": "long"
}
}
}
}
}
}
}
可以附加一个field,使用数字类型排序使用这个字段
 
"sort": [
{
"tag.num": {
"order": "desc"
}
}
]

doom

赞同来自:

不是很明白你的意思,是keyword类型的字段,值是数字吗?keyword是字符串类型,肯定是按照字符串类型排序,asc排序,像'2'就排序‘199’后面,这与数字类型的排序是不同的
匿名用户

匿名用户

赞同来自:

是的,是keyword类型的字段,值是数字。直接sort会出现你说的那样,我想实现按值排序,是不是script脚本可以实现?

doom

赞同来自:

给你一个案例
PUT mmm
{
"settings": {
"number_of_replicas": 0,
"number_of_shards": 1
},
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"tag":{
"type": "keyword"
}
}
}
}
}
PUT mmm/_doc/_bulk
{"index":{"_id":1}}
{"name":"jack","tag":"2"}
{"index":{"_id":2}}
{"name":"jack","tag":"19"}
{"index":{"_id":3}}
{"name":"jack","tag":"129"}

GET mmm/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"tag": {
"order": "asc"
}
}
]
}
GET mmm/_search
{
"query": {
"match_all": {}
},
"sort": {
"_script": {
"script": {
"lang": "painless",
"source": "Integer.parseInt(doc['tag'].value)"
},
"type": "number",
"order": "asc"
}
}
}

要回复问题请先登录注册