先注入文章来源地址https://www.toymoban.com/news/detail-504259.html
@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://www.toymoban.com/news/detail-504259.html
到了这里,关于java实现 elasticsearch es 的单个条件查询和多个条件查询,根据id更新指定字段的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!