package cn.itcast.config;import org.apache.http.HttpHost;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestHighLevelClient;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@ConfigurationProperties(prefix = "elasticsearch")public class ElasticSearchConfig { private String host; private int port; public void setHost(String host) { this.host = host; } public void setPort(int port) { this.port = port; } @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost( host,port,"http" ))); return client; }}
package cn.itcast;import cn.itcast.pojo.Person;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import org.elasticsearch.action.admin.indices.create.CreateIndexAction;import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;import org.elasticsearch.action.bulk.BulkRequest;import org.elasticsearch.action.bulk.BulkResponse;import org.elasticsearch.action.delete.DeleteRequest;import org.elasticsearch.action.delete.DeleteResponse;import org.elasticsearch.action.get.GetRequest;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.index.IndexRequest;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.action.support.master.AcknowledgedResponse;import org.elasticsearch.client.IndicesClient;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.common.xcontent.XContentType;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;import java.util.HashMap;import java.util.Map;@RunWith(SpringRunner.class)@SpringBootTestpublic class ElasticTest { @Autowired private RestHighLevelClient client; @Test public void test(){ System.out.println(client); } /* * 创建索引库 **/ @Test public void addIndex() throws IOException { //获取索引管理客户端 IndicesClient indices = client.indices(); //创建请求对象,参数1-索引库名称 CreateIndexRequest request = new CreateIndexRequest("itheima"); //执行请求 CreateIndexResponse response = indices.create(request, RequestOptions.DEFAULT); //打印结果 System.out.println(response.isAcknowledged()); } /* * 创建索引库,并添加映射 * */ @Test public void addIndexAndMapping() throws IOException { //获取索引管理客户端 IndicesClient indices = client.indices(); //创建请求对象,参数1-索引库名称 CreateIndexRequest itcast = new CreateIndexRequest("itcast"); //为某个表指定映射信息,参数1-表名,参数2-mapping具体内容,参数3-指定字符串的格式 itcast.mapping("_doc", "{\n" + " \"_doc\" : {\n" + " \"properties\" : {\n" + " \"address\" : {\n" + " \"type\" : \"text\",\n" + " \"analyzer\" : \"ik_max_word\"\n" + " },\n" + " \"age\" : {\n" + " \"type\" : \"integer\"\n" + " },\n" + " \"name\" : {\n" + " \"type\" : \"keyword\"\n" + " }\n" + " }\n" + " }\n" + " }", XContentType.JSON); //执行请求 CreateIndexResponse createIndexResponse = indices.create(itcast, RequestOptions.DEFAULT); //打印结果 System.out.println(createIndexResponse.isAcknowledged()); } /* * 删除索引库 * */ @Test public void deleteIndex() throws IOException { //获取索引管理客户端 IndicesClient indices = client.indices(); //创建请求对象 DeleteIndexRequest request = new DeleteIndexRequest("itheima"); //执行请求 AcknowledgedResponse delete = indices.delete(request, RequestOptions.DEFAULT); //打印结果 System.out.println(delete.isAcknowledged()); } /* * 添加文档,方式1:通过map添加 * */ @Test public void addDoc1() throws IOException { //创建请求对象,参数1-索引库名称,参数2-表名,参数3-id IndexRequest indexRequest = new IndexRequest("itcast","_doc","1"); //创建文档内容 Map<String,Object> doc = new HashMap<>(); doc.put("name","123"); doc.put("age",18); doc.put("address","天香园"); //添加文档内容 indexRequest.source(doc); //执行请求 IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT); System.out.println(index); } /* * 添加文档,方式2 * */ @Test public void addDoc2() throws IOException { //创建请求对象,参数1-索引库名称,参数2-表名,参数3-id IndexRequest request = new IndexRequest("itcast", "_doc", "1"); //创建文档内容 Person person = new Person(); person.setName("小姐姐"); person.setAge(20); person.setAddress("emm"); //将对象转换为json字符串 String string = new ObjectMapper().writeValueAsString(person); //添加文档内容 request.source(string,XContentType.JSON); //执行请求 IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response); } /* *修改文档 * */ @Test public void updateDoc() throws Exception { //创建请求对象,参数1-索引库,参数2-表名,参数3-id IndexRequest request = new IndexRequest("itcast", "_doc", "1"); //创建文档内容 Person person = new Person(); person.setName("小杰杰好骚"); person.setAge(20); person.setAddress("丽春院常客"); //转换为JSON字符串 String value = new ObjectMapper().writeValueAsString(person); //添加文档内容 request.source(value, XContentType.JSON); //执行请求 IndexResponse index = client.index(request, RequestOptions.DEFAULT); System.out.println(index.getId()); } /* * 根据文档id查询文档 * */ @Test public void findById()throws Exception{ //创建请求对象 GetRequest request = new GetRequest("itcast", "_doc", "1"); //执行请求 GetResponse response = client.get(request, RequestOptions.DEFAULT); System.out.println(response.getSourceAsString()); } /* * 删除文档 * */ @Test public void deleteById()throws Exception{ //创建请求对象 DeleteRequest request = new DeleteRequest("itcast", "_doc", "1"); //执行请求 DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT); System.out.println(delete.getId()); } /* * 批量添加文档 * */ @Test public void bulk()throws Exception{ //批量请求对象 BulkRequest request = new BulkRequest(); //循环添加 for (int i = 10; i <20 ; i++) { //创建添加文档请求对象 IndexRequest indexRequest = new IndexRequest("itcast", "_doc", i + ""); //创建文档内容 Person person = new Person(); person.setName("小骚骚"+i); person.setAge(20); person.setAddress("李春元常客"+i); //将对象转换为JSON字符串 String s = new ObjectMapper().writeValueAsString(person); //添加文档内容 indexRequest.source(s,XContentType.JSON); //添加请求对象 request.add(indexRequest); } //执行请求 BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT); //输出结果 getStatus返回状态码,200-成功 System.out.println(bulk.status().getStatus()); }}