1. 主要代码
1.1 查询
@Override
public List<ProcessEsVo> queryBatch(String index, List<String> ids) {
Client client = elasticsearchTemplate.getClient();
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
TermsQueryBuilder termsQuery = QueryBuilders.termsQuery("_id", ids);
searchRequestBuilder.setQuery(termsQuery);
SearchResponse response = searchRequestBuilder.get();
SearchHits searchHits = response.getHits();
return getProcessHitList(searchHits);
}
public List<QesDocumentVo> getDocumentByIdList(List<String> docIdList) {
List<QesDocumentVo> qesDocumentVoList = Lists.newArrayList();
if (CollectionUtils.isEmpty(docIdList)) {
return qesDocumentVoList;
}
SearchRequestBuilder searchRequestBuilder = initBaseRequestBuilder();
searchRequestBuilder.setFrom(0).setSize(1000);
searchRequestBuilder.setQuery(QueryBuilders.termsQuery("_id", docIdList));
SearchResponse response = searchRequestBuilder.get();
SearchHits hits = response.getHits();
if (hits.getTotalHits() <= 0) {
return qesDocumentVoList;
}
return getHitList(hits);
}
1.2 删除
@Override
public void deleteByIndexAndSubjectId(String index, Long id) {
List<String> list = Arrays.asList("question", "quality_college", "definition", "bms");
if (!list.contains(index)) {
return;
}
EsEnum esEnum = EsEnum.getByIndexName(index);
elasticsearchTemplate.delete(index, esEnum.getType(), String.valueOf(id));
}
@Override
public void batchDelete(String index, String docType, List<String> ids) {
if (CollectionUtils.isEmpty(ids)) {
return;
}
Client client = elasticsearchTemplate.getClient();
BulkRequestBuilder bulkRequestBuilder = elasticsearchTemplate.getClient().prepareBulk();
for (String docId : ids) {
bulkRequestBuilder.add(client.prepareDelete(index, docType, docId));
}
BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
if (bulkResponse.hasFailures()) {
log.error(bulkResponse.buildFailureMessage());
}
}
@Override
public void deleteByQuery(String index, String docType, String field, List<String> value) {
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setIndex(index);
deleteQuery.setType(docType);
deleteQuery.setQuery(QueryBuilders.termsQuery(field, value));
elasticsearchTemplate.delete(deleteQuery);
}
1.3 修改
@SneakyThrows
@Override
@Transactional(rollbackFor = Exception.class)
public Integer updateFieldByLLCenter(Long id, Long viewCount) {
Client client = elasticsearchTemplate.getClient();
String index = "ll_center";
String type = "center";
JSONObject jsonObject = new JSONObject();
jsonObject.put("viewcount", viewCount + 1L);
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(index);
updateRequest.type(type);
updateRequest.id(id.toString());
updateRequest.doc(jsonBuilder().startObject().field("viewcount", viewCount + 1L).endObject());
RestStatus status = client.update(updateRequest).actionGet().status();
return status.getStatus();
}
2 相关代码
private List<ProcessEsVo> getProcessHitList(SearchHits hits) {
List<ProcessEsVo> list = Lists.newArrayList();
if (hits != null || hits.getTotalHits() != 0) {
for (SearchHit searchHit : hits) {
list.add(convertSearchHit2Vo(searchHit, ProcessEsVo.class));
}
}
return list;
}
private <T extends BaseESVo> T convertSearchHit2Vo(SearchHit searchHit, Class<T> clazz) {
JSONObject jsonObject = JSON.parseObject(searchHit.getSourceAsString());
jsonObject.put("id", searchHit.getId());
if (MapUtils.isNotEmpty(searchHit.getHighlightFields())) {
searchHit.getHighlightFields().forEach((k, v) -> {
String hight = "";
for (Text text : v.getFragments()) hight += text.string();
setJsonProperty(jsonObject, v.getName(), hight);
});
}
return JSONObject.toJavaObject(jsonObject, clazz);
}