Elastic Search 8.6.2客户端实战
Elastic Search和客户端交互,可以使用很多语言来完成搜索,如:Java、Python、Php和Go等多种语言。由于我平常使用最多的就是Java,这里仅基本于Java语言来演示。基于Java语言可以选择的客户端包括RestClient、Spring Data Elasticsearch。安装的话可以参考《Elastic Search 8.6.2集群安装部署》。
4.1使用RestClient【简单介绍】
参考文档:
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/introduction.html
创建项目Spring Boot Web项目 es-demo,配置项目pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 " target="_blank">https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<groupId>com.study</groupId> <artifactId>es-demo</artifactId> <version>0.0.1-SNAPSHOT</version>
<name>es-demo</name> <description>Elastic Search Demo project for Spring Boot</description>
<properties> <java.version>17</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>8.6.2</version> </dependency>
<dependency> <groupId>co.elastic.clients</groupId> <artifactId>elasticsearch-java</artifactId> <version>8.6.2</version> </dependency>
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>8.6.2</version> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins |