Spring Data ElasticSearch增删改查

Spring Data ElasticSearch

一.介绍
ElasticSearch 学习路线图
1.学习ElasticSearch安装及其API操作
2. 学习原生ElastricSearch java aui操作
3. spring data ElasticSearch 对步骤二进行简化
ElasticSearch Data 特点
4. 基于@Configgutation,只需要在application.yml进行配置,就可以使配置信息生效
5. 工具类 ElasticSearch(es模块),可以简化操作
6. 根据持久层接口自动生成对应实现方法(Reponsitory接口)

二. 环境搭建
pom文件

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

yml文件

#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类(测试类)

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

    @Resource
   private BookRepositoryES bookRepositoryES;

3.索引库

步骤1 : 编写javabean ,并进行配置,使用javabean与es对应
@Decument使用注解确定 ,索引库,类型 ,分片数, 副本数
@Id 唯一标识(api操作_id)
@Field 使用注解配置字段, 注解type ,分词analyzer ,存储store ,索引 index 等
步骤2 : 使用ElsticSearchTemplate工具类,进行索引操作
createIndex()方法用于创建索引
putMapping()方法用于初始化映射
deleteIndex()方法用于删除索引
4文档操作
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;
}


创建/构建索引

@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);

    }

添加

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

添加一组

@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);
    }

修改

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

删除索引

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

    }

根据id删除

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

5.查询
基本查询

    /**
     * 查询所有
     */
    @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);
    }

自定义方法查询
spring data按照命名约定, 实现自定义方法查询 根据方法名就可知晓查询内容
语法: findBy条件
关键字: and , or , between , not , lessThan ,like ,orderBy
通过标题查询

首先在是实现类


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

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

根据标题查询

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

区间查询

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

最后分享一个费曼学习法

  1. 整理课程知识大纲,既要有框架又要有细节
  2. 假装自己是老师 知道大纲里的备案, 不看课本讲解课程内容
  3. 记不清或讲不明白的地方重点复习

你可能感兴趣的:(spring,elasticsearch)