添加依赖
<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> {
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();
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());
}
其实这些东西都是要从数据查询出来放入仓库中,所有要使用定时任务,每天定时同步一下。
添加依赖
<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)
public static void play() throws Exception {
System.out.println("执行Quartz定时器任务:"+new Date());
}
@Scheduled(cron = "0/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 * * ?")
public static void date() throws Exception {
System.out.println("到了17点37执行:"+new Date());
}
}
使用jdbc将数据库的数据查询出来,使用定时任务添加到仓库中(略)