Elasticsearch学习笔记_巨輪的博客-CSDN博客

环境配置

创建一个maven环境,并添加如下依赖:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.elasticsearch</groupId>
  4. <artifactId>elasticsearch</artifactId>
  5. <version>7.8.0</version>
  6. </dependency>
  7. <!-- elasticsearch 的客户端 -->
  8. <dependency>
  9. <groupId>org.elasticsearch.client</groupId>
  10. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  11. <version>7.8.0</version>
  12. </dependency>
  13. <!-- elasticsearch 依赖 2.x 的 log4j -->
  14. <dependency>
  15. <groupId>org.apache.logging.log4j</groupId>
  16. <artifactId>log4j-api</artifactId>
  17. <version>2.8.2</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.apache.logging.log4j</groupId>
  21. <artifactId>log4j-core</artifactId>
  22. <version>2.8.2</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.fasterxml.jackson.core</groupId>
  26. <artifactId>jackson-databind</artifactId>
  27. <version>2.9.9</version>
  28. </dependency>
  29. <!-- junit 单元测试 -->
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. <version>4.12</version>
  34. </dependency>
  35. </dependencies>

依赖添加完成之后,连接本机的ES服务进行测试:

  1. import java.io.IOException;
  2. import org.apache.http.HttpHost;
  3. import org.elasticsearch.client.RestClient;
  4. import org.elasticsearch.client.RestHighLevelClient;
  5. public class HelloElasticsearch {
  6. public static void main(String[] args) throws IOException {
  7. // 创建客户端对象
  8. RestHighLevelClient client = new RestHighLevelClient(
  9. RestClient.builder(new HttpHost("localhost", 9200, "http")));
  10. // ...
  11. System.out.println(client);
  12. // 关闭客户端连接
  13. client.close();
  14. }
  15. }

上述代码能正常运行完毕,则环境配置成功,可以进行后续的操作

索引操作

增加

  1. public class CreateIndex {
  2. public static void main(String[] args) throws IOException {
  3. // 创建客户端对象
  4. RestHighLevelClient client = new RestHighLevelClient(
  5. RestClient.builder(new HttpHost("localhost", 9200, "http")));
  6. // 创建索引 - 请求对象
  7. CreateIndexRequest request = new CreateIndexRequest("user2");
  8. // 发送请求,获取响应
  9. CreateIndexResponse response = client.indices().create(request,
  10. RequestOptions.DEFAULT);
  11. boolean acknowledged = response.isAcknowledged();
  12. // 响应状态
  13. System.out.println("操作状态 = " + acknowledged);
  14. // 关闭客户端连接
  15. client.close();
  16. }
  17. }

查询&删除

查询

  1. public class SearchIndex {
  2. public static void main(String[] args) throws IOException {
  3. // 创建客户端对象
  4. RestHighLevelClient client = new RestHighLevelClient(
  5. RestClient.builder(new HttpHost("localhost", 9200, "http")));
  6. // 查询索引 - 请求对象
  7. GetIndexRequest request = new GetIndexRequest("user2");
  8. // 发送请求,获取响应
  9. GetIndexResponse response = client.indices().get(request,
  10. RequestOptions.DEFAULT);
  11. System.out.println("aliases:"+response.getAliases());
  12. System.out.println("mappings:"+response.getMappings());
  13. System.out.println("settings:"+response.getSettings());
  14. client.close();
  15. }
  16. }

删除

  1. public class DeleteIndex {
  2. public static void main(String[] args) throws IOException {
  3. RestHighLevelClient client = new RestHighLevelClient(
  4. RestClient.builder(new HttpHost("localhost", 9200, "http")));
  5. // 删除索引 - 请求对象
  6. DeleteIndexRequest request = new DeleteIndexRequest("user2");
  7. // 发送请求,获取响应
  8. AcknowledgedResponse response = client.indices().delete(request,RequestOptions.DEFAULT);
  9. // 操作结果
  10. System.out.println("操作结果 : " + response.isAcknowledged());
  11. client.close();
  12. }
  13. }
  • 根据上述的代码可以猜测,Java的ES客户端中相关的API,往往都是见名知意的,比如创建索引:CreateIndexRequest,删除索引:DeleteIndexRequest,不过这仅仅只有和索引相关的操作,继续往下看

文档操作

