ES学习
第一篇:ElasticSearch 邂逅ES
第二篇:ElasticSearch入门
第三篇:ElasticSearch JavaAPI操作
第四篇:Windows单点和集群配置
第五篇:Linux单点和集群配置
第六篇:ElasticSearch核心概念
第七篇:ElasticSearch系统架构
第八篇:ElasticSearch分布式集群
第九篇:ElasticSearch路由计算
Part3:ElasticSearch JavaAPI操作
- ElasticSearch软件是由Java语言开发,可以通过JavaAPI的方式对ElasticSearch服务进行访问。
3.1 创建Maven项目
-
IDEA创建Maven项目,配置pom.xml依赖关系
<dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch客户端 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch依赖2.x的log4j --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <!--Jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> <!-- junit单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
3.2 客户端对象
-
早起版本的客户端对象已经不再推荐使用,且在未来版本中会被删除,这里采用高级REST客户端对象
-
早起的客户端对象,在api里面已经标识为不推荐使用了
-
-
创建客户端对象进行连接ES服务器
- 9200端口时ElasticSearch的Web通信端口,localhost为启动ES服务的主机名称
/** * @ClassName ESClient * @Description 通过JavaAPI连接ES服务器 * @Author Administrator * @Date 2021/5/18 14:12 **/ public class ESClient { public static void main(String[] args) { // 配置建立http连接的配置信息 HttpHost httpHost = new HttpHost("localhost", 9200, "http"); // 辅助构建RestClient RestClientBuilder builder = RestClient.builder(httpHost); // 创建ES高级客户端对象,封装了RestClient对象,并允许发起请求和读取响应 RestHighLevelClient client = new RestHighLevelClient(builder); // 关闭连接 try { client.close(); } catch (IOException e) { e.printStackTrace(); } } }
- 连接成功时,控制台不会报错,会自动运行结束
3.3 索引操作
- ES服务器正常启动之后,可以通过JavaAPI客户端对象对ES索引进行操作
3.3.1 创建索引
-
代码
- 使用CreateIndexRequest对象
/** * @ClassName ES_Index_Create * @Description 通过JavaAPI操作ES的索引的创建 * @Author Administrator * @Date 2021/5/18 14:11 **/ public class ES_Index_Create { private static HttpHost httpHost; private static RestClientBuilder builder; private static RestHighLevelClient client; static { // 配置建立http连接的配置信息 httpHost = new HttpHost("localhost", 9200, "http"); // 辅助构建RestClient builder = RestClient.builder(httpHost); // 创建ES高级客户端对象,封装了RestClient对象,并允许发起请求和读取响应 client = new RestHighLevelClient(builder); } public static void main(String[] args) { // 创建索引,这里使用的构造方法为指定索引名称的方式 CreateIndexRequest request = new CreateIndexRequest("user"); // 向ES发送请求,获取响应信息 CreateIndexResponse response = null; try { response = client.indices().create(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } boolean acknowledged = response.isAcknowledged(); // 输出响应状态 System.out.println("操作状态:"+acknowledged); // 关闭连接 try { client.close(); } catch (IOException e) { e.printStackTrace(); } } }
-
结果
3.3.2 查看索引
-
代码
- 使用GetIndexRequest对象
/** * @ClassName ES_Index_Query * @Description 通过JavaAPI操作ES的索引的查询 * @Author Administrator * @Date 2021/5/18 14:11 **/ public class ES_Index_Query { private static HttpHost httpHost; private static RestClientBuilder builder; private static RestHighLevelClient client; static { // 配置建立http连接的配置信息 httpHost = new HttpHost("localhost", 9200, "http"); // 辅助构建RestClient builder = RestClient.builder(httpHost); // 创建ES高级客户端对象,封装了RestClient对象,并允许发起请求和读取响应 client = new RestHighLevelClient(builder); } public static void main(String[] args) { // 查询索引,这里使用的构造方法为指定索引名称的方式 GetIndexRequest request = new GetIndexRequest("user"); // 向ES发送请求,获取响应信息 GetIndexResponse response = null; try { response = client.indices().get(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } System.out.println("Aliases:"+response.getAliases()); System.out.println("DataStreams:"+response.getDataStreams()); System.out.println("DefaultSettings:"+response.getDefaultSettings()); System.out.println("Indices:"+response.getIndices()); System.out.println("Mappings:"+response.getMappings()); System.out.println("Settings:"+response.getSettings()); // 关闭连接 try { client.close(); } catch (IOException e) { e.printStackTrace(); } } }
-
结果
-
这里可以使用Postman做一次查询,和控制台输出结果对应即可
3.3.3 删除索引
-
代码
- 使用DeleteIndexRequest对象
/** * @ClassName ES_Index_Delete * @Description 通过JavaAPI操作ES的索引的删除 * @Author Administrator * @Date 2021/5/18 14:11 **/ public class ES_Index_Delete { private static HttpHost httpHost; private static RestClientBuilder builder; private static RestHighLevelClient client; static { // 配置建立http连接的配置信息 httpHost = new HttpHost("localhost", 9200, "http"); // 辅助构建RestClient builder = RestClient.builder(httpHost); // 创建ES高级客户端对象,封装了RestClient对象,并允许发起请求和读取响应 client = new RestHighLevelClient(builder); } public static void main(String[] args) { // 查询索引,这里使用的构造方法为指定索引名称的方式 DeleteIndexRequest request = new DeleteIndexRequest("user"); // 向ES发送请求,获取响应信息 AcknowledgedResponse response = null; try { response = client.indices().delete(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } System.out.println("删除状态:"+response.isAcknowledged()); // 关闭连接 try { client.close(); } catch (IOException e) { e.printStackTrace(); } } }
-
结果
3.4 文档操作
-
这里开始对文档进行操作,我们一般都会给文档添加一些属性,这里在Java中将其定义为一个类,同时搭载一些类方法,便于操作
-
定义User类
/** * @ClassName User * @Description 用于文档操作的User实体类 * @Author Administrator * @Date 2021/5/18 16:33 **/ public class User { private String name; private String sex; private Integer age; public User(String name, String sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } public User() { } public String getName() { return name; } public