说明
本文介绍 springboot + Spring Data Elasticsearch方式 操作 Elasticsearch 7.9
注意
版本对应:见 官网链接
springboot 2.3.x 支持 Elasticsearch 7.6
springboot 2.4.x 支持 Elasticsearch 7.9
由于版本不同,有一些配置也会不同
本文介绍 springboot 2.4.2
准备数据
创建索引、指定映射
PUT /toutiao_v1
{
"mappings": {
"properties": {
"author": {
"type": "keyword"
},
"content": {
"type": "text"
},
"date": {
"type": "date"
},
"like": {
"type": "integer"
},
"type": {
"type": "keyword"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
添加测试数据:
POST /toutiao_v1/_doc
{
"title": "JAVA技巧",
"content": "JAVA高级技巧",
"author":"李雷",
"like":229,
"date":"2021-08-30",
"type":"java"
}
POST /toutiao_v1/_doc
{
"title": "JAVA mysql技巧大全",
"content": "JAVA mysql技巧大全",
"author":"李雷",
"like":528,
"date":"2021-08-29",
"type":"java"
}
POST /toutiao_v1/_doc
{
"title": "JAVA从入门到放弃",
"content": "JAVA高手的技巧,java基础、高级",
"author":"韩梅梅",
"like":983,
"date":"2021-08-29",
"type":"java"
}
POST /toutiao_v1/_doc
{
"title": "mysql从入门到放弃",
"content": "mysql技术内幕",
"author":"李四",
"like":397,
"date":"2021-08-29",
"type":"mysql"
}
POST /toutiao_v1/_doc
{
"title": "elasticsearch技术大全",
"content": "java操作elasticsearch",
"author":"lucy",
"like":191,
"date":"2022-03-29",
"type":"elasticsearch"
}
POST /toutiao_v1/_doc
{
"title": "java常用工具类",
"content": "常用工具类和常用代码",
"author":"lili",
"like":231,
"date":"2022-03-28",
"type":"java"
}
创建maven工程
略
修改 maven 配置
pom.xml
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.properties
在 resources 目录下创建 application.properties,内容如下:
spring.data.elasticsearch.repositories.enabled = true
spring.elasticsearch.rest.uris=http://hadoop2:9200
主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
实体类
package top.malaoshi.es.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "toutiao_v1", shards = 1, replicas = 0)
public class Toutiao {
//必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
@Id
private String id;
/**
* type : 字段数据类型
* analyzer : 分词器类型
* index : 是否索引(默认:true)
* Keyword : 短语,不进行分词
*/
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
@Field(type = FieldType.Keyword)
private String type;
@Field(type = FieldType.Keyword)
private String author;
@Field(type = FieldType.Integer)
private int like;
@Field(type = FieldType.Date)
private String date;
@Override
public String toString() {
return "Toutiao{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", content='" + content + '\'' +
", type='" + type + '\'' +
", author='" + author + '\'' +
", like=" + like +
", date=" + date +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getLike() {
return like;
}
public void setLike(int like) {
this.like = like;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
dao(可略)
一般在文档增删改、根据id查询时,使用较多
package top.malaoshi.es.dao;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import top.malaoshi.es.entity.Toutiao;
@Repository
public interface ToutiaoDao extends ElasticsearchRepository<Toutiao,String> {
}
controller
这里只写 controller 实现查询,没有 service
注意:如果没有该索引,查询时,会自动创建索引
package top.malaoshi.ctrl;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.es.entity.Toutiao;
@RestController
public class TestCtrl {
@Autowired
private ElasticsearchRestTemplate elasticsearchTemplate;
/**
* term 查询
*/
@RequestMapping("/query")
public SearchHits query() {
//term查询
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title.keyword", "JAVA技巧");
NativeSearchQuery query=new NativeSearchQueryBuilder()
.withQuery(termQueryBuilder)
.build();
SearchHits<Toutiao> shs = elasticsearchTemplate.search(query,Toutiao.class);
System.out.println("共:"+shs.getTotalHits()+" 条记录!");
for (SearchHit<Toutiao> item : shs) {
Toutiao toutiao=item.getContent();
System.out.println(toutiao);
}
return shs;
}
}