首先看要实现的功能,如下图:
<1> 支持模糊查询
<2> 支持查询匹配的关键字高亮显示
<3> 支持点赞功能,即修改文档
<4> 支持查询结果分页
<5> 支持指定字段精确匹配;支持日期和数值类的范围查询
下面用ElasticSearch(以下简称ES)一一实现上述功能。
准备工作一、搭建ElasticSearch Windows测试环境
下载
因SpringBoot的starter还不支持最新版的ES,这里使用6.5.4版本
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.zip
下载后解压缩,并执行bin/elasticsearch.bat即可启动ES
用浏览器打开 http://localhost:9200/ 如无意外,可看到类似以下信息
{
"name" : "sRdVRrd",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "HRu5vExrQo6L_OkTtCb8eQ",
"version" : {
"number" : "6.5.4",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
准备工作二、创建一个SpringBoot Web应用
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.querydsl/querydsl-core -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
repositories:
enabled: true
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML
encoding: UTF-8
cache: false
接下来创建一个用于保存和展示数据的JavaBean,本例中模拟Blog的形式,属性包括作者、内容、发布时间及点赞数,如下:
import java.util.Date;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "website", type = "blog")
public class Blog {
private int id;
private String author;
private String text;
private int likes;
private Date date;
public Blog() {
}
public Blog(int id, String author, String text, int likes, Date date) {
this.id = id;
this.author = author;
this.text = text;
this.likes = likes;
this.date = date;
}
//getter/setter略
注意注解@Document是必须的。
测试:写入一条"Blog"并读取出来
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void contextLoads() {
Blog blog1 = new Blog(1, "Mary Jones", "Jane is an expert in her field", 0, new Date());
IndexQuery indexQuery = new IndexQueryBuilder().withObject(blog1).build();
elasticsearchTemplate.index(indexQuery);
GetQuery getQuery = new GetQuery();
getQuery.setId("1");
Blog blog2 = elasticsearchTemplate.queryForObject(getQuery, Blog.class);
System.out.println(ToStringBuilder.reflectionToString(blog2));
}