因为es需要满足多个组的数据存储,所以字段类型没法确定,就用keyword和text作为通用类型保存所有数据,但是需要对某些字段排序,这些字段一般是数值,需要怎么做?
排序实例:
/index/type/_search?pretty
{
"from": 0,
"size": 20,
"query": {"match_all":{} },
"sort": {
"re_5": {
"order": "desc"
}
}
}
排序字段re_5是数值,例子:"1233",困扰是使用这个查询语句返回的不是按照re_5的数值降序排列的。
es版本6.1.1
排序实例:
/index/type/_search?pretty
{
"from": 0,
"size": 20,
"query": {"match_all":{} },
"sort": {
"re_5": {
"order": "desc"
}
}
}
排序字段re_5是数值,例子:"1233",困扰是使用这个查询语句返回的不是按照re_5的数值降序排列的。
es版本6.1.1
3 个回复
zpzkit
赞同来自:
{
"settings": {
"index": {
"number_of_shards": "5",
"number_of_replicas": "1"
}
},
"mappings": {
"type": {
"dynamic": false,
"_all": {
"enabled": false
},
"properties": {
"id": {
"type": "keyword"
},
"dt": {
"type": "date"
},
"page_id": {
"type": "keyword"
},
"task_name": {
"type": "keyword"
},
"re_1": {
"type": "keyword",
"fields": {
"text": {
"type": "text"
}
}
}
}
}
}
}
测试信息:
put data
1、{
"dt": "2018-06-01",
"id": 604459,
"page_id": 10,
"re_1": "8888888888"
}
2、{
"dt": "2018-06-01",
"es_id": "6fc95ea7825feb6f08713d981749c3c6",
"id": 604459,
"page_id": 10,
"re_1": "1000002475"
}
3、{
"dt": "2018-06-01",
"es_id": "6fc95ea7825feb6f08713d981749c3c6",
"id": 604459,
"page_id": 10,
"re_1": "3321"
}
4、{
"dt": "2018-06-01",
"es_id": "6fc95ea7825feb6f08713d981749c3c6",
"id": 604459,
"page_id": 10,
"re_1": "98"
}
5、{
"dt": "2018-06-01",
"es_id": "6fc95ea7825feb6f08713d981749c3c6",
"id": 604459,
"page_id": 10,
"re_1": "99864"
}
6、{
"dt": "2018-06-01",
"es_id": "6fc95ea7825feb6f08713d981749c3c6",
"id": 604459,
"page_id": 10,
"re_1": "99863"
}
7、{
"dt": "2018-06-01",
"es_id": "6fc95ea7825feb6f08713d981749c3c6",
"id": 604459,
"page_id": 10,
"re_1": "99865"
}
以上是创建的测试数据,
请求参数:
{
"from": 0,
"size": 20,
"query": {"match_all":{} },
"sort": {
"re_1": {
"order": "asc"
}}}
返回的顺序是乱的?并不是按照re_1的数值排序。我哪里出错了吗?求指点。
zpzkit
赞同来自:
mapping:
{
"settings": {
"index": {
"number_of_shards": "5",
"number_of_replicas": "1"
}
},
"mappings": {
"union": {
"properties": {
"post_date": { "type": "date" },
"user": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"age": { "type": "integer" }
}
}
}
}
数据:
{
"post_date": "2018-03-07",
"user":"jd-analysis",
"name":"3321",
"age":29
}
{
"post_date": "2018-03-07",
"user":"jd-analysis",
"name":"98",
"age":29
}
{
"post_date": "2018-03-07",
"user":"jd-analysis",
"name":"99865",
"age":29
}
请求参数:
{
"from": 0,
"size": 20,
"query": {"match_all":{} },
"sort": {
"name": {
"order": "asc"
}
}
}
返回依然是乱序的,猜测应该是对keyword类型的排序应该是不行的,最后想到的解决办法就是用script的方式将字段转换成number类型进行排序了,如果有好的办法的希望能不吝赐教,谢谢!
rochy - rochy_he
赞同来自:
使用脚本批量修改费时费力、而且很容易造成 ES 挂掉。