新建Maven工程
添加依赖:
<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>
<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>
客户端连接:
public class ESTest_Client {
public static void main(String[] args) throws IOException {
// 创建客户端对象,高级别客户端,你需要连接服务器,需要知道ip端口号,访问方式
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//业务逻辑
System.out.println(client);
// 关闭客户端连接
client.close();
}
}
4.2 索引操作
4.2.1 创建索引
public static void main(String[] args) throws IOException {
// 创建客户端对象,高级别客户端,你需要连接服务器,需要知道ip端口号,访问方式
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//业务逻辑
System.out.println(client);
// 创建索引,先拿到索引,然后创建,第一个参数是请求对象,后面是选项
CreateIndexRequest request = new CreateIndexRequest("user");//索引名称
// 发完请求,就会有响应
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);// 默认请求配置
// 响应的状态
boolean acknowledged = response.isAcknowledged();
//输出true
System.out.println(acknowledged);
// 关闭客户端连接
client.close();
}
查询所有索引发现索引创建成功!
4.2.2 索引查询
public class ESTest_Index_Search {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 查询索引
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
// 别名操作
System.out.println(response.getAliases());
// 结构
System.out.println(response.getMappings());
client.close();
}
}
输出:
{user=[]}
{user=org.elasticsearch.cluster.metadata.MappingMetadata@e2704661}
4.2.3 索引删除
public class DeleteIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 删除索引 - 请求对象
DeleteIndexRequest request = new DeleteIndexRequest("user");
// 发送请求,获取响应
AcknowledgedResponse response = client.indices().delete(request,RequestOptions.DEFAULT);
// 操作结果
System.out.println("操作结果 : " + response.isAcknowledged());
client.close();
}
}
输出true:
4.3 文档操作
4.3.1 新建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private String sex;
private Integer age;
}
4.3.2 插入数据
public class ESTest_Doc_Insert {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 插入数据,这里需要的是index
IndexRequest indexRequest = new IndexRequest();
// 索引名字,索引的id
indexRequest.index("user").id("1001");
User user = new User();
user.setName("zhangsan");
user.setAge(30);
user.setSex("男");
// 向ES插入数据,必须将数据转换为json格式
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(user);
// 放到请求体中
indexRequest.source(value, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
//输出:CREATED
System.out.println(response.getResult());
client.close();
}
}
postman获取文档,发现其已经创建成功!
4.3.3 修改数据
public class ESTest_Doc_Update {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//修改数据
UpdateRequest request=new UpdateRequest();
request.index("user").id("1001");
request.doc(XContentType.JSON,"sex","女");
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
//输出:UPDATED
System.out.println(response.getResult());
client.close();
}
}
4.3.4 查询数据
/**
* 查询数据
*/
public class ESTest_Doc_Query {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
GetRequest request=new GetRequest().index("user").id("1001");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
//打印信息
System.out.println("_index:" + response.getIndex());
System.out.println("_type:" + response.getType());
System.out.println("_id:" + response.getId());
System.out.println("source:" + response.getSourceAsString());
}
}
输出:
_index:user
_type:_doc
_id:1001
source:{"name":"zhangsan","sex":"女","age":30}
4.3.5 数据删除
/**
* 删除数据
*/
public class ESTest_Doc_Delete {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//创建请求对象
DeleteRequest request = new DeleteRequest().index("user").id("1001");
//客户端发送请求,获取响应对象
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
//打印信息
System.out.println(response.toString());
}
}
输出:
DeleteResponse[index=user,type=_doc,id=1001,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]