赞
踩
<!-- elasticsearch-rest-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.13.3</version>
</dependency>
<!-- elasticsearch-rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.13.3</version>
</dependency>
@Configuration
public class ESConfig {
@Bean
public RestHighLevelClient getRestHighLevelClient(){
HttpHost http1 = new HttpHost("127.0.0.1", 9200, "http");
HttpHost http2 = new HttpHost("127.0.0.1", 9201, "http");
RestClientBuilder restClientBuilder = RestClient.builder(http1, http2);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
return restHighLevelClient;
}
}
@Autowired
private RestHighLevelClient restHighLevelClient;
public void insertOrUpdateData() throws IOException { HashMap<String, String> map = new HashMap<>(); map.put("name", "dd df ff"); map.put("age", "16"); IndexRequest indexRequest = new IndexRequest("myindex"); //设置ID indexRequest.id("9"); //添加数据 indexRequest.source(map); restHighLevelClient.indexAsync(indexRequest, RequestOptions.DEFAULT, new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { RestStatus status = indexResponse.status(); System.out.println(status.name()); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { System.out.println("创建成功"); } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { ## 标题 System.out.println("更新成功"); } } @Override public void onFailure(Exception e) { System.out.println("出异常了"); } }); }
public void deleteData() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("myindex");
deleteRequest.id("1");
deleteRequest.timeout(TimeValue.timeValueMinutes(2));
DeleteResponse deleteResponse = restHighLevelClient.delete(
deleteRequest, RequestOptions.DEFAULT);
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
System.out.println("未找到需要删除的数据");
}
}
public void updateData() throws IOException { Map<String, Object> parameters = new HashMap<>(); parameters.put("count", 4); Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.field += params.count", parameters); UpdateRequest updateRequest = new UpdateRequest("myindex", "2"); updateRequest.script(script); UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); DocWriteResponse.Result result = updateResponse.getResult(); if (result == DocWriteResponse.Result.CREATED) { System.out.println("created"); } else if (result == DocWriteResponse.Result.UPDATED) { System.out.println("updated"); } else if (result == DocWriteResponse.Result.DELETED) { System.out.println("deleted"); } else if (result == DocWriteResponse.Result.NOOP) { System.out.println("noop"); } }
public void getOneData() throws IOException { String[] includes = Strings.EMPTY_ARRAY; String[] excludes = Strings.EMPTY_ARRAY; FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes); GetRequest getRequest = new GetRequest("myindex"); getRequest.id("9"); getRequest.fetchSourceContext(fetchSourceContext); boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT); if (exists) { System.out.println("有数据"); } GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); if (response.isExists()) { Map<String, Object> sourceAsMap = response.getSourceAsMap(); //获取索引名称 String index = response.getIndex(); //获取索引类型 String type = response.getType(); //获取索引id String id = response.getId(); System.out.println("index:" + index); System.out.println("type:" + type); System.out.println("id:" + id); System.out.println(sourceAsMap); } else { System.out.println("没有数据"); } }
public void multiQuery() throws IOException {
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add("myindex", "3");
multiGetRequest.add("myindex", "5");
MultiGetResponse multiGetResponse = restHighLevelClient
.mget(multiGetRequest, RequestOptions.DEFAULT);
MultiGetItemResponse[] responses = multiGetResponse.getResponses();
Stream.of(responses).forEach(response -> {
String id = response.getResponse().getId();
String source = response.getResponse().getSourceAsString();
System.out.println("id = " + id + " --> " + source);
});
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。