Elasticsearch
Elastic search (es) is an open source, distributed, RESTful interface full-text search engine based on Lucene. Elasticsearch is also a distributed document database, in which each field is indexed and searchable. It can be extended to hundreds of servers to store and process PB level data. It can store, search and analyze a large amount of data in a very short time. It is usually used as the core engine in the case of complex search scenes. es is written in java language.
Elasticsearch is designed for high availability and scalability. This can be done by purchasing servers with stronger performance.
Official website
Spring Data
Spring Data is a subproject of spring. It is used to simplify database access and support NoSQL and relational database storage. The main goal is to make the database easy to access.
Official website
Spring Data ElasticSearch
Spring Data ElasticSearch simplifies the elasticSearch operation based on the spring data API and encapsulates the client API of the original elasticSearch operation. Spring Data provides an integrated search engine for elasticSearch project. Spring Data Elasticsearch POJO's key functional area centered model interacts with elasticSearch documents and easily writes a repository data access layer.
Official website
Spring Data Elasticsearchhttps://spring.io/projects/spring-data-elasticsearch
code
Create maven project and introduce dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> </dependencies>
Create application. In the resources folder Properties file
application.properties are as follows
# es service address elasticsearch.host=deploy es Server ip # es service port elasticsearch.port=9200 # Configure the log level and enable the debug log logging.level.com.es=debug
Create MainApplication
package com.es; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }
Create ElasticSearch configuration file ElasticsearchConfig
package com.es.config; import lombok.Data; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; @ConfigurationProperties(prefix = "elasticsearch") @Configuration @Data public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { private String host ; private Integer port ; @Override public RestHighLevelClient elasticsearchClient() { RestClientBuilder builder = RestClient.builder(new HttpHost(host, port)); RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder); return restHighLevelClient; } }
Create entity class Person
package com.es.DO; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @Data @Document(indexName = "contact", shards = 3, replicas = 1) public class Person { /** * Primary key id */ @Id public Long id; /** * Name name */ @Field(type = FieldType.Text, analyzer = "ik_max_word") public String name; /** * age */ @Field(type = FieldType.Integer) public int age; /** * address */ @Field(type = FieldType.Keyword, index = false) public String address; }
Configure Dao class of Person, PersonDao
package com.es.dao; import com.es.DO.Person; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface PersonDao extends ElasticsearchRepository<Person, Long>{ }
Create a test class SpringDataESIndexTest. The test class is in the same package as the startup class, otherwise the startup will report an error.
package com.es.test; import com.es.DO.Person; 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.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringDataESIndexTest { //Inject ElasticsearchRestTemplate @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; //Create index and add mapping configuration @Test public void createIndex(){ //When you create an index, the system will automatically create an index during initialization System.out.println("Create index"); } @Test public void deleteIndex(){ //When you create an index, the system will automatically create an index during initialization boolean flg = elasticsearchRestTemplate.deleteIndex(Person.class); System.out.println("Delete index = " + flg); } }
test
Test index creation and deletion
http://IP Address: 9200/_cat/indices?v , access the address with a browser or Postman to see the results
Run the createIndex test method
Run the deleteIndex test method
Document operation
Newly added SpringDataESPersonDaoTest test class
Add
@Autowired private PersonDao personDao; /** * newly added */ @Test public void save(){ Person person = new Person(); person.setId(1L); person.setName("Zhang San"); person.setAge(21); person.setAddress("Haidian District, Beijing"); personDao.save(person); }
Test address: http://IP Address: 9200/contact/_doc/1
Modification
@Autowired private PersonDao personDao; //modify @Test public void update(){ Person person = new Person(); person.setId(1L); person.setName("Zhang San"); person.setAge(21); person.setAddress("Chaoyang District of Beijing City"); personDao.save(person); }
Test address: http://IP Address: 9200/contact/_doc/1
Query by id
@Autowired private PersonDao personDao; //Query by id @Test public void findById(){ Person person = personDao.findById(1L).get(); System.out.println(person); }
Print successful
Print all
@Autowired private PersonDao personDao; @Test public void findAll(){ Iterable<Person> persons = personDao.findAll(); for (Person person : persons) { System.out.println(person); } }
delete
@Autowired private PersonDao personDao; @Test public void delete(){ Person person = new Person(); person.setId(1L); personDao.delete(person); }
Test address: http://IP Address: 9200/contact/_doc/1
Deleted
Batch add
@Autowired private PersonDao personDao; //Batch add @Test public void saveAll(){ List<Person> personList = new ArrayList<>(); for (int i = 0; i < 10; i++) { Person person = new Person(); person.setId(Long.valueOf(i)); person.setName("["+i+"]Zhang San"); person.setAge(21+i); person.setAddress("Haidian District, Beijing"); personList.add(person); } personDao.saveAll(personList); }
Test address: http://ip Address: 9200/contact/_search
Paging query
@Autowired private PersonDao personDao; @Test public void findByPageable(){ //Set sorting (sorting method, positive or reverse order, sorting id) Sort sort = Sort.by(Sort.Direction.DESC,"id"); int currentPage=0;//Current page, the first page starts from 0, and 1 indicates the second page int pageSize = 5;//How many are displayed on each page //Set query paging PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort); //Paging query Page<Person> personPage = personDao.findAll(pageRequest); for (Person person : personPage.getContent()) { System.out.println(person); } }
Query succeeded, find the first page
The complete code is as follows
package com.es.test; import com.es.DO.Person; import com.es.dao.PersonDao; 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.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class SpringDataESPersonDaoTest { @Autowired private PersonDao personDao; /** * newly added */ @Test public void save(){ Person person = new Person(); person.setId(1L); person.setName("Zhang San"); person.setAge(21); person.setAddress("Haidian District, Beijing"); personDao.save(person); } //POSTMAN, GET http://IP Address: 9200/contact/_doc/1 //modify @Test public void update(){ Person person = new Person(); person.setId(1L); person.setName("Zhang San"); person.setAge(21); person.setAddress("Chaoyang District of Beijing City"); personDao.save(person); } //POSTMAN, GET http://IP Address: 9200/contact/_doc/1 //Query by id @Test public void findById(){ Person person = personDao.findById(1L).get(); System.out.println(person); } @Test public void findAll(){ Iterable<Person> persons = personDao.findAll(); for (Person person : persons) { System.out.println(person); } } //delete @Test public void delete(){ Person person = new Person(); person.setId(1L); personDao.delete(person); } //POSTMAN, GET http://IP Address: 9200/contact/_doc/1 //Batch add @Test public void saveAll(){ List<Person> personList = new ArrayList<>(); for (int i = 0; i < 10; i++) { Person person = new Person(); person.setId(Long.valueOf(i)); person.setName("["+i+"]Zhang San"); person.setAge(21+i); person.setAddress("Haidian District, Beijing"); personList.add(person); } personDao.saveAll(personList); } //Paging query @Test public void findByPageable(){ //Set sorting (sorting method, positive or reverse order, sorting id) Sort sort = Sort.by(Sort.Direction.DESC,"id"); int currentPage=0;//Current page, the first page starts from 0, and 1 indicates the second page int pageSize = 5;//How many are displayed on each page //Set query paging PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort); //Paging query Page<Person> personPage = personDao.findAll(pageRequest); for (Person person : personPage.getContent()) { System.out.println(person); } } }
Document search
Create a new test class springdatasearchtest
termQuery
@Autowired private PersonDao personDao; /** * term query * search(termQueryBuilder) Call the search method and query the builder object with parameters */ @Test public void termQuery(){ TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "[8]Zhang San"); Iterable<Person> persons = personDao.search(termQueryBuilder); for (Person person : persons) { System.out.println(person); } }
termQuery plus paging
@Autowired private PersonDao personDao; /** * term Query and paging */ @Test public void termQueryByPage(){ int currentPage= 0 ; int pageSize = 5; //Set query paging PageRequest pageRequest = PageRequest.of(currentPage, pageSize); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "[8]Zhang San"); Iterable<Person> persons = personDao.search(termQueryBuilder,pageRequest); for (Person person : persons) { System.out.println(person); } }
There is only one record whose name is [8] Zhang San
The complete code is as follows
package com.es.test; import com.es.DO.Person; import com.es.dao.PersonDao; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; 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.data.domain.PageRequest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringDataESSearchTest { @Autowired private PersonDao personDao; /** * term query * search(termQueryBuilder) Call the search method and query the builder object with parameters */ @Test public void termQuery(){ TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "[8]Zhang San"); Iterable<Person> persons = personDao.search(termQueryBuilder); for (Person person : persons) { System.out.println(person); } } /** * term Query and paging */ @Test public void termQueryByPage(){ int currentPage= 0 ; int pageSize = 5; //Set query paging PageRequest pageRequest = PageRequest.of(currentPage, pageSize); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "[8]Zhang San"); Iterable<Person> persons = personDao.search(termQueryBuilder,pageRequest); for (Person person : persons) { System.out.println(person); } } }