ElasticSearch CRUD
1.springboot spring data it
spring data
A spring for data access abstraction of these data can be placed db, index, nosql and so contain the following:
spring data jpa spring for relational database access support
spring data ES spring es support for data access
and other spring spring data redis redis support for data access
spring data .....
springboot springdata xxxx
spring data es
Is the spring of Es data access. Original spring data jpa access to db.
springboot spring data es
Of spring data es simplified configuration.
2. Getting Started
Start your elasticsearch
2.1 dependence
Note: I used here lombok
<!--springboot版本仲裁中心--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--springboot 对spring data es支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> <scope>provided</scope> </dependency> </dependencies>
1.1 arrangement application.yml
Spring:
Data:
elasticsearch:
Cluster-name: elasticsearch # Specify elasticsearch cluster name Cluster-Nodes: localhost: 9300 # 9200 is a graphical user interface terminal 9300 is code side
2.3 Entrance
@SpringBootApplication public class EsApplication { public static void main(String[] args) { SpringApplication.run(EsApplication.class, args); } }
2.4 to create an index file
Note: The index database name only are all lowercase
Package cn.dyier.doc; Import lombok.Getter; Import lombok.NoArgsConstructor; Import lombok.Setter; Import lombok.ToString; 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; / ** * the Person index library * now ES Doc @Document (index, type) @Id @Feild established the object is a document direct mapping * when operating wwjEsTest person under document index database * @author Lenovo * / @Document (indexName = "wwjestest", type = "Person" ) @NoArgsConstructor @Getter @Setter @ToString (the exclude = { "All" }) public class PersonDoc { / ** * ID is the ID document object * / @ the above mentioned id Private Long the above mentioned id; / ** * no special requirements without @Field * / Private Integer Age; / ** * keyword, regardless word * / @Field (of the type = FieldType.Keyword) Private String name; / ** * specify the type of word , word, a searcher * / @Field (of the type =FieldType.Text, Analyzer = "ik_max_word", searchAnalyzer = "ik_max_word" ) Private String intro; / ** * keyword search, specify the name field acting on the intro and * virtual field all (all values need to do a keyword search, in the middle separated by white space) zs + "". + zs is a field corresponding to the field in place of a lot * / @Field (type = FieldType.Text, Analyzer = "ik_max_word", searchAnalyzer = "ik_max_word" ) Private String All; / ** splicing * * @return return the result of splicing * / public String the getAll () { return the this .name + ""+ this.intro; } public PersonDoc(Long id, Integer age, String name) { this.id = id; this.age = age; this.name = name; } }
2.5 repository configuration
public interface PersonDocRepository extends ElasticsearchRepository<PersonDoc, Long> { }
2.6 creates an index database, add type mapping, CRUD , advanced queries, sorting, pagination
package cn.dyier; import cn.dyier.doc.PersonDoc; import cn.dyier.repository.PersonDocRepository; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.sort.SortBuilders; 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.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.query.FetchSourceFilter; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; @RunWith(SpringRunner.class) @SpringBootTest(classes = EsApplication.class) public class EsTest { //所有的操作都是通过ElasticsearchTemplate @Autowired Private ElasticsearchTemplate elasticsearchTemplate; @Autowired Private PersonDocRepository personDocRepository; / ** * Create index database, the type of mapping CURD (the Repository) * @throws Exception * / @Test public void the init () throws Exception { System.out.println (elasticsearchTemplate) ; // create an index library elasticsearchTemplate.createIndex (PersonDoc. class ); // type mapping elasticsearchTemplate.putMapping (PersonDoc. class ); } / ** * Add or modify Save * ID is present in the modified index database * @throws Exception * / @Test public void testAddOrUpdate () throws Exception { PersonDoc personDoc = new new PersonDoc (1L,. 17, "wwj_test01" ); personDoc.setIntro ( " this is the first test " ); personDocRepository.save (personDoc); } / ** * add bulk saveAll * @throws Exception * / @Test public void testAddAll () throws Exception { the ArrayList<PersonDoc> personDocs = new ArrayList<>(); for (int i = 0; i < 50; i++) { PersonDoc personDoc = new PersonDoc(2L + i, 19 + i, "wwj_" + i); personDoc.setIntro("wwj_intro_" + i); personDocs.add(personDoc); } personDocRepository.saveAll(personDocs); } /** * 根据id删除一个 deleteById() * @throws Exception */ @Test public void testDeleteById() throws Exception { personDocRepository.deleteById(14L); } /** * 根据id获取 一个 * @throws Exception */ @Test public void testFindOne() throws Exception { System.out.println(personDocRepository.findById(1L)); } /** * 查询所有 * @throws Exception */ @Test public void testFindAll() throws Exception { personDocRepository.findAll () forEach (System.out :: println);. } / ** * page + DSL + Advanced Search Sort * @throws Exception * / @Test public void testDsl () throws Exception { // 1. create a constructor NativeSearchQueryBuilder NativeSearchQueryBuilder Builder = new new NativeSearchQueryBuilder (); // 2. setting conditions QueryBuilders.boolQuery () boolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery (); // 2.1 mUST field must contain all WWJ boolQueryBuilder.must (QueryBuilders.matchQuery ( "all "," wwj ")); // 2.2 filter filtering gte less than or equal to lte boolQueryBuilder.filter (QueryBuilders.rangeQuery ( "Age") GTE (20 is) .lte (50. )); Builder.withQuery (boolQueryBuilder); // 3. Sort Sort id builder.withSort (SortBuilders. fieldSort ( "ID" )); // 4. tab pages from 0 builder.withPageable (PageRequest.of (0, 10 )); // 5. the field taken builder.withSourceFilter ( new new FetchSourceFilter ( new new String [] { "ID", "name", "Age"}, null )); // 6. The results of the query and packaged NativeSearchQuery Build = builder.build (); //This page data Page <PersonDoc> = Page personDocRepository.search (Build); // Number of System.out.println (page.getTotalElements ()); // this page data System.out.println (page.getContent ()); } }