Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
preface
Happy holiday, everyone. 1024 falls on the weekend. I wish you no bugs. This chapter describes how to integrate Elasticsearch with Spring Boot. Let’s get started.
ES introduction
What is ES
Elasticsearch is a distributed, scalable, real-time search and data analysis engine. It makes it easy to search, analyze and explore large amounts of data. Taking advantage of Elasticsearch’s horizontal scalability can make data more valuable in a production environment. Elasticsearch is implemented in the following steps: First, users submit data to the Elasticsearch database, then use the word segmentation controller to classify the corresponding words, and store their weight and word segmentation results in the data. When users search for data, they rank and score the results based on the weight. The result is then presented to the user.
ES characteristics
- High scalability
- Out of the box, simple to build
- Complementing the database
- The integration of full-text retrieval, data analysis and distributed technology
Quick start
Introduction of depend on
The dependency version must be the same as the version used by Spring Boot. If the version number does not match, an unknown exception may occur. This Demo is for demonstration purposes only, so 7.6.1 is used. SpringBoot uses 2.3.0.release.
<! -- ES start --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId> ElasticSearch -rest-high-level-client</artifactId> <version>7.6.1 </version> </dependency> <! -- ES end --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> The < version > 1.2.74 < / version > < / dependency >Copy the code
Configuration parameters
Elasticsearch port 9200 is used to allow HTTP REST APIS to access Elasticsearch. Port 9300 is the default port for transport layer listening. Cluster. name must be consistent with cluster.name in config/ elasticSearch. yml.
Elasticsearch. IP = 127.0.0.1 elasticsearch. Port = 9300 elasticsearch. Pool = 10. Elasticsearch cluster. The name = my - applicationCopy the code
The configuration class
@slf4j@configuration Public class ElasticSearchConfig {/** * ELk Cluster address */ @value ("${elasticSearch.ip}") private String hostName; /** * port */ @value ("${elasticSearch.port}") private int port; / cluster name * * * * / @ Value (" ${elasticsearch. Cluster. The name} ") private String clusterName; /** * pool */ @value ("${elasticSearch.pool}") private String poolSize; /** * Bean name Default function name ** @return */ @bean public RestHighLevelClient RestHighLevelClient (){RestHighLevelClient client =new RestHighLevelClient(RestClient.builder(new HttpHost(hostName,port,"http"))); return client; }}Copy the code
Writing test classes
@Autowired private RestHighLevelClient client; @Test public void testES(){ System.out.println(client); @test public void testIndexData() throws IOException {CreateIndexRequest Request = new CreateIndexRequest("es_demo_index1"); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse.index()); System.out.println(createIndexResponse); } // Check whether the index exists @test public void testExistIndex () throws IOException{GetIndexRequest request = new GetIndexRequest("es_demo_index1"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); }Copy the code
One is to create an index and the other is to determine whether an index exists. Creating an index succeededCheck whether the index exists
Elasticsearch with Spring Boot is a simple example of Elasticsearch integrated with Spring Boot. You can also check the interface documentation for more details.
Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.
About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.