我想获取sequenceStatus为“OB ORDER COMPLETE”的最后记录,因此我使用matchQuery并将时间戳与String[] includeFields = new String[] {"eventTimestamp"};
String[] excludeFields = new String[] {"logEvent","testId","orderId","marketId"};
searchSourceBuilder.query(QueryBuilders.matchQuery("sequenceStatus", "OB ORDER COMPLETE")).sort("eventTimestamp", SortOrder.DESC).fetchSource(includeFields,excludeFields);
searchSourceBuilder.size(20);
searchReq.source(searchSourceBuilder);
try {
SearchResponse searchResp=client.search(searchReq);
SearchHit[] results = searchResp.getHits().getHits();
queryResultPrint(results);
String eventTimestamp= searchResp.getHits().getAt(0).getSourceAsMap().get("eventTimestamp").toString();
return eventTimestamp;
} catch (IOException e) {
e.printStackTrace();
}
顺序排序。然后我返回第一个结果。
打印完前10个结果后,我发现第一个结果不是我预期的最后记录。这是第一张唱片。我如何获得最后一条记录?
这是代码:
{{1}}
结果:
{“eventTimestamp”:“2018-05-14 19:52:22.221184000”}
{“eventTimestamp”:“2018-05-14 19:52:22.221645000”}
{“eventTimestamp”:“2018-05-14 19:52:22.222064000”}
{“eventTimestamp”:“2018-05-14 19:52:22.222445000”}
{“eventTimestamp”:“2018-05-14 19:52:22.222880000”} ......
注意:queryResultPrint(results)是打印searchResponse的代码。
答案 0 :(得分:0)
我认为您错过了searchSourceBuilder
和searchReq
之间的关系。至少我无法在您发布的代码片段中看到。
从我的观点来看,在获得回复之前你应该有另一行代码:
searchSourceBuilder.query(QueryBuilders.matchQuery("sequenceStatus", "OB ORDER COMPLETE")).sort("eventTimestamp", SortOrder.DESC).fetchSource(includeFields,excludeFields);
/* binding source searchSourceBuilder to searchReq */
searchReq.source(searchSourceBuilder);
/* */
SearchResponse searchResp=client.search(searchReq);
正如我所说,我是ES的新手,可能是错的