package com.atgulgu.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
/**
* ClassName: ESTest_Client
* Package: com.atgulgu.es.test
*
* @Author 杨小燕
* @Create 2024/10/7 0007 23:37
* @Version 1.0
*/
public class ESTest_index_Create {
public static void main(String[] args) throws Exception {
//创建ES客户单
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.108",9200,"http"))
);
//创建索引
CreateIndexRequest request = new CreateIndexRequest("user");
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
//响应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("索引操作:" +acknowledged);
//关闭ES客户端
esClient.close();
}
}
上述是创建索引的操作
下面是使用代码查询索引信息
package com.atgulgu.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
/**
* ClassName: ESTest_Client
* Package: com.atgulgu.es.test
*
* @Author 杨小燕
* @Create 2024/10/7 0007 23:37
* @Version 1.0
*/
public class ESTest_index_Search {
public static void main(String[] args) throws Exception {
//创建ES客户单
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.108",9200,"http"))
);
//查询索引
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
//关闭ES客户端
esClient.close();
}
}
执行结果如下:
package com.atgulgu.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
/**
* ClassName: ESTest_Client
* Package: com.atgulgu.es.test
*
* @Author 杨小燕
* @Create 2024/10/7 0007 23:37
* @Version 1.0
*/
public class ESTest_index_Delete {
public static void main(String[] args) throws Exception {
//创建ES客户单
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.108",9200,"http"))
);
//查询索引
DeleteIndexRequest request = new DeleteIndexRequest("user");
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(response.isAcknowledged());
//关闭ES客户端
esClient.close();
}
}
删除索引的执行结果