依赖组件
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> </dependency>
配置
spring: elasticsearch: rest: uris: - ip1:port - ip2:port ... username: xxx password: xxx
使用说明
映射关系
spring-data-elasticsearch
提供了一系列用于实体及其属性的注解,可以方便的配置字段映射关系。
-
@Document:应用于类,包括以下属性。
-
indexName:用于存储此索引的名称,它可以包含SpEL模板表达式,例如“log-#{T(java.time.LocalDate).now().toString()}“。
-
shards:设置索引的分片数。
-
replicas:设置索引的副本数。
-
refreshIntervall:索引的刷新间隔,用于索引创建,默认值为“ 1s”。
-
indexStoreType:索引的索引存储类型,用于索引创建,默认值为“ fs”。
-
createIndex:默认为true,表示是否在应用程序启动期间检查由@Document注释定义的索引是否存在,若不存在则创建索引,并将从实体的注释建立映射。
-
versionType: 版本管理配置,默认为“EXTERNAL”。
-
-
@Id: 应用于字段,标识目的字段。
-
@Transient: 忽略映射字段。
-
@Field:应用于字段,并定义字段的属性,大多数属与Elasticsearch映射定义一致。
-
name:字段名称,设置Elasticsearch文档中字段名,如果未设置,则使用Java字段名称。
-
type:字段类型,请参考 Elasticsearch映射类型。
-
format,pattern定义日期类型及相关格式。
-
store:标记原始字段值是否应存储在Elasticsearch中,默认值为false。
-
analyzer,searchAnalyzer,normalizer用于配置分词。
-
-
@GeoPoint:将字段标记为geo_point数据类型。如果字段是GeoPoint类的实例,则可以省略。
使用示例:
@Document(indexName = "person", shards = 1, replicas = 1)
public class Person {
@Id
private Long id;
@Field(name = "name", type = FieldType.Keyword)
private String name;
@Field(name = "address", type = FieldType.Text, analyzer = "ik_max_word")
private String address;
@Field(name = "mobile", type = FieldType.Keyword)
private String mobile;
@Field(name = "email", type = FieldType.Keyword)
private String email;
@Field(name = "birthday", type = FieldType.Date, format = DateFormat.date)
private LocalDate birthday;
@Field(name = "id_card", type = FieldType.Keyword)
private String idCard;
@Field(name = "company", type = FieldType.Object)
private Company company;
}
说明:
@Document:指定Person实体对应的索引名为'person',并指定分片数和副本数为1。
@Field:配置字段映射。
-
name:指定es中文档的字段名,若不指定默认使用类字段名;需要注意的是,java中使用驼峰命名,不符合es文档命名规范,因此遇到多单词的情况需要以下划线'_'分隔,例如
id_card
。 -
type
:Elasticsearch5.x以上版本使用text
和keyword
作为字符串类型,取代之前版本的string类型,因此对于string类型的数据,需要根据使用情况处理:-
FieldType.Text:用于全文索引,该类型的字段将通过分词器进行分词,最终用于构建索引,可以使用
analyzer
字段设置分词器。 -
FieldType.Keyword:不分词,只能搜索该字段的完整的值。若配置分词器相关属性(如:
analyzer
)会报错。 -
对于string类型字段,若不指定Text或Keyword,则es会默认为string字段创建Text和Keyword以满足全文搜索和精确匹配。
-
{
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
-
若类中属性为对象类型,必须在
@Field
注解中指定type = FieldType.Object,并且该对象也要使用上述注解定义映射,否则会创建默认映射关系。
编辑
查询方法
CRUD
spring-data-elasticsearch提供了基本的CRUD操作,只需定义一个接口继承Repository
子接口,就能具备各种基本的CRUD功能。
示例:
public interface PersonRepository extends CrudRepository<Person, Long>
其中Person
是进行CRUD操作的索引对应的实体类,Long
表示索引id字段的数据类型。CrudRepository
接口默认提供了以下方法
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();
}
自定义方法
spring-data-elasticsearch提供了根据方法名进行自定义查询的方法。参考官方文档。