当前位置:   article > 正文

架构师系列-搜索引擎ElasticSearch(三)- Java API

架构师系列-搜索引擎ElasticSearch(三)- Java API

SpringBoot整合ES
 

搭建SpringBoot工程,引入ElasticSearch相关坐标

  1. <!--引入es的坐标-->
  2. <dependency>
  3. <groupId>org.elasticsearch.client</groupId>
  4. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  5. <version>7.4.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.elasticsearch.client</groupId>
  9. <artifactId>elasticsearch-rest-client</artifactId>
  10. <version>7.4.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.elasticsearch</groupId>
  14. <artifactId>elasticsearch</artifactId>
  15. <version>7.4.0</version>
  16. </dependency>

测试
ElasticSearchConfig.java

  1. @Configuration
  2. @ConfigurationProperties(prefix = "elasticsearch")
  3. public class ElasticSearchConfig {
  4. private String host;
  5. private int port;
  6. public String getHost() {
  7. return host;
  8. }
  9. public void setHost(String host) {
  10. this.host = host;
  11. }
  12. public int getPort() {
  13. return port;
  14. }
  15. public void setPort(int port) {
  16. this.port = port;
  17. }
  18. @Bean
  19. public RestHighLevelClient client(){
  20. return new RestHighLevelClient(RestClient.builder(
  21. new HttpHost(
  22. host,
  23. port,
  24. "http"
  25. )
  26. ));
  27. }
  28. }
  1. @SpringBootTest
  2. class ElasticsearchDemoApplicationTests {
  3. @Autowired
  4. private RestHighLevelClient client;
  5. @Test
  6. void contextLoads() {
  7. /* //1.创建ES客户端对象
  8. RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
  9. new HttpHost(
  10. "192.168.149.135",
  11. 9200,
  12. "http"
  13. )
  14. ));*/
  15. System.out.println(client);
  16. }

创建索引

添加索引

  1. /**
  2. * 添加索引
  3. */
  4. @Test
  5. public void addIndex() throws IOException {
  6. //1.使用client获取操作索引的对象
  7. IndicesClient indicesClient = client.indices();
  8. //2.具体操作,获取返回值
  9. CreateIndexRequest createRequest = new CreateIndexRequest("itheima");
  10. CreateIndexResponse response = indicesClient.create(createRequest, RequestOptions.DEFAULT);
  11. //3.根据返回值判断结果
  12. System.out.println(response.isAcknowledged());
  13. }

 添加索引,并添加映射

  1. /**
  2. * 添加索引
  3. */
  4. @Test
  5. public void addIndexAndMapping() throws IOException {
  6. //1.使用client获取操作索引的对象
  7. IndicesClient indicesClient = client.indices();
  8. //2.具体操作,获取返回值
  9. CreateIndexRequest createRequest = new CreateIndexRequest("itcast");
  10. //2.1 设置mappings
  11. String mapping = "{\n" +
  12. " \"properties\" : {\n" +
  13. " \"address\" : {\n" +
  14. " \"type\" : \"text\",\n" +
  15. " \"analyzer\" : \"ik_max_word\"\n" +
  16. " },\n" +
  17. " \"age\" : {\n" +
  18. " \"type\" : \"long\"\n" +
  19. " },\n" +
  20. " \"name\" : {\n" +
  21. " \"type\" : \"keyword\"\n" +
  22. " }\n" +
  23. " }\n" +
  24. " }";
  25. createRequest.mapping(mapping,XContentType.JSON);
  26. CreateIndexResponse response = indicesClient.create(createRequest, RequestOptions.DEFAULT);
  27. //3.根据返回值判断结果
  28. System.out.println(response.isAcknowledged());
  29. }

 查询、删除、判断索引

  1. /**
  2. * 查询索引
  3. */
  4. @Test
  5. public void queryIndex() throws IOException {
  6. IndicesClient indices = client.indices();
  7. GetIndexRequest getReqeust = new GetIndexRequest("itcast");
  8. GetIndexResponse response = indices.get(getReqeust, RequestOptions.DEFAULT);
  9. //获取结果
  10. Map<String, MappingMetaData> mappings = response.getMappings();
  11. for (String key : mappings.keySet()) {
  12. System.out.println(key+":" + mappings.get(key).getSourceAsMap());
  13. }
  14. }
  15. /**
  16. * 删除索引
  17. */
  18. @Test
  19. public void deleteIndex() throws IOException {
  20. IndicesClient indices = client.indices();
  21. DeleteIndexRequest deleteRequest = new DeleteIndexRequest("itheima");
  22. AcknowledgedResponse response = indices.delete(deleteRequest, RequestOptions.DEFAULT);
  23. System.out.println(response.isAcknowledged());
  24. }
  25. /**
  26. * 判断索引是否存在
  27. */
  28. @Test
  29. public void existIndex() throws IOException {
  30. IndicesClient indices = client.indices();
  31. GetIndexRequest getRequest = new GetIndexRequest("itcast");
  32. boolean exists = indices.exists(getRequest, RequestOptions.DEFAULT);
  33. System.out.println(exists);
  34. }

添加文档

添加文档使用map作为数据

  1. /**
  2. * 添加文档,使用map作为数据
  3. */
  4. @Test
  5. public void addDoc() throws IOException {
  6. //数据对象,map
  7. Map data = new HashMap();
  8. data.put("address","北京昌平");
  9. data.put("name","大胖");
  10. data.put("age",20);
  11. //1.获取操作文档的对象
  12. IndexRequest request = new IndexRequest("itcast").id("1").source(data);
  13. //添加数据,获取结果
  14. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
  15. //打印响应结果
  16. System.out.println(response.getId());
  17. }

添加文档使用对象作为数据

  1. /**
  2. * 添加文档,使用对象作为数据
  3. */
  4. @Test
  5. public void addDoc2() throws IOException {
  6. //数据对象,javaObject
  7. Person p = new Person();
  8. p.setId("2");
  9. p.setName("小胖2222");
  10. p.setAge(30);
  11. p.setAddress("陕西西安");
  12. //将对象转为json
  13. String data = JSON.toJSONString(p);
  14. //1.获取操作文档的对象
  15. IndexRequest request = new IndexRequest("itcast").id(p.getId()).source(data,XContentType.JSON);
  16. //添加数据,获取结果
  17. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
  18. //打印响应结果
  19. System.out.println(response.getId());
  20. }
  21. public class Person {
  22. private String id;
  23. private String name;
  24. private int age;
  25. private String address;
  26. public String getId() {
  27. return id;
  28. }
  29. public void setId(String id) {
  30. this.id = id;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public void setName(String name) {
  36. this.name = name;
  37. }
  38. public int getAge() {
  39. return age;
  40. }
  41. public void setAge(int age) {
  42. this.age = age;
  43. }
  44. public String getAddress() {
  45. return address;
  46. }
  47. public void setAddress(String address) {
  48. this.address = address;
  49. }
  50. @Override
  51. public String toString() {
  52. return "Person{" +
  53. "id='" + id + '\'' +
  54. ", name='" + name + '\'' +
  55. ", age=" + age +
  56. ", address='" + address + '\'' +
  57. '}';
  58. }
  59. }

修改、查询、删除文档
 

修改文档
 

  1. /**
  2. * 修改文档:添加文档时,如果id存在则修改,id不存在则添加
  3. */
  4. @Test
  5. public void updateDoc() throws IOException {
  6. Person person=new Person();
  7. person.setId("2");
  8. person.setName("李四");
  9. person.setAge(20);
  10. person.setAddress("北京三环车王");
  11. String data = JSON.toJSONString(person);
  12. IndexRequest request = new IndexRequest("itcast").id(person.getId()).source(data,XContentType.JSON);
  13. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
  14. System.out.println(response.getId());
  15. }

查询文档

  1. /**
  2. * 根据id查询文档
  3. */
  4. @Test
  5. public void findDocById() throws IOException {
  6. GetRequest getReqeust = new GetRequest("itcast","1");
  7. //getReqeust.id("1");
  8. GetResponse response = client.get(getReqeust, RequestOptions.DEFAULT);
  9. //获取数据对应的json
  10. System.out.println(response.getSourceAsString());
  11. }

删除文档

  1. /**
  2. * 根据id删除文档
  3. */
  4. @Test
  5. public void delDoc() throws IOException {
  6. DeleteRequest deleteRequest = new DeleteRequest("itcast","1");
  7. DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
  8. System.out.println(response.getId());
  9. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/429120
推荐阅读
相关标签
  

闽ICP备14008679号