SpringBoot整合Elasticsearch,使用定时任务添加数据到仓库

添加依赖
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
自动配置属性(如果在本机,不需要配置)
spring.elasticsearch.rest.uris=http://localhost:9200
添加实体类
@Document(indexName = "znsd",type = "student")
public class Student {
    private Integer id;
    private String name;
    private String aihao;
    private String sex;

    public Student(Integer id, String name, String aihao, String sex) {
        this.id = id;
        this.name = name;
        this.aihao = aihao;
        this.sex = sex;
    }

    public Student() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAihao() {
        return aihao;
    }

    public void setAihao(String aihao) {
        this.aihao = aihao;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", aihao='" + aihao + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}
手动自定义的查询
public interface StudentRepository extends ElasticsearchRepository<Student,Integer> {

    //根据书名搜索书籍的方法(类似jpa中的方法命名规则)

	//查询名字
    List<Student> getListByName(String name);
	//查询性别
    List<Student> getListBySex(String sex);
	//查询爱好
    List<Student> getListByAihao(String aihao);
    //查询所有
    List<Student> getAllBy();
}
增加数据到仓库
@Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Test
    void contextLoads() {
        Student student1=new Student(1,"中","打篮球","女");
        IndexQuery indexQuery =new IndexQueryBuilder()
                .withId(student1.getId().toString())
                .withObject(student1)
                .build();
        elasticsearchRestTemplate.index(indexQuery);



        Student student2=new Student(2,"中国人","打乒乓球","男");
        IndexQuery indexQuery2 =new IndexQueryBuilder()
                .withId(student2.getId().toString())
                .withObject(student2)
                .build();
        elasticsearchRestTemplate.index(indexQuery2);



        elasticsearchRestTemplate.queryForObject(GetQuery.getById("2"),Student.class);
        Student student3=new Student(3,"中国话","打高尔夫","男");
        IndexQuery indexQuery3 =new IndexQueryBuilder()
                .withId(student3.getId().toString())
                .withObject(student3)
                .build();
        elasticsearchRestTemplate.index(indexQuery3);
    }
模糊搜索
@Test
    void test3(){
        List<Student> student=studentRepository.getListByName("中");
        for (Student student1:student) {
            System.out.println(student1);
        }
    }
搜索结果
Student{id=1, name='中', aihao='打篮球', sex='女'}
Student{id=3, name='中国话', aihao='打高尔夫', sex='男'}
Student{id=2, name='中国人', aihao='打乒乓球', sex='男'}
分页查询
@Test
    void test5(){
        //分页参数
        int page = 0;
        int size = 1;
        // 构建查询条件
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        // 查询id倒序查询
        builder.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
        //模糊查询条件
        builder.withQuery(QueryBuilders.matchPhraseQuery("name", "中"));
        //分页
        builder.withPageable(PageRequest.of(page, size));
        //查询
        Page<Student> page123 =studentRepository.search(builder.build());
        //总记录数
        System.out.println(page123.getTotalElements());
        //当前页数
        System.out.println(page123.getTotalPages());
    }
其实这些东西都是要从数据查询出来放入仓库中,所有要使用定时任务,每天定时同步一下。
添加依赖
<!--quartz-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
在项目启动入口添加@EnableScheduling注解
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class DemoApplication {

     public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
@Component
public class QuartzDemo {
    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public static void work() throws Exception {
        System.out.println("执行调度任务:"+new Date());
    }


    @Scheduled(fixedRate = 5000)//每5秒执行一次
    public static void play() throws Exception {
        System.out.println("执行Quartz定时器任务:"+new Date());
    }

    @Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次
    public static void doSomething() throws Exception {
        System.out.println("每2秒执行一个的定时任务:"+new Date());
    }

    @Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
    public static void goWork() throws Exception {
        System.out.println("每一小时执行一次的定时任务:"+new Date());
    }

    @Scheduled(cron = "0 37 17 * * ?") // 到17点37执行
    public static void date() throws Exception {
        System.out.println("到了17点37执行:"+new Date());
    }
}
使用jdbc将数据库的数据查询出来,使用定时任务添加到仓库中(略)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值