上文已经教了大家最基本的操作了,那我们在java代码里面要如何实现呢?本文的目的就是教大家在springboot框架下实现上文的API操作,也就是CURD!
环境准备
首先我们要知道ES的API都是HTTP请求!!!!,所以什么语言都可以操作,就是发送请求和处理返回而已嘛,只是说现在这种封装不需要我们做,有人做好了,这种叫做ES的客户端!
依赖配置
我们直接采用Spring-data-es的依赖,先看一下版本依赖说明:
这里建议客户端版本和你自身搭建的es版本保持一致(es不同版本间api差异很大,如果不想出现莫名其妙的错的最好一致),所以这里我们选择springboot 2.3版本,这里给出spring-data-es的官方文档
# springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
# spring-elasticsearch依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
因为我这ES是7.6的,所以选择使用HighLevelRestClient客户端,虽然这个已经在高版本过时了(8.x),但是在7.x版本里面官方建议使用这个
项目引入依赖后,使用非常简单,文件中配置一下ES地址,就可以愉快的访问啦:
# yml配置文件
spring:
elasticsearch:
rest:
uris: ip:port
username:
password:
实体类准备
@Data
@Document(indexName = "es_apply_test")
public class EsTest {
@Id
private Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Keyword)
private String sex;
@Field(type = FieldType.Integer)
private Integer age;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String remark;
@Field(type = FieldType.Keyword)
private String[] tag;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String addressLocation;
@Field(type = FieldType.Keyword)
private String birthAddress;
@Field(type = FieldType.Date,pattern = "yyyy-MM-dd HH:mm:ss",format = DateFormat.custom)
private Date createTime;
@Field(type = FieldType.Boolean)
private Boolean hasGirlFriend;
public EsTest(){}
// 下面都是为了生成测试数据而准备的
private final static String[] city=new String[]{"深圳","广州","上海","北京","武汉"};
private final static String[] address=new String[]{"北京市朝阳区北辰东路15号","上海市黄浦区人民大道200号","深圳市福田区福中三路市民中心C区","武汉市江岸区一元街道沿江大道188号","广州市花都区新华街新都大道68号"};
public static EsTest getRandomData(Long id){
EsTest esTest = new EsTest();
esTest.setId(id);
esTest.setName(RandomUtil.randomString("张三李四王五陈六江文档词测试",3));
esTest.setSex(id%2==0 ? "男":"女");
esTest.setAge(RandomUtil.randomInt(15,30));
esTest.setRemark(RandomUtil.randomString("活波开朗,具有进取精神和团队精神,有较强的动手能力。良好协调沟通能力,适应力强,反应快、积极、细心、灵活, 具有一定的社会交往能力",15));
esTest.setTag(new String[]{RandomUtil.randomString("活波开朗,具有进取精神和团队精神,有较强的动手能力。良好协调沟通能力,适应力强,反应快、积极、细心、灵活, 具有一定的社会交往能力",3),
RandomUtil.randomString("活波开朗,具有进取精神和团队精神,有较强的动手能力。良好协调沟通能力,适应力强,反应快、积极、细心、灵活, 具有一定的社会交往能力",3),
RandomUtil.randomString("活波开朗,具有进取精神和团队精神,有较强的动手能力。良好协调沟通能力,适应力强,反应快、积极、细心、灵活, 具有一定的社会交往能力",3)});
esTest.setAddressLocation(address[RandomUtil.randomInt(0,address.length-1)]);
esTest.setBirthAddress(city[RandomUtil.randomInt(0,city.length-1)]);
esTest.setCreateTime(RandomUtil.randomDay(0,100));
esTest.setHasGirlFriend(id%4==0 ? true:false);
return esTest;
}
}
-
注解:@Document用来声明Java对象与ElasticSearch索引的关系
indexName 索引名称
type 索引类型
shards 主分区数量
replicas 副本分区数量
createIndex 索引不存在时,是否自动创建索引,默认true
不建议自动创建索引(自动创建的索引 是按着默认类型和默认分词器)
-
注解:@Id 表示索引的主键
-
注解:@Field 用来描述字段的ES数据类型,是否分词等配置,等于Mapping描述
index 设置字段是否索引,默认是true,如果是false则该字段不能被查询
store 默认为no,被store标记的fields被存储在和index不同的fragment中,以便于快速检索。虽然store占用磁盘空间,但是减少了计算。
type 数据类型(text、keyword、date、object、geo等)
analyzer 对字段使用分词器,注意一般如果要使用分词器,字段的type一般是text。
format 定义日期时间格式,详细见 官方文档: https://www.elastic.co/guide/reference/mapping/date-format/.
-
注解:@CompletionField 定义关键词索引 要完成补全搜索
analyzer 对字段使用分词器,注意一般如果要使用分词器,字段的type一般是text。
searchAnalyzer 显示指定搜索时分词器,默认是和索引是同一个,保证分词的一致性。
maxInputLength:设置单个输入的长度,默认为50 UTF-16 代码点
使用说明
我们引入依赖后,在使用的时候有四种使用方式(下面我由简→难说明一下):
- ElasticsearchRepository:自动生成简单CURD方法,直接调用即可(复杂的不友好)
- ElasticsearchRestTemplate:内部使用的是RestHighLevelClient,它帮我们封装了一层
- RestHighLevelClient:直接