在后续的操作中,我们并不想每次都编写连接和关闭ES的操作代码,因此对上述代码进行重构

  1. import org.elasticsearch.client.RestHighLevelClient;
  2. public interface ElasticsearchTask {
  3. void doSomething(RestHighLevelClient client) throws Exception;
  4. }
  1. public class ConnectElasticsearch{
  2. public static void connect(ElasticsearchTask task){
  3. // 创建客户端对象
  4. RestHighLevelClient client = new RestHighLevelClient(
  5. RestClient.builder(new HttpHost("localhost", 9200, "http")));
  6. try {
  7. task.doSomething(client);
  8. // 关闭客户端连接
  9. client.close();
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. }

后续如果需要完成相关操作,只需要使用一个Lambda表达式即可:

  1. public class SomeClass {
  2. public static void main(String[] args) {
  3. ConnectElasticsearch.connect(client -> {
  4. //do something
  5. });
  6. }
  7. }

新增&修改

新增

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import com.lun.elasticsearch.hello.ConnectElasticsearch;
  3. import com.lun.elasticsearch.model.User;
  4. import org.elasticsearch.action.index.IndexRequest;
  5. import org.elasticsearch.action.index.IndexResponse;
  6. import org.elasticsearch.client.RequestOptions;
  7. import org.elasticsearch.common.xcontent.XContentType;
  8. public class InsertDoc {
  9. public static void main(String[] args) {
  10. ConnectElasticsearch.connect(client -> {
  11. // 新增文档 - 请求对象
  12. IndexRequest request = new IndexRequest();
  13. // 设置索引及唯一性标识
  14. request.index("user").id("1001");
  15. // 创建数据对象
  16. User user = new User();
  17. user.setName("zhangsan");
  18. user.setAge(30);
  19. user.setSex("男");
  20. ObjectMapper objectMapper = new ObjectMapper();
  21. String productJson = objectMapper.writeValueAsString(user);
  22. // 添加文档数据,数据格式为 JSON 格式
  23. request.source(productJson, XContentType.JSON);
  24. // 客户端发送请求,获取响应对象
  25. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
  26. 3.打印结果信息
  27. System.out.println("_index:" + response.getIndex());
  28. System.out.println("_id:" + response.getId());
  29. System.out.println("_result:" + response.getResult());
  30. });
  31. }
  32. }

修改

  1. import com.lun.elasticsearch.hello.ConnectElasticsearch;
  2. import org.elasticsearch.action.update.UpdateRequest;
  3. import org.elasticsearch.action.update.UpdateResponse;
  4. import org.elasticsearch.client.RequestOptions;
  5. import org.elasticsearch.common.xcontent.XContentType;
  6. public class UpdateDoc {
  7. public static void main(String[] args) {
  8. ConnectElasticsearch.connect(client -> {
  9. // 修改文档 - 请求对象
  10. UpdateRequest request = new UpdateRequest();
  11. // 配置修改参数
  12. request.index("user").id("1001");
  13. // 设置请求体,对数据进行修改
  14. request.doc(XContentType.JSON, "sex", "女");
  15. // 客户端发送请求,获取响应对象
  16. UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
  17. System.out.println("_index:" + response.getIndex());
  18. System.out.println("_id:" + response.getId());
  19. System.out.println("_result:" + response.getResult());
  20. });
  21. }
  22. }

查询&删除

查询

  1. import com.lun.elasticsearch.hello.ConnectElasticsearch;
  2. import org.elasticsearch.action.get.GetRequest;
  3. import org.elasticsearch.action.get.GetResponse;
  4. import org.elasticsearch.client.RequestOptions;
  5. public class GetDoc {
  6. public static void main(String[] args) {
  7. ConnectElasticsearch.connect(client -> {
  8. //1.创建请求对象
  9. GetRequest request = new GetRequest().index("user").id("1001");
  10. //2.客户端发送请求,获取响应对象
  11. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  12. 3.打印结果信息
  13. System.out.println("_index:" + response.getIndex());
  14. System.out.println("_type:" + response.getType());
  15. System.out.println("_id:" + response.getId());
  16. System.out.println("source:" + response.getSourceAsString());
  17. });
  18. }
  19. }

删除

  1. import com.lun.elasticsearch.hello.ConnectElasticsearch;
  2. import org.elasticsearch.action.delete.DeleteRequest;
  3. import org.elasticsearch.action.delete.DeleteResponse;
  4. import org.elasticsearch.client.RequestOptions;
  5. public class DeleteDoc {
  6. public static void main(String[] args) {
  7. ConnectElasticsearch.connect(client -> {
  8. //创建请求对象
  9. DeleteRequest request = new DeleteRequest().index("user").id("1001");
  10. //客户端发送请求,获取响应对象
  11. DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
  12. //打印信息
  13. System.out.println(response.toString());
  14. });
  15. }
  16. }

到这里,结合上面所用的一些API,已经可以印证之前的猜想了:提供的API都是语义化的。删除就对应DELETE,修改就对应UPDATE