ElasticSearch 学习 二 (spring boot整合)

ElasticSearch  学习 

1、SpringBoot整合

  1. 引入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>

  2.ElasticSearchConfig配置

 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(host,port,"http")
22         ));
23     }
24 }

  3.配置yml文件

 1 elasticsearch: 2 host: localhost 3 port: 9200 

测试

  1 @RunWith(SpringRunner.class)
  2 @SpringBootTest(classes = SpringElasticSearchApplication.class)
  3 public class SpringElasticSearchApplicationTests {
  4 
  5     @Autowired
  6     private RestHighLevelClient restClient;
  7 
  8 
  9     @Test
 10     public void contextLoads() {
 11         System.out.println(restClient);
 12     }
 13 
 14     /**
 15      * 添加索引
 16      *
 17      * @throws IOException
 18      */
 19     @Test
 20     public void addIndex() throws IOException {
 21         //1.使用client获取操作索引对象
 22         IndicesClient indices = restClient.indices();
 23         //2.具体操作获取返回值
 24         //2.1 设置索引名称
 25         CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima");
 26 
 27         CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
 28         //3.根据返回值判断结果
 29         System.out.println(createIndexResponse.isAcknowledged());
 30     }
 31 
 32     /**
 33      * 添加索引与映射
 34      */
 35     @Test
 36     public void addIndexAndMapping() throws IOException {
 37         //1.获取操作索引对象
 38         IndicesClient indices = restClient.indices();
 39         //2.具体操作
 40         //创建索引
 41         CreateIndexRequest createIndexRequest = new CreateIndexRequest("itcast");
 42         //设置mapping
 43         String mapping = "{
" +
 44                 "      "properties" : {
" +
 45                 "        "address" : {
" +
 46                 "          "type" : "text",
" +
 47                 "          "analyzer" : "ik_max_word"
" +
 48                 "        },
" +
 49                 "        "age" : {
" +
 50                 "          "type" : "long"
" +
 51                 "        },
" +
 52                 "        "name" : {
" +
 53                 "          "type" : "keyword"
" +
 54                 "        }
" +
 55                 "      }
" +
 56                 "    }";
 57 
 58         createIndexRequest.mapping(mapping, XContentType.JSON);
 59 
 60         CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
 61 
 62         System.out.println("createIndexResponse = " + createIndexResponse.isAcknowledged());
 63     }
 64 
 65     /**
 66      *  C查询索引
 67      * @throws IOException
 68      */
 69     @Test
 70     public void queryIndex() throws IOException {
 71         IndicesClient indices = restClient.indices();
 72 
 73         GetIndexRequest getReqeust = new GetIndexRequest("itcast");
 74         GetIndexResponse response = indices.get(getReqeust, RequestOptions.DEFAULT);
 75 
 76 
 77         //获取结果
 78         Map<String, MappingMetaData> mappings = response.getMappings();
 79         for (String key : mappings.keySet()) {
 80             System.out.println(key+":" + mappings.get(key).getSourceAsMap());
 81 
 82         }
 83     }
 84 
 85 
 86     /**
 87      * 删除索引
 88      * @throws IOException
 89      */
 90     @Test
 91     public void deleteInsex() throws IOException {
 92         //获取索引操作对象
 93         IndicesClient indices = restClient.indices();
 94         //获取删除的请求对象
 95         DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("itcast");
 96         //执行删除
 97         AcknowledgedResponse delete = indices.delete(deleteIndexRequest, RequestOptions.DEFAULT);
 98 
 99         System.out.println("delete = " + delete.isAcknowledged());
100 
101     }
102 
103 
104     /**
105      * 文档操作,添加数据
106      */
107     @Test
108     public void addMappings() throws IOException {
109         Map map = new HashMap();
110 
111         map.put("name","张三");
112         map.put("age",18);
113         map.put("address","南京高淳");
114 
115         Person p = new Person();
116         p.setId("2");
117         p.setName("小胖2222");
118         p.setAge(30);
119         p.setAddress("陕西西安");
120 
121         //将对象转为json
122         String data = JSON.toJSONString(p);
123 
124         //1.获取操作文档的对象 ,为对象的时候source后面要制定文档类型
125         IndexRequest indexRequest = new IndexRequest("itcast").id("2").source(data,XContentType.JSON);
126         //添加数据,获取结果
127         IndexResponse response = restClient.index(indexRequest, RequestOptions.DEFAULT);
128         System.out.println("response = " + response.getId());
129     }
130 
131     /**
132      * 修改文档:添加文档时,如果id存在则修改,id不存在则添加
133      */
134     @Test
135     public void updateDoc() throws IOException {
136         Person p = new Person();
137         p.setId("2");
138         p.setName("小胖");
139         p.setAge(20);
140         p.setAddress("陕西宝鸡");
141         //将对象转为json
142         String data = JSON.toJSONString(p);
143 
144         //1.获取操作文档的对象 ,为对象的时候source后面要制定文档类型
145         IndexRequest indexRequest = new IndexRequest("itcast").id("2").source(data,XContentType.JSON);
146         //添加数据,获取结果
147         IndexResponse response = restClient.index(indexRequest, RequestOptions.DEFAULT);
148         System.out.println("response = " + response.getId());
149     }
150 
151 
152     /**
153      * 通过id查询文档
154      * @throws IOException
155      */
156     @Test
157     public void getDoc() throws IOException {
158         GetRequest request = new GetRequest("itcast", "2");
159         GetResponse r = restClient.get(request, RequestOptions.DEFAULT);
160         System.out.println("r.getSourceAsString() = " + r.getSourceAsString());
161     }
162 
163 
164     /**
165      * 根据id删除文档
166      * @throws IOException
167      */
168     @Test
169     public void deleteDoc() throws IOException {
170         DeleteRequest request = new DeleteRequest("itcast", "2");
171         DeleteResponse delete = restClient.delete(request, RequestOptions.DEFAULT);
172         System.out.println("delete = " + delete.getId());
173     }
174 }
原文地址:https://www.cnblogs.com/luckysupermarket/p/13843425.html