刚开始学习,只是简单入门使用,做个记录。
Elasticsearch介绍和安装
Elasticsearch环境搭建和介绍(Windows)这篇文章介绍的很全面
链接:
elasticsearch简单运用
spring boot 2.0版本
pom.xml
<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
application.properties配置文件
#elasticsearch
spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true
# cluster-name与elasticsearch配置中的cluster-name保持一致
启动文件
如果启动时遇到availableProcessors is already set to [4], rejecting [4]类似错误,需要在启动类的main方法中添加一段代码。
public static void main(String[] args) {
System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(OzoMallApplication.class, args);
}
GoodsDto实体类
package com.ozomall.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.sql.Timestamp;
@ApiModel
@TableName("goods")
@Data
@Document(indexName = "ozomall", type = "goods", shards = 1, replicas = 0)
public class GoodsDto extends PageReqDto {
@TableId(type = IdType.AUTO)
private int id;
@ApiModelProperty(value = "商品名称")
@Field(type = FieldType.Text)
private String goodsName;
@ApiModelProperty(value = "商品价格")
@Field(type = FieldType.Text)
private String goodsPrice;
@ApiModelProperty(value = "商品评论数")
private Integer commentCount;
@ApiModelProperty(value = "商品销量")
@Field(type = FieldType.Integer, value = "0")
private Integer sales;
@ApiModelProperty(value = "商品1级分类id")
private Integer classify1Id;
@ApiModelProperty(value = "商品2级分类id")
private Integer classify2Id;
@ApiModelProperty(value = "商品3级分类id")
private Integer classify3Id;
@ApiModelProperty(value = "商品品牌id")
private Integer brandId;
@ApiModelProperty(value = "商品创建时间")
private Timestamp createTime;
@ApiModelProperty(value = "删除标记")
@Field(type = FieldType.Integer)
private Integer del;
@ApiModelProperty(value = "商品状态:0下架;1上架;")
@Field(type = FieldType.Integer, value = "0")
private Integer status;
@ApiModelProperty(value = "填写商品步骤")
private Integer step;
@ApiModelProperty(value = "商品封面")
private String cover;
@ApiModelProperty(value = "商品详情")
@Field(type = FieldType.Text)
private String details;
}
需要添加@Document、@Field注解
@Document
Douument注解源码查看
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Document {
String indexName();//索引库的名称,建议以项目的名称命名
String type() default "";//类型,建议以实体的名称命名-索引库的type
boolean useServerConfiguration() default false;
short shards() default 5;//默认分区数
short replicas() default 1; //每个分区默认的备份数
String refreshInterval() default "1s";//索引文件存储类型
String indexStoreType() default "fs";
boolean createIndex() default true;
}
@Field
@Field 源码分析,查看其内部值
public @interface Field {
FieldType type() default FieldType.Auto; //自动检测属性的类型,可以根据实际情况自己设置
FieldIndex index() default FieldIndex.analyzed; //是否分词,默认情况下分词,一般默认分词就好,除非这个字段你确定查询时不会用到
DateFormat format() default DateFormat.none; //时间类型的格式化
String pattern() default "";
boolean store() default false; //默认情况下不存储原文
String analyzer() default ""; //指定字段建立索引分词时指定的分词器
//比如对索引库中的中国人进行分词
String searchAnalyzer() default ""; //指定字段被搜索时使用的分词器
//比如输入框中写中国人,然后服务器对输入框中的中国人进行分词
String[] ignoreFields() default {}; //如果某个字段需要被忽略
boolean includeInParent() default false;
}
Dao层MallGoodsMapper
package com.ozomall.dao.mall;
import com.ozomall.entity.GoodsDto;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
@Mapper
public interface MallGoodsMapper extends ElasticsearchRepository<GoodsDto, Integer> {
}
ServiceImpl
添加商品到mysql时需要同步添加到es中,包括修改删除,这里就不细写了。
/**
* 添加商品信息
*
* @param form
*/
@Override
public Result addGoods(GoodsDto form) {
int row = goodsMapper.insert(form); //这里是添加到mysql
if (row > 0) {
mallGoodsMapper.save(form); // 同步添加到es中存储
return ResultGenerate.genSuccessResult(form);
} else {
return ResultGenerate.genErroResult("商品信息添加失败,请重试!");
}
}
/**
* 搜索商品
*
* @param form
*/
@Override
public Result searchGoods(GoodsDto form) {
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
searchQueryBuilder.withQuery(QueryBuilders.matchQuery("goodsName", form.getGoodsName()));
searchQueryBuilder.withPageable(PageRequest.of(form.getPage(), form.getSize()));
org.springframework.data.domain.Page<GoodsDto> rows = mallGoodsMapper.search(searchQueryBuilder.build()); // 这里使用es搜索数据
if (rows != null) {
Map<String, Object> data = new HashMap<>();
data.put("page", rows.getNumber());
data.put("size", rows.getSize());
data.put("total", rows.getTotalElements());
data.put("records", rows.getContent());
return ResultGenerate.genSuccessResult(data);
} else {
return ResultGenerate.genErroResult("失败!");
}
}
es中的数据
查询:
结果: