I wanted to get the last records whose sequenceStatus is "OB ORDER COMPLETE", so I used matchQuery and sort the timestamp with DESC
order. Then I return the first result.
After printing the first 10 results, I found the first result is not the last records as I expected. It is the first record. How can I get the last record? This is the code:
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();
}
The results:
{"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"} ......
Note: queryResultPrint(results) is code to print searchResponse.
I think you are missing relation between your searchSourceBuilder
and searchReq
. At least I can't see that in code fragment you've posted.
From my point of view you should have another line of code before getting the response:
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);
As I said I am new to ES, and can be wrong
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.