依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
package com.lzy.order.es;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzy.order.entity.TbProduct;
import com.lzy.order.mapper.TbProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
@Component
@EnableScheduling
@Slf4j
public class TaskElasticSearch {
private Boolean syncFirst = true;
@Autowired
TbProductMapper tbProductMapper;
@Autowired
ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
RedisTemplate redisTemplate;
@Scheduled(fixedRate = 1000)
public void fullSyncProducts() {
//--1 如果不是第一次运行,则放弃全量同步
if(!syncFirst) {
return;
}
//--2 创建索引
if(!elasticsearchRestTemplate.indexOps(TbProduct.class).exists()) {
// 创建索引
elasticsearchRestTemplate.indexOps(TbProduct.class).create();
// 手动创建映射
Document mapping = elasticsearchRestTemplate.indexOps(TbProduct.class).createMapping();
elasticsearchRestTemplate.indexOps(TbProduct.class).putMapping(mapping);
}
//--3 进行全量同步
List<TbProduct> tbProducts = tbProductMapper.selectList(null);
elasticsearchRestTemplate.save(tbProducts);
//--4 不再运行
log.info("fullSyncProducts....");
syncFirst = false;
}
}
实体类
package com.lzy.order.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.format.annotation.DateTimeFormat;
/**
* 产品表
* @TableName tb_product
*/
@TableName(value ="tb_product")
@Data
@Document(indexName = "tb_product",shards = 1, replicas = 1)
public class TbProduct implements Serializable {
/**
* 产品ID
*/
@TableId(type = IdType.AUTO)
@Id
private Integer productId;
/**
* 产品名称
*/
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String productName;
/**
* 产品价格,单位:分
*/
@Field(type = FieldType.Integer)
private Integer productPrice;
/**
* 产品库存
*/
@Field(type = FieldType.Integer)
private Integer productWare;
/**
* 产品描述
*/
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String productDesc;
/**
* 删除状态0:未删除,1:已删除
*/
@Field(type = FieldType.Integer)
private Integer deleted;
/**
* 创建时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_second")
private Date createTime;
/**
* 更新时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_second")
private Date updateTime;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}