好的,開篇就是個引用,朋友問我說Restful是啥?
我回我前面不是有貼連接了?這懶人居然連找都懶的找...
這邊簡單說說吧
恩恩,既然大家都說增刪改查(CRUD)
那麼就一個個來吧
今天來把上次建的INDEX(it_help)所有資料拉出來
@GetMapping
public List<UserInfo> findAll() throws Exception {
return userInfoService.findAll();
}
public List<UserInfo> findAll() throws IOException {
return userInfoDAO.findAll();
}
public List<UserInfo> findAll() throws IOException {
//建立搜尋請求
SearchRequest searchRequest = buildSearchRequest(INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//es封裝查詢語句
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
return getSearchResult(client.search(searchRequest, RequestOptions.DEFAULT));
}
private SearchRequest buildSearchRequest(String index) {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(index);
return searchRequest;
}
/**
* 將查詢返回勞資要轉變爲實體後回傳
*/
private List<UserInfo> getSearchResult(SearchResponse response) {
SearchHit[] searchHit = response.getHits().getHits();
List<UserInfo> userInfoList = new ArrayList<>();
for (SearchHit hit : searchHit) {
userInfoList
.add(objectMapper
.convertValue(hit
.getSourceAsMap(), UserInfo.class));
}
return userInfoList;
}
那麼上次再寫完文章後偷偷塞了幾筆資料來執行看看吧,看起來一摸一樣呢~ (豈不廢話??)
好咧,明天來寫寫怎麽依照name辣個欄位來搜尋記錄吧~
要準備開始正職務農了