赞
踩
先注入
- @Autowired
- private RestHighLevelClient restHighLevelClient;
- 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;
- }
- 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();
- 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();
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。