一、创建索引代码示例
-
创建索引代码示例
package com.xz.esdemo.day1; 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 java.io.IOException; /** * @description: 创建索引 * @author: xz */ public class EsIndexCreate { public static void main(String[] args) throws IOException { // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 创建索引 - 请求对象 CreateIndexRequest createIndexRequest = new CreateIndexRequest("role"); // 发送请求,获取响应 CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); // 响应状态 boolean acknowledged = createIndexResponse.isAcknowledged(); System.out.println("响应状态===="+acknowledged); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息
-
使用postman工具查看创建索引是否成功,如下图表示JavaAPI操作创建索引成功
二、查询索引代码示例
-
查询索引代码示例
package com.xz.esdemo.day1; 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.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexResponse; import java.io.IOException; /** * @description: 查询索引 * @author: xz */ public class EsIndexSearch { public static void main(String[] args) throws IOException { // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 查询索引 请求对象 GetIndexRequest request = new GetIndexRequest("role"); // 发送请求,获取响应 GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT); System.out.println("aliases:"+response.getAliases()); System.out.println("mappings:"+response.getMappings()); System.out.println("settings:"+response.getSettings()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息
二、删除索引代码示例
-
删除索引代码示例
package com.xz.esdemo.day1; 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 java.io.IOException; /** * @description: 删除索引 * @author: xz */ public class EsIndexDelete { public static void main(String[] args) throws IOException { // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 删除索引 请求对象 DeleteIndexRequest request = new DeleteIndexRequest("role"); // 发送请求,获取响应 AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println("acknowledged:"+delete.isAcknowledged()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息
-
再使用postman工具查看删除索引是否成功,如下图表示JavaAPI操作删除索引成功。