Spring Data Elasticsearch
Spring Data Elasticsearch是Spring提供的一种以Spring Data风格来操作数据存储的方式,它可以避免编写大量的样板代码。
常用注解
@Document
//表示映射到Elasticsarch文档上的领域对象
public @interface Document{
//索引库名次,可以理解为MySQL数据库的概念
String indexName;
//文档类型,可以理解为MySQL中表的概念
String type() default "";
//默认分片数
short shards() default 5;
//默认分片数量
short replicas() default 1;
}
@Id
//表示是文档的Id,可以理解为MySQL中表行的概念
public @interface Id{
}
@Field
public @interface Field{
//文档中字段的类型
FieldType type() default FieldType.Auto;
//是否建立倒排索引
boolean index() default true;
//是否进行存储
boolean store() default false;
//分词器名称
String analyzer() default "";
}
//为文档自动指定元数据类型
public enum FieldType{
Text, //会自动进行分词并建了索引的字符类型
Integer,
Long,
Date,
Float,
Double,
Boolean,
Object,
Auto, //自动判断字段类型
Nested, //嵌套对象类型
Ip, //可以索引和存储IPV4和IPV6地址
Attachment, //附件类型
Keyword //不会进行分词建立索引类型
}
Spring Data方式的数据操作
继承ElasticsearchRepository接口可以获取常用的数据操作方法
使用衍生查询
在接口中直接指定查询方法便可进行查询,无需进行实现,如表中有名称、标题、关键字,可以直接定义以下查询,即可对该三个字段实现全文搜索
/**
* 搜索查询
*
* @param name 名称
* @param subTitle 标题
* @param keywords 关键字
* @param page 分页信息
* @return
*/
Page<EsProduct> findByNameOrSubTitleOrKeywords(String name, String subTitle, String keywords, Pageable page);
衍生查询原理
使用@Query注解可以用Elasticsearch的DSL语句进行查询
@Query("{"bool" : {"must" : {"field" : {"name" : "?0"}}}}")
Page<EsProduct> findByName(String name,Pageable pageable);
整合Elasticsearch实现搜索功能
在Pom.xml中添加相关依赖
<!--Elasticsearch相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch<artifactId>
</dependency>
版本对应说明
修改SpringBoot配置文件
data:
elasticsearch:
repositories:
enabled: true
cluster-nodes: 127.0.0.1:9300 # es的连接地址及端口号
cluster-name: elasticsearch # es集群的名称
添加商品文档对象EsProduct
不需要中文分词设置为@Field(type=FieldType.Keyword)类型,需要中文分词的设置为@Field(analyzer = “ik_max_word”,type = FieldType.Text)类型。
package com.macro.tiny.nosql.elasticsearch.document;
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;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
@Data
@Document(indexName = "pms", type = "product",shards = 1,replicas = 0)
public class EsProduct implements Serializable {
private static final long serialVersionUID = -1L;
@Id
private Long id;
@Field(type = FieldType.Keyword)
private String productSn;
private Long brandId;
@Field(type = FieldType.Keyword)
private String brandName;
private Long productCategoryId;
@Field(type = FieldType.Keyword)
private String productCategoryName;
private String pic;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String name;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String subTitle;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String keywords;
private BigDecimal price;
private Integer sale;
private Integer newStatus;
private Integer recommandStatus;
private Integer stock;
private Integer promotionType;
private Integer sort;
@Field(type =FieldType.Nested)
private List<EsProductAttributeValue> attrValueList;
}
添加EsProductRepository接口用于操作Elasticsearch
继承ElasticsearchRepository接口,这样就拥有了一些基本的Elasticsearch数据操作方法,同时相当于定义了一个衍生方法
package com.macro.tiny.nosql.elasticsearch.r