Spring Data ElasticSearch Addition, Deletion and Modification Check

Spring Data ElasticSearch

1. Introduce
ElasticSearch learning roadmap
1. Learn ElasticSearch installation and its API operation
2. Learn native ElastricSearch java aui operation
3. Spring data ElasticSearch simplifies step two
ElasticSearch Data features
4. Based on @Configgutation, only need to be done in application.yml Configure, you can make the configuration information take effect
5. Tool class ElasticSearch (es module), can simplify the operation
6. Automatically generate the corresponding implementation method (Reponsitory interface) according to the persistence layer interface

2. Environment build
pom file

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

yml file

#redis配置
spring:
  redis:
    database:   0     #数据库
    host: 127.0.0.1   #地址
    port: 6379        #端口号
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300
      cluster-name: elasticsearch

test class (test class)

@RunWith(SpringRunner.class)
@SpringBootTest(classes= TestApplication.class)
public class ESTest {
    
    
    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Resource
   private BookRepositoryES bookRepositoryES;

3. Index library

Step 1: Write javabean and configure it, use javabean to correspond to es
@Decument use annotations to determine, index library, type, number of shards, number of copies
@Id unique identifier (api operation_id)
@Field use annotations to configure fields, annotations type, word segmentation analyzer, storage store, index index, etc.
Step 2: Use the ElsticSearchTemplate tool class to perform index operations
createIndex() method is used to create an index
putMapping() method is used to initialize the mapping
deleteIndex() method is used to delete index
4 document operation
javaBean


@Data
@Document(indexName = "tiao",type = "book",shards = 3,replicas = 1)
@NoArgsConstructor
@AllArgsConstructor
public class BookEs {
    
    
    @Id
    private Long id;
    @Field(type= FieldType.Text , analyzer = "ik_max_word")
    private String title;
    @Field(type = FieldType.Keyword,index = true)
    private String images;
    @Field(type = FieldType.Float)
    private Float price;
}


Create/build index

@RunWith(SpringRunner.class)
@SpringBootTest(classes= TestApplication.class)
public class ESTest {
    
    
    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Resource
   private BookRepositoryES bookRepositoryES;

    @Test
    public void deom01(){
    
    

        //1. 创建索引
        elasticsearchTemplate.createIndex(BookEs.class);
        //2. 构建类型
        elasticsearchTemplate.putMapping(BookEs.class);

    }

Add to

  @Test
    public void add(){
    
     //添加
       //准备数据
        BookEs bookEs = new BookEs();
        bookEs.setId(11l);
        bookEs.setTitle("静心怎么炼成的");
        bookEs.setImages("33.jpg");
        bookEs.setPrice(22f);
        bookRepositoryES.save(bookEs);
    }

Add a group

@Test
    public void addAll(){
    
     //添加一组数据
        //准备数据List
        List<BookEs>list=new ArrayList<>();
       list.add(new BookEs(1L,"放下手中的工作仔细听听我说","11.jpg",44.1f));
        list.add(new BookEs(2L,"夜未央星河独流淌","13.jpg",48.0f));
        list.add(new BookEs(3L,"扬帆远航 亦不过彷徨","14.jpg",66.6f));
        //添加
        bookRepositoryES.saveAll(list);
    }

modify

 @Test
    public void updateFn(){
    
    
        //修改
        BookEs es = new BookEs(11l,"枯藤老树昏鸦","33.jpg",33f);
        bookRepositoryES.save(es);
    }

Delete index

  @Test
    public void demo2(){
    
    
        //删除
        elasticsearchTemplate.deleteIndex(BookEs.class);

    }

Delete according to id

 @Test
    public void deleteFn(){
    
    
        //删除 (根据/同构id删除)
        BookEs bookEs = new BookEs();
        bookEs.setId(11l);
        bookRepositoryES.delete(bookEs);
    }

5. Query
basic query

    /**
     * 查询所有
     */
    @Test
    public void find01(){
    
    
       Iterable<BookEs>blist=bookRepositoryES.findAll();
       Iterator<BookEs> iterable=blist.iterator();
       while (iterable.hasNext()){
    
    
           BookEs bookEs=iterable.next();
           System.out.println("---------"+bookEs);
       }
    }

    /**
     * 通过id查询
     */
    public void findByIdFn(){
    
    
        Optional<BookEs> byId = bookRepositoryES.findById(11l);
        BookEs bookEs=byId.get();
        System.out.println(bookEs);
    }

Custom method query
spring data follows the naming convention, and realizes the custom method query according to the method name to know the query content.
Syntax: findBy condition
Keywords: and, or, between, not, lessThan, like, orderBy
Query by title

The first is the implementation class


public interface BookRepositoryES extends ElasticsearchCrudRepository<BookEs,Long> {
    
    
    //通过标题查询
    public List<BookEs>findByTitle(String title);

    //通过价格查询(区间)
    public List<BookEs>findByPriceBetween(float start,float end);
}

Query by title

 public void findTitle(){
    
    
        List<BookEs>list=bookRepositoryES.findByTitle("夜未央星河独流淌");
        System.out.println(list);
    }

Interval query

 public void findByPrice(){
    
    
        List<BookEs>list=bookRepositoryES.findByPriceBetween(20f,80f);
        System.out.println(list);
    }

Finally share a Feynman learning method

  1. Organize the curriculum knowledge outline, with both a framework and details
  2. Pretend to be a teacher and know the record in the syllabus, do not read the textbook to explain the course content
  3. Focus on reviewing the areas that are not clear or understand

Guess you like

Origin blog.csdn.net/weixin_43464372/article/details/105496287