创建maven工程,主要是通过两个依赖:
5.2 索引 *** 作org.elasticsearch elasticsearch7.8.0 org.elasticsearch.client elasticsearch-rest-high-level-client7.8.0 org.apache.logging.log4j log4j-api2.8.2 org.apache.logging.log4j log4j-core2.8.2 com.fasterxml.jackson.core jackson-databind2.9.9 junit junit4.12
5.2.1 创建索引
首先需要使用RestHighLevelClient创建客户端,
RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) // 其中包括主机名,端口,请求方式 ); // 创建索引请求 CreateIndexRequest request = new CreateIndexRequest("user"); //user是索引的名 // 通过客户端创建索引并得到响应的请求 CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); //相应状态 boolean acknowledged = response.isAcknowledged(); System.out.println(acknowledged); restHighLevelClient.close();
5.2.2 查询索引
查询索引使用的是GetIndexRequest,及GetIndexResponse.
// 创建索引 GetIndexRequest request = new GetIndexRequest("user"); GetIndexResponse response = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
通过postman查询的索引信息主要包括下面的内容,通过java的api的get方法得到响应的信息
{ "user": { "aliases": {}, //response.getAliases(); "mappings": {}, //response.getMappings(); "settings": { //response.getSettings(); "index": { "creation_date": "1650780419948", "number_of_shards": "1", "number_of_replicas": "1", "uuid": "dWGyIiiFTZ6gEtqfqJ-g_g", "version": { "created": "7080099" }, "provided_name": "user" } } } }
5.2.3 删除索引
删除索引使用DeleteIndexRequest,返回的可以是bool值表示是否删除成功。
DeleteIndexRequest request = new DeleteIndexRequest("user");
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
response.isAcknowledged();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)