elasticsearch-rest-high-level-client api
导入依赖
<properties>
<elasticsearch.version>6.2.2elasticsearch.version>
properties>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
<version>${elasticsearch.version}version>
dependency>
在配置文件中添加es配置
# 设置超时时间默认1s
#spring.elasticsearch.rest.connection-timeout
# 设置连接用户
#spring.elasticsearch.rest.username
# 设置连接密码
#spring.elasticsearch.rest.password
# 设置es读取数据的超时时间默认30s
#spring.elasticsearch.rest.read-timeout
# 设置es地址多个以,分隔
spring.elasticsearch.rest.uris=127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
// 初始化查询器 并制定 es 索引
SearchRequest request = new SearchRequest()
.indices(EsConsUtils.INDEX).types(EsConsUtils.INDEX_DOC);
// 初始化条件构造器
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 指定返回 0 条记录
sourceBuilder.size(0);
// 定义普通查询
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("prId", 1688));
// 定义聚合查询
ValueCountAggregationBuilder terms_city = AggregationBuilders.count("count_city").field("id");
// 添加查询条件
sourceBuilder.query(boolQuery);
sourceBuilder.aggregation(terms_city);
request.source(sourceBuilder);
// 发起请求并获取返回值
try {
SearchResponse response = restHighLevelClient.search(request);
ValueCount count_city = response.getAggregations().get("count_city");
return count_city.getValue();
} catch (IOException e) {
e.printStackTrace();
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.size(0);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("proId", "520000"));
sourceBuilder.query(boolQuery);
TermsAggregationBuilder termsCity = AggregationBuilders.terms("terms_city").field("city.keyword");
sourceBuilder.aggregation(termsCity);
SearchRequest request = new SearchRequest().indices(EsConsUtils.INDEX).types(EsConsUtils.INDEX_DOC);
request.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(request);
ParsedStringTerms terms = response.getAggregations().get("terms_city");
JSONObject result = new JSONObject();
for (Terms.Bucket bucket : terms.getBuckets()) {
result.put(bucket.getKeyAsString(),bucket.getDocCount());
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("provinceId", 410000));
sourceBuilder.query(boolQuery);
request.source(sourceBuilder);
List<SysLogES> esList = new ArrayList();
SearchResponse response = restHighLevelClient.search(request);
Arrays.stream(response.getHits().getHits())
.forEach(i -> {
String sourceAsString = i.getSourceAsString();
JSONObject resJson = (JSONObject) JSONObject.parse(sourceAsString);
SysLogES logES = resJson.toJavaObject(SysLogES.class);
esList.add(logES);
});
System.out.println(JSON.toJSONString(esList));
IndexRequest request = new IndexRequest(EsConsUtils.INDEX, EsConsUtils.INDEX_DOC, "10010");
String jsonStr = "{id:10010,name:zs}";
request.source(jsonStr, XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
String resIndex = indexResponse.getIndex();
String resType = indexResponse.getType();
String resId = indexResponse.getId();
long resVersion = indexResponse.getVersion();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
System.out.println("新增文档成功!" + resIndex + resType + resId + resVersion);
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("修改文档成功!");
}
BulkRequest request = new BulkRequest();
List<RestClientTest> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new RestClientTest(i, "hashVal00" + i, "zs" + i));
}
for (RestClientTest rest : list) {
request.add(new IndexRequest(EsConsUtils.INDEX,
EsConsUtils.INDEX_DOC,
String.valueOf(rest.getId()))
.source(JSON.toJSONString(rest), XContentType.JSON));
}
BulkResponse bulkResponse = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
for (BulkItemResponse bulkItemResponse : bulkResponse) {
DocWriteResponse itemResponse = bulkItemResponse.getResponse();
if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
|| bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
IndexResponse indexResponse = (IndexResponse) itemResponse;
//TODO 新增成功的处理
System.out.println("新增成功,{}" + indexResponse.toString());
} else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
UpdateResponse updateResponse = (UpdateResponse) itemResponse;
//TODO 修改成功的处理
System.out.println("修改成功,{}" + updateResponse.toString());
} else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
//TODO 删除成功的处理
System.out.println("删除成功,{}" + deleteResponse.toString());
}
}
DeleteRequest deleteRequest = new DeleteRequest(EsConsUtils.INDEX, EsConsUtils.INDEX_DOC, "10086");
DeleteResponse response = null;
try {
response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(response);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name.keyword", "zhangshan");
sourceBuilder.query(termQueryBuilder);
SearchRequest searchRequest = new SearchRequest(EsConsUtils.INDEX);
searchRequest.types(EsConsUtils.INDEX_DOC);
searchRequest.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
List<String> docIds = new ArrayList<>(hits.getHits().length);
for (SearchHit hit : hits) {
docIds.add(hit.getId());
}
BulkRequest bulkRequest = new BulkRequest();
for (String id : docIds) {
DeleteRequest deleteRequest = new DeleteRequest(EsConsUtils.INDEX, EsConsUtils.INDEX_DOC, id);
bulkRequest.add(deleteRequest);
}
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
对象 | 操作 |
---|---|
IndexRequest | 新增 |
DeleteRequest | 删除 |
SearchRequest | 查询 |
UpdateRequest | 修改 |
@Data
public class ChineseDateFormat extends SimpleDateFormat {
private static final long serialVersionUID = 6276491293753307748L;
public ChineseDateFormat() throws Exception{
super("y-MM-dd HH:mm:ss", Locale.CHINESE);
setTimeZone(TimeZone.getTimeZone("GMT+8"));
}
}
spring:
jackson:
date-format: com.config.ChineseDateFormat
System.setProperty("es.set.netty.runtime.available.processors", "false");
@Configuration
public class ElasticsearchConfig {
@PostConstruct
void init() {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
}