先注入
@Autowired
private RestHighLevelClient restHighLevelClient;
1:ES根据单个条件查询
public String searchEsAttachmentsLink(String riskId,String dataFrom){
try {
//添加条件,
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource()
.size(1)
.query(QueryBuilders.termQuery( name: "字段", 值));
//拼装
SearchRequest searchRequest = new SearchRequest()
.indices(索引)
.source(searchSourceBuilder);
//用ESClient执行search,得到响应结果
SearchResponse searchResponse =
restHighLevelClient.search(searchRequest,RequestOption.DEFAULT);
//根据自己返回的数据格式取值
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
//取值
hit.getSourceAsMap().get("字段");
}
return null;
}
2:ES根据多个条件查询
BoolOueryBuilder boolOueryBuilder = new BoolOueryBuilder();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//用must拼接where条件
boolQueryBuilder.must(QueryBuilders.termQuery("字段","值"))
.must(QueryBuilders.termQuery("字段","值");
SearchRequest searchRequest = new SearchRequest()
.indices(索引)
.source(searchSourceBuilder);
searchSourceBuilder.query(boolQueryBuilder);
log.info("拼接后的请求参数: ",searchRequest);
SearchResponse searchResponse = restHighLevelclient.search(searchRequest,RequestOptions .DEFAULT) ;
log.info("查询后的结果: ",searchResponse);
//取值
SearchHitl] hits = searchResponse.getHits() .getHits();
3:ES根据id,局部更新文档
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(索引);
updateRequest.type("_doc");
updateRequest.id(id);
//添加where条件
updateRequest.doc(
XContentFactory.jsonBuilder(
.startobject()
.field( "字段", "值")
.field( "字段", "值")
.endobject()
);
//执行
String resultUpdate = restHighlevelClient.update(updateRequest, Request0ptions.DEFAULT).getResult().tostring();
本文转载自: https://blog.csdn.net/zbh1957282580/article/details/128476516
版权归原作者 清石小猿 所有, 如有侵权,请联系我们删除。
版权归原作者 清石小猿 所有, 如有侵权,请联系我们删除。