1. package cn.itcast.config;
    2. import org.apache.http.HttpHost;
    3. import org.elasticsearch.client.RestClient;
    4. import org.elasticsearch.client.RestHighLevelClient;
    5. import org.springframework.boot.context.properties.ConfigurationProperties;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.context.annotation.Configuration;
    8. @Configuration
    9. @ConfigurationProperties(prefix = "elasticsearch")
    10. public class ElasticSearchConfig {
    11. private String host;
    12. private int port;
    13. public void setHost(String host) {
    14. this.host = host;
    15. }
    16. public void setPort(int port) {
    17. this.port = port;
    18. }
    19. @Bean
    20. public RestHighLevelClient restHighLevelClient(){
    21. RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(
    22. host,port,"http"
    23. )));
    24. return client;
    25. }
    26. }
    1. package cn.itcast;
    2. import cn.itcast.pojo.Person;
    3. import com.fasterxml.jackson.core.JsonProcessingException;
    4. import com.fasterxml.jackson.databind.ObjectMapper;
    5. import org.elasticsearch.action.admin.indices.create.CreateIndexAction;
    6. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
    7. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
    8. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    9. import org.elasticsearch.action.bulk.BulkRequest;
    10. import org.elasticsearch.action.bulk.BulkResponse;
    11. import org.elasticsearch.action.delete.DeleteRequest;
    12. import org.elasticsearch.action.delete.DeleteResponse;
    13. import org.elasticsearch.action.get.GetRequest;
    14. import org.elasticsearch.action.get.GetResponse;
    15. import org.elasticsearch.action.index.IndexRequest;
    16. import org.elasticsearch.action.index.IndexResponse;
    17. import org.elasticsearch.action.support.master.AcknowledgedResponse;
    18. import org.elasticsearch.client.IndicesClient;
    19. import org.elasticsearch.client.RequestOptions;
    20. import org.elasticsearch.client.RestHighLevelClient;
    21. import org.elasticsearch.common.xcontent.XContentType;
    22. import org.junit.Test;
    23. import org.junit.runner.RunWith;
    24. import org.springframework.beans.factory.annotation.Autowired;
    25. import org.springframework.boot.test.context.SpringBootTest;
    26. import org.springframework.test.context.junit4.SpringRunner;
    27. import java.io.IOException;
    28. import java.util.HashMap;
    29. import java.util.Map;
    30. @RunWith(SpringRunner.class)
    31. @SpringBootTest
    32. public class ElasticTest {
    33. @Autowired
    34. private RestHighLevelClient client;
    35. @Test
    36. public void test(){
    37. System.out.println(client);
    38. }
    39. /*
    40. * 创建索引库
    41. **/
    42. @Test
    43. public void addIndex() throws IOException {
    44. //获取索引管理客户端
    45. IndicesClient indices = client.indices();
    46. //创建请求对象,参数1-索引库名称
    47. CreateIndexRequest request = new CreateIndexRequest("itheima");
    48. //执行请求
    49. CreateIndexResponse response = indices.create(request, RequestOptions.DEFAULT);
    50. //打印结果
    51. System.out.println(response.isAcknowledged());
    52. }
    53. /*
    54. * 创建索引库,并添加映射
    55. * */
    56. @Test
    57. public void addIndexAndMapping() throws IOException {
    58. //获取索引管理客户端
    59. IndicesClient indices = client.indices();
    60. //创建请求对象,参数1-索引库名称
    61. CreateIndexRequest itcast = new CreateIndexRequest("itcast");
    62. //为某个表指定映射信息,参数1-表名,参数2-mapping具体内容,参数3-指定字符串的格式
    63. itcast.mapping("_doc", "{\n" +
    64. " \"_doc\" : {\n" +
    65. " \"properties\" : {\n" +
    66. " \"address\" : {\n" +
    67. " \"type\" : \"text\",\n" +
    68. " \"analyzer\" : \"ik_max_word\"\n" +
    69. " },\n" +
    70. " \"age\" : {\n" +
    71. " \"type\" : \"integer\"\n" +
    72. " },\n" +
    73. " \"name\" : {\n" +
    74. " \"type\" : \"keyword\"\n" +
    75. " }\n" +
    76. " }\n" +
    77. " }\n" +
    78. " }", XContentType.JSON);
    79. //执行请求
    80. CreateIndexResponse createIndexResponse = indices.create(itcast, RequestOptions.DEFAULT);
    81. //打印结果
    82. System.out.println(createIndexResponse.isAcknowledged());
    83. }
    84. /*
    85. * 删除索引库
    86. * */
    87. @Test
    88. public void deleteIndex() throws IOException {
    89. //获取索引管理客户端
    90. IndicesClient indices = client.indices();
    91. //创建请求对象
    92. DeleteIndexRequest request = new DeleteIndexRequest("itheima");
    93. //执行请求
    94. AcknowledgedResponse delete = indices.delete(request, RequestOptions.DEFAULT);
    95. //打印结果
    96. System.out.println(delete.isAcknowledged());
    97. }
    98. /*
    99. * 添加文档,方式1:通过map添加
    100. * */
    101. @Test
    102. public void addDoc1() throws IOException {
    103. //创建请求对象,参数1-索引库名称,参数2-表名,参数3-id
    104. IndexRequest indexRequest = new IndexRequest("itcast","_doc","1");
    105. //创建文档内容
    106. Map<String,Object> doc = new HashMap<>();
    107. doc.put("name","123");
    108. doc.put("age",18);
    109. doc.put("address","天香园");
    110. //添加文档内容
    111. indexRequest.source(doc);
    112. //执行请求
    113. IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
    114. System.out.println(index);
    115. }
    116. /*
    117. * 添加文档,方式2
    118. * */
    119. @Test
    120. public void addDoc2() throws IOException {
    121. //创建请求对象,参数1-索引库名称,参数2-表名,参数3-id
    122. IndexRequest request = new IndexRequest("itcast", "_doc", "1");
    123. //创建文档内容
    124. Person person = new Person();
    125. person.setName("小姐姐");
    126. person.setAge(20);
    127. person.setAddress("emm");
    128. //将对象转换为json字符串
    129. String string = new ObjectMapper().writeValueAsString(person);
    130. //添加文档内容
    131. request.source(string,XContentType.JSON);
    132. //执行请求
    133. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
    134. System.out.println(response);
    135. }
    136. /*
    137. *修改文档
    138. * */
    139. @Test
    140. public void updateDoc() throws Exception {
    141. //创建请求对象,参数1-索引库,参数2-表名,参数3-id
    142. IndexRequest request = new IndexRequest("itcast", "_doc", "1");
    143. //创建文档内容
    144. Person person = new Person();
    145. person.setName("小杰杰好骚");
    146. person.setAge(20);
    147. person.setAddress("丽春院常客");
    148. //转换为JSON字符串
    149. String value = new ObjectMapper().writeValueAsString(person);
    150. //添加文档内容
    151. request.source(value, XContentType.JSON);
    152. //执行请求
    153. IndexResponse index = client.index(request, RequestOptions.DEFAULT);
    154. System.out.println(index.getId());
    155. }
    156. /*
    157. * 根据文档id查询文档
    158. * */
    159. @Test
    160. public void findById()throws Exception{
    161. //创建请求对象
    162. GetRequest request = new GetRequest("itcast", "_doc", "1");
    163. //执行请求
    164. GetResponse response = client.get(request, RequestOptions.DEFAULT);
    165. System.out.println(response.getSourceAsString());
    166. }
    167. /*
    168. * 删除文档
    169. * */
    170. @Test
    171. public void deleteById()throws Exception{
    172. //创建请求对象
    173. DeleteRequest request = new DeleteRequest("itcast", "_doc", "1");
    174. //执行请求
    175. DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
    176. System.out.println(delete.getId());
    177. }
    178. /*
    179. * 批量添加文档
    180. * */
    181. @Test
    182. public void bulk()throws Exception{
    183. //批量请求对象
    184. BulkRequest request = new BulkRequest();
    185. //循环添加
    186. for (int i = 10; i <20 ; i++) {
    187. //创建添加文档请求对象
    188. IndexRequest indexRequest = new IndexRequest("itcast", "_doc", i + "");
    189. //创建文档内容
    190. Person person = new Person();
    191. person.setName("小骚骚"+i);
    192. person.setAge(20);
    193. person.setAddress("李春元常客"+i);
    194. //将对象转换为JSON字符串
    195. String s = new ObjectMapper().writeValueAsString(person);
    196. //添加文档内容
    197. indexRequest.source(s,XContentType.JSON);
    198. //添加请求对象
    199. request.add(indexRequest);
    200. }
    201. //执行请求
    202. BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
    203. //输出结果 getStatus返回状态码,200-成功
    204. System.out.println(bulk.status().getStatus());
    205. }
    206. }