SpringBoot -ElasticSearch RestHighLevelClient 高階客戶端使用(3) 文件操作
阿新 • • 發佈:2018-12-25
前一章講過如何將SpringBoot -ElasticSearch的高階客戶端RestHighLevelClient 的Index操作
這一章是使用RestHighLevelClient 進行document操作
之前說過es7.X會移除type,所以以下操作中 index和type保持一致,為後續升級提供便利
插入
public Integer documentInsert(String indexName, Map<String, Object> data) {
IndexRequest indexRequest = new IndexRequest (indexName, indexName);
indexRequest.source(JSON.toJSONString(data), XContentType.JSON);
try {
rhlClient.index(indexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
更新
public Integer documentUpdate (String indexName, Map<String, Object> data, String id) {
UpdateRequest updateRequest = new UpdateRequest(indexName, indexName, id);
updateRequest.doc(data);
try {
rhlClient.update(updateRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e. printStackTrace();
}
return 1;
}
刪除
public Integer documentDelete(String indexName, String id) {
SysTable table = tableMapper.select(tableId);
DeleteRequest deleteRequest = new DeleteRequest(indexName, indexName, id);
try {
rhlClient.delete(deleteRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
查詢
public List<Map<String, Object>> documentQuery(String indexName, Map<String, Object> query) {
List<Map<String, Object>> result = Lists.newArrayList();
SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.types(indexName);
queryBuilder(null, null, query, indexName, searchRequest);
try {
SearchResponse response = rhlClient.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits().getHits()) {
Map<String, Object> map = hit.getSourceAsMap();
map.put("id", hit.getId());
result.add(map);
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private void queryBuilder(Integer pageIndex, Integer pageSize, Map<String, Object> query,
String indexName, SearchRequest searchRequest) {
if (query != null && !query.keySet().isEmpty()) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
if (pageIndex != null && pageSize != null) {
searchSourceBuilder.size(pageSize);
if (pageIndex <= 0) {
pageIndex = 0;
}
searchSourceBuilder.from((pageIndex - 1) * pageSize);
}
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
query.keySet().forEach(key -> {
ESDataTypeEnum type = ESDataTypeEnum.TEXT;
boolBuilder.must(QueryBuilders.matchQuery(key, query.get(key)));
});
searchSourceBuilder.query(boolBuilder);
searchRequest.source(searchSourceBuilder);
}
}
分頁查詢
public PageInfo<Map<String, Object>> documentPage(Integer pageIndex, Integer pageSize, String indexName, Map<String, Object> data) {
PageInfo<Map<String, Object>> pageInfo = new PageInfo<>();
List<Map<String, Object>> result = Lists.newArrayList();
SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.types(indexName);
queryBuilder(pageIndex, pageSize, data, indexName, searchRequest);
try {
SearchResponse response = rhlClient.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits().getHits()) {
Map<String, Object> map = hit.getSourceAsMap();
map.put("id", hit.getId());
result.add(map);
}
pageInfo.setPageSize(pageSize);
pageInfo.setPageNum(pageIndex);
pageInfo.setTotal(response.getHits().getTotalHits());
pageInfo.setList(result);
} catch (IOException e) {
e.printStackTrace();
}
return pageInfo;
}
批量插入
public Integer documentBatchInsert(String indenName, List<Map<String, Object>> data) {
BulkRequest bulkRequest = new BulkRequest();
data.forEach(datum -> {
IndexRequest indexRequest = new IndexRequest(indenName, indenName);
indexRequest.source(JSON.toJSONString(datum), XContentType.JSON);
bulkRequest.add(indexRequest);
});
try {
rhlClient.bulk(bulkRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return data.size();
}