SpringBoot集成Elasticsearch(二)
前言
Es的基本安装与配置参考我这篇文章 SpringBoot集成Elasticsearch(一)
本文使用kotlin来解释说明,其他语言可对照修改代码
实体类的编写
代码实现:
@Document(indexName = "brand_es", type = "brand_es")
open class EsBrand {
@org.springframework.data.annotation.Id
@Field(type = FieldType.Keyword)
var brandID: String? = null
@Field(type = FieldType.Text,analyzer = "pinyin_analyzer",searchAnalyzer = "pinyin_analyzer",fielddata = true)
var brandCname: String? = null
@Field(type = FieldType.Text)
var brandName: String? = null
@Field(type = FieldType.Keyword)
var brandLogo: String? = null
}
注解说明:
@Document注解标明实体是Elasticsearch的Document,放在类上
public @interface Document {
String indexName();//索引库的名称,只能小写,不要用特殊字符,不能超过255字节
String type() default "";//索引库的类型,建议以实体的名称命名
boolean useServerConfiguration() default false;
short shards() default 5;//默认分区数
short replicas() default 1; //每个分区默认的备份数
String refreshInterval() default "1s";//索引文件存储类型
String indexStoreType() default "fs";
boolean createIndex() default true;
VersionType versionType() default VersionType.EXTERNAL;
}
@Field注解标明了字段的配置
public @interface Field {
@AliasFor("name")
String value() default ""; //默认值
@AliasFor("value")
String name() default "";
本文介绍了如何在SpringBoot应用中集成Elasticsearch,包括实体类的编写、Dao接口的实现、设置与映射配置、以及Elasticsearch的聚合查询。使用Kotlin进行示例,并提供了ElasticsearchRepository的基本使用和分页查询的实现方法。
最低0.47元/天 解锁文章
410

被折叠的 条评论
为什么被折叠?



