Project Creation
Add dependent
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.1.7.RELEASE</version> </dependency> <!-- <dependency>--> <!-- <groupId>org.elasticsearch</groupId>--> <!-- <artifactId>elasticsearch</artifactId>--> <!-- <version>6.2.1</version>--> <!-- </dependency>--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.2.1</version> </dependency>
use
1, application configuration
Xuecheng: elasticsearch: the hostlist: $ {eshostlist: 127.0.0.1: 9200} # plurality of intermediate nodes separated by commas
2, the configuration Elasticsearch
- A client can use, we use restHighLevelClient
@Configuration public class ElasticsearchConfig { @Value ( "xuecheng.elasticsearch.hostlist $ {}") Private String hostlist; @SuppressWarnings ( "All") @Bean public RestHighLevelClient restHighLevelClient () { // parse hostlist configuration String [] split = hostlist.split ( ","); // Create HttpHost array, wherein the configuration information is stored in the host and port es HttpHost [] = new new httpHostArray HttpHost [split.length]; for (int I = 0; I <split.length; ++ I) { String = Split Item [I]; httpHostArray [I] = new new HttpHost (item.split ( ":") [0], the Integer.parseInt (item.split ( ":") [. 1]), "HTTP "); } // Create RestHighLevelClient client new new RestHighLevelClient return (RestClient.builder (httpHostArray)); } // projects primarily RestHighLevelClient, for the client being not lower @SuppressWarnings ( "All") @Bean public RestClient RESTClient () { // parse the configuration information hostlist String [ ] = hostlist.split Split ( ","); // Create HttpHost array where host and port es store configuration information HttpHost [] = new new httpHostArray HttpHost [split.length]; for (int I = 0; I <Split .length; I ++) { String = Split Item [I]; httpHostArray [I] = new new HttpHost (item.split ( ":") [0], the Integer.parseInt (item.split ( ":") [. 1]) , "HTTP"); } return RestClient.builder (httpHostArray) .build (); } }
3, the test
@SpringBootTest @RunWith (SpringRunner.class) public class TestIndex { @Autowired RestHighLevelClient Client; // test is not used RESTClient @Autowired RestClient RESTClient; // create an index Library @Test public void testCreateIndex () throws IOException { // create an index object = new new CreateIndexRequest createIndexRequest CreateIndexRequest ( "xc_course"); // set parameters createIndexRequest.settings (Settings.builder () PUT ( "number_of_shards", ". 1") PUT ( "number_of_replicas", "0")..); // specify the mapping createIndexRequest.mapping ( "DOC", "{\ n-" + "\"properties\": {\n" + " \"name\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"description\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"studymodel\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"pic\":{\n" + " \t\"type\":\"text\"\n"+ // index client operation "}", XContentType.JSON); "} \ n-" + "} \ N-" + // create an index Library execution IndicesClient indices = client.indices (); CreateIndexResponse createIndexResponse = indices.create (createIndexRequest); // get a response Boolean Acknowledged createIndexResponse.isAcknowledged = (); System.out.println (Acknowledged); } // delete the index database @Test public testDeleteIndex void () throws IOException { // remove an indexed object DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest ( "xc_course "); client operation // index IndicesClient indices client.indices = (); // delete the index DeleteIndexResponse delete = indices.delete (deleteIndexRequest); // get a response boolean = Acknowledged the delete. isAcknowledged (); System.out.println (Acknowledged); } // add documents @Test public void testAddDoc () throws IOException { // document content // prepare json data Map <String, Object> jsonMap = HashMap new new <> (); jsonMap.put ( "name", "Cloud combat the Spring"); jsonMap.put ( "the Description", "this course is to explain in four chapters: 1. micro services architecture Getting 2.spring . Basics cloud 3. combat Spring Boot 4. registration Center Eureka "); jsonMap.put (" studymodel "," 201001 "); jsonMap.put (" PIC "," HTTP: //xx/xx/abd.png "); // = the SimpleDateFormat the dateFormat the SimpleDateFormat new new (" the mM-dd-YYYY HH: mm:ss"); //jsonMap.put("timestamp ", dateFormat.format (new new a Date ())); ())); // create index create objects IndexRequest indexRequest = new new IndexRequest ( "xc_course", "DOC"); // document content indexRequest.source (jsonMap); request for http // by Client IndexResponse indexResponse = client.index (indexRequest); DocWriteResponse.Result Result indexResponse.getResult = (); System.out.println (Result); } // query document @Test public void testGetDoc () throws IOException { // query request object GetRequest getRequest = new GetRequest ( "xc_course ", "doc", "jxhqfW4B7IA_qgGLOu2z"); GetResponse getResponse = client.get (getRequest); // get the contents of the document Map <String, Object> sourceAsMap = getResponse.getSourceAsMap (); System.out.println (sourceAsMap); } // update document (partial update) @Test public void updateDoc () throws IOException { UpdateRequest updateRequest = new UpdateRequest("xc_course", "doc", "jxhqfW4B7IA_qgGLOu2z"); Map<String, String> map = new HashMap<>(); map.put("name", "spring"); updateRequest.doc(map); UpdateResponse update = client.update(updateRequest); RestStatus status = update.status(); System.out.println(status); } //根据id删除文档 @Test public void testDelDoc() throws IOException { //删除文档id String id = "jxhqfW4B7IA_qgGLOu2z"; //删除索引请求对象 DeleteRequest deleteRequest = new DeleteRequest("xc_course", "doc", id); // response object DeleteResponse DeleteResponse = client.delete (DeleteRequest); // get the response result DocWriteResponse.Result deleteResponse.getResult Result = (); System.out.println (Result); } }