Preface Integrating Elasticsearch into a Spring Boot project
Link to Elasticsearch Learning Notes
Import Dependencies
The dependency for the Elasticsearch client should match the version of the Elasticsearch server.
Because the version of the Elasticsearch client is defined in the Spring Boot project, the version needs to be manually modified to match the version of the Elasticsearch server.
pom.xml 1 2 3 4 5 6 7 8 9 10 <properties > <elasticsearch.version > 7.12.1</elasticsearch.version > </properties > <dependencies > <dependency > <groupId > org.elasticsearch.client</groupId > <artifactId > elasticsearch-rest-high-level-client</artifactId > </dependency > </dependencies >
Create RestHighLevelClient Object
Use HttpHost.create("127.0.0.1:9200")
to specify the address of the Elasticsearch server. If it is an Elasticsearch server cluster, multiple addresses can be specified.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com;import org.apache.http.HttpHost;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestHighLevelClient;import org.junit.jupiter.api.Test;import java.io.IOException;public class Tests { @Test public void test () throws IOException { RestHighLevelClient restHighLevelClient = new RestHighLevelClient (RestClient.builder( HttpHost.create("127.0.0.1:9200" ) )); System.out.println(restHighLevelClient); restHighLevelClient.close(); } }
Operations on Index Library
Perform operations on the index library through methods called from indices()
.
Add Index Library
Use indices().create()
method to create an index library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com;import org.apache.http.HttpHost;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.client.indices.CreateIndexRequest;import org.elasticsearch.common.xcontent.XContentType;import org.junit.jupiter.api.Test;import java.io.IOException;public class Tests { @Test public void test () throws IOException { RestHighLevelClient restHighLevelClient = new RestHighLevelClient (RestClient.builder( HttpHost.create("127.0.0.1:9200" ) )); CreateIndexRequest request = new CreateIndexRequest ("index name" ); String dls = "{\"mappings\": {\"properties\": {\"field name\": {\"type\": \"field type\"},\"id\": {\"type\": \"keyword\",\"index\": false},\"name\": {\"type\": \"text\",\"analyzer\": \"ik_smart\"},\"englishname\": {\"type\": \"text\",\"copy_to\": \"name\"},\"father\": {\"properties\": {\"son\": {\"type\": \"keyword\"}}}}}}\n" ; request.source(dls, XContentType.JSON); restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); restHighLevelClient.close(); } }
Delete Index Library
Use indices().delete()
method to delete an index library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com;import org.apache.http.HttpHost;import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestHighLevelClient;import org.junit.jupiter.api.Test;import java.io.IOException;public class Tests { @Test public void test () throws IOException { RestHighLevelClient restHighLevelClient = new RestHighLevelClient (RestClient.builder( HttpHost.create("127.0.0.1:9200" ) )); DeleteIndexRequest request = new DeleteIndexRequest ("index name" ); restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); restHighLevelClient.close(); } }
Check if Index Library Exists
Use indices().exists()
method to check if an index library exists.
package com;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClie