你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
输入关键字进行搜索
搜索:
没有找到相关结果
God_lockin
赞同来自: fantaigan 、laoyang360
POST test/_search { "sort": [ { "_script": { "script": { "source": """String id = doc['_id'].getValue(); if ("1".equals(id) || "2".equals(id)) { return Integer.parseInt(id) * System.nanoTime(); } else { return doc['time'].getValue().toInstant().toEpochMilli(); }""" }, "type": "number", "order": "desc" } } ] }
PUT test { "settings": { "index": { "number_of_shards": "1", "number_of_replicas": "0" } }, "mappings": { "properties": { "id": { "type": "keyword" }, "time": { "type": "date", "format": [ "yyyy-MM-dd HH:mm:ss" ] } } } }
POST test/_doc/1 { "name": "1", "time": "2020-02-02 09:00:00" } POST test/_doc/2 { "name": "2", "time": "2020-02-02 08:00:00" } POST test/_doc/3 { "name": "3", "time": "2020-02-02 05:00:00" } POST test/_doc/4 { "name": "4", "time": "2020-02-02 06:00:00" }
{ "took" : 11, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "test", "_type" : "_doc", "_id" : "2", "_score" : null, "_source" : { "name" : "2", "time" : "2020-02-02 08:00:00" }, "sort" : [ 3.985284816144E12 ] }, { "_index" : "test", "_type" : "_doc", "_id" : "1", "_score" : null, "_source" : { "name" : "1", "time" : "2020-02-02 09:00:00" }, "sort" : [ 1.992642329371E12 ] }, { "_index" : "test", "_type" : "_doc", "_id" : "4", "_score" : null, "_source" : { "name" : "4", "time" : "2020-02-02 06:00:00" }, "sort" : [ 1.5806232E12 ] }, { "_index" : "test", "_type" : "_doc", "_id" : "3", "_score" : null, "_source" : { "name" : "3", "time" : "2020-02-02 05:00:00" }, "sort" : [ 1.5806196E12 ] } ] } }
tacsklet - 公司有用到es
赞同来自:
caizhongao
xzorz
要回复问题请先登录或注册
JAVA
4 个回复
God_lockin
赞同来自: fantaigan 、laoyang360
先给答案,可以用这个script。
准备数据
Mapping
数据:
简化一下题目,id=1,2的按顺序返回,也可以逆序,看你自己的需求,其他的按时间倒序。
思路:把id=1,2的单独拿出来给个大参数(比如当前时间),然后其他的按时间倒排。
分别对应
Integer.parseInt(id) * System.nanoTime();
和
doc['time'].getValue().toInstant().toEpochMilli();
最终结果
tacsklet - 公司有用到es
赞同来自:
caizhongao
赞同来自:
xzorz
赞同来自: