Elasticsearch挂掉后,如何快速恢复数据

目录

一、Elasticsearch使用

二、实体类

[2.1 mysql 实体类](#2.1 mysql 实体类)

[2.2 Elasticsearch实体类](#2.2 Elasticsearch实体类)

三、XXL-job定时执行


一、Elasticsearch使用

当我们做搜索功能时,如果为了提高查询效率,通常使用Elasticsearch搜索引擎加快搜索效率。以搜索商品为例,我们mysql肯定有一张商品表,对应的有一个实体类,而我们的es索引中,我们要多冗余两个字段,销量和好评。当用户搜索的时候,我们直接查Elasticsearch中的数据,但是当我们的Elasticsearch挂了之后,es数据会消失,我们可以用xxl-job做个定时任务,先查询mysql中的数据,再将数据库里的数据遍历出来再添加到Elasticsearch中。

二、实体类

2.1 mysql 实体类

java 复制代码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.beimao.common.model;

import com.beimao.common.enums.ProductStatus;
import java.math.BigDecimal;

public class Product extends BaseModel {
    private Integer id;
    private String name;
    private String subName;
    private Integer categoryId;
    private String img;
    private Integer status = 1;
    private String statusX;
    private BigDecimal price;
    private String brief;
    private Integer seq = 0;
    private String tags;

    public String getStatusX() {
        ProductStatus productStatus = ProductStatus.findByCode(this.status);
        return productStatus != null ? productStatus.getDesc() : "未定义";
    }

    public Product() {
    }

    public Integer getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public String getSubName() {
        return this.subName;
    }

    public Integer getCategoryId() {
        return this.categoryId;
    }

    public String getImg() {
        return this.img;
    }

    public Integer getStatus() {
        return this.status;
    }

    public BigDecimal getPrice() {
        return this.price;
    }

    public String getBrief() {
        return this.brief;
    }

    public Integer getSeq() {
        return this.seq;
    }

    public String getTags() {
        return this.tags;
    }

    public void setId(final Integer id) {
        this.id = id;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setSubName(final String subName) {
        this.subName = subName;
    }

    public void setCategoryId(final Integer categoryId) {
        this.categoryId = categoryId;
    }

    public void setImg(final String img) {
        this.img = img;
    }

    public void setStatus(final Integer status) {
        this.status = status;
    }

    public void setStatusX(final String statusX) {
        this.statusX = statusX;
    }

    public void setPrice(final BigDecimal price) {
        this.price = price;
    }

    public void setBrief(final String brief) {
        this.brief = brief;
    }

    public void setSeq(final Integer seq) {
        this.seq = seq;
    }

    public void setTags(final String tags) {
        this.tags = tags;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Product)) {
            return false;
        } else {
            Product other = (Product)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label143: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label143;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label143;
                    }

                    return false;
                }

                Object this$categoryId = this.getCategoryId();
                Object other$categoryId = other.getCategoryId();
                if (this$categoryId == null) {
                    if (other$categoryId != null) {
                        return false;
                    }
                } else if (!this$categoryId.equals(other$categoryId)) {
                    return false;
                }

                Object this$status = this.getStatus();
                Object other$status = other.getStatus();
                if (this$status == null) {
                    if (other$status != null) {
                        return false;
                    }
                } else if (!this$status.equals(other$status)) {
                    return false;
                }

                label122: {
                    Object this$seq = this.getSeq();
                    Object other$seq = other.getSeq();
                    if (this$seq == null) {
                        if (other$seq == null) {
                            break label122;
                        }
                    } else if (this$seq.equals(other$seq)) {
                        break label122;
                    }

                    return false;
                }

                label115: {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name == null) {
                            break label115;
                        }
                    } else if (this$name.equals(other$name)) {
                        break label115;
                    }

                    return false;
                }

                Object this$subName = this.getSubName();
                Object other$subName = other.getSubName();
                if (this$subName == null) {
                    if (other$subName != null) {
                        return false;
                    }
                } else if (!this$subName.equals(other$subName)) {
                    return false;
                }

                Object this$img = this.getImg();
                Object other$img = other.getImg();
                if (this$img == null) {
                    if (other$img != null) {
                        return false;
                    }
                } else if (!this$img.equals(other$img)) {
                    return false;
                }

                label94: {
                    Object this$statusX = this.getStatusX();
                    Object other$statusX = other.getStatusX();
                    if (this$statusX == null) {
                        if (other$statusX == null) {
                            break label94;
                        }
                    } else if (this$statusX.equals(other$statusX)) {
                        break label94;
                    }

                    return false;
                }

                label87: {
                    Object this$price = this.getPrice();
                    Object other$price = other.getPrice();
                    if (this$price == null) {
                        if (other$price == null) {
                            break label87;
                        }
                    } else if (this$price.equals(other$price)) {
                        break label87;
                    }

                    return false;
                }

                Object this$brief = this.getBrief();
                Object other$brief = other.getBrief();
                if (this$brief == null) {
                    if (other$brief != null) {
                        return false;
                    }
                } else if (!this$brief.equals(other$brief)) {
                    return false;
                }

                Object this$tags = this.getTags();
                Object other$tags = other.getTags();
                if (this$tags == null) {
                    if (other$tags != null) {
                        return false;
                    }
                } else if (!this$tags.equals(other$tags)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Product;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $categoryId = this.getCategoryId();
        result = result * 59 + ($categoryId == null ? 43 : $categoryId.hashCode());
        Object $status = this.getStatus();
        result = result * 59 + ($status == null ? 43 : $status.hashCode());
        Object $seq = this.getSeq();
        result = result * 59 + ($seq == null ? 43 : $seq.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $subName = this.getSubName();
        result = result * 59 + ($subName == null ? 43 : $subName.hashCode());
        Object $img = this.getImg();
        result = result * 59 + ($img == null ? 43 : $img.hashCode());
        Object $statusX = this.getStatusX();
        result = result * 59 + ($statusX == null ? 43 : $statusX.hashCode());
        Object $price = this.getPrice();
        result = result * 59 + ($price == null ? 43 : $price.hashCode());
        Object $brief = this.getBrief();
        result = result * 59 + ($brief == null ? 43 : $brief.hashCode());
        Object $tags = this.getTags();
        result = result * 59 + ($tags == null ? 43 : $tags.hashCode());
        return result;
    }

    public String toString() {
        return "Product(id=" + this.getId() + ", name=" + this.getName() + ", subName=" + this.getSubName() + ", categoryId=" + this.getCategoryId() + ", img=" + this.getImg() + ", status=" + this.getStatus() + ", statusX=" + this.getStatusX() + ", price=" + this.getPrice() + ", brief=" + this.getBrief() + ", seq=" + this.getSeq() + ", tags=" + this.getTags() + ")";
    }
}
java 复制代码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.beimao.common.model;

import java.time.LocalDateTime;

public abstract class BaseModel {
    private String lastUpdateBy;
    private LocalDateTime lastUpdateTime;

    public BaseModel() {
    }

    public String getLastUpdateBy() {
        return this.lastUpdateBy;
    }

    public void setLastUpdateBy(String lastUpdateBy) {
        this.lastUpdateBy = lastUpdateBy;
    }

    public LocalDateTime getLastUpdateTime() {
        return this.lastUpdateTime;
    }

    public void setLastUpdateTime(LocalDateTime lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }
}

2.2 Elasticsearch实体类

java 复制代码
package com.beimao.model;

import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName;
import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.annotation.rely.IdType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * EsProduct 类用于表示一个商品实体,并定义了商品相关的属性。
 * 它使用了特定的注解来定义如何在搜索引擎中索引这些属性。
 */
@IndexName(aliasName = "beimao_product") // 定义索引的别名
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class EsProduct {

    //商品id
    @IndexId(type = IdType.CUSTOMIZE) // 定义自定义的主键ID
    private Integer id;

    //商品名称
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD, searchAnalyzer = Analyzer.IK_MAX_WORD)
    private String name;// 商品名称,使用IK分词器进行索引,便于全文检索

    //商品标签
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD)
    private List<String> tags; //使用列表存储并使用IK分词器,便于检索具有多个标签的商品

    //商品副标题
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD)
    private String subName;  // 假设subName也需要全文搜索

    //商品价格
    @IndexField(fieldType = FieldType.DOUBLE)
    private BigDecimal price;

    //商品状态
    @IndexField(fieldType = FieldType.INTEGER)
    private Integer status; // 使用KEYWORD类型更改为TEXT以示例中保持一致,实际根据需求可调整为FieldType.KEYWORD以进行精确匹配
    //商品品类
    private Integer categoryId; // 商品所属的类别ID

    //商品图片
    @IndexField(fieldType = FieldType.KEYWORD)
    private String img; // 商品图片链接,使用关键词类型索引,适用于精确匹配

    //商品简介
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD)
    private String brief; // 商品简介,使用IK分词器进行索引,便于全文检索


    //商品排序
    @IndexField(fieldType = FieldType.INTEGER)
    private Integer sort;
    //商品库存
    @IndexField(fieldType = FieldType.INTEGER)
    private String stock;

    //商品好评数量
    @IndexField(fieldType = FieldType.INTEGER)
    private Integer highOpinion = 0; // 默认好评数量为0
    //商品销量
    @IndexField(fieldType = FieldType.INTEGER)
    private Integer salesVolume = 0; // 商品销量,整型存储


}

三、XXL-job定时执行

相关推荐
小小工匠5 分钟前
ElasticSearch - 深入解析 Elasticsearch Composite Aggregation 的分页与去重机制
elasticsearch·composite·after_key·桶聚合分页
风_流沙9 分钟前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
szxinmai主板定制专家15 分钟前
【国产NI替代】基于FPGA的32通道(24bits)高精度终端采集核心板卡
大数据·人工智能·fpga开发
TGB-Earnest2 小时前
【py脚本+logstash+es实现自动化检测工具】
大数据·elasticsearch·自动化
大圣数据星球4 小时前
Fluss 写入数据湖实战
大数据·设计模式·flink
suweijie7684 小时前
SpringCloudAlibaba | Sentinel从基础到进阶
java·大数据·sentinel
Data跳动9 小时前
Spark内存都消耗在哪里了?
大数据·分布式·spark
woshiabc11110 小时前
windows安装Elasticsearch及增删改查操作
大数据·elasticsearch·搜索引擎
lucky_syq11 小时前
Saprk和Flink的区别
大数据·flink
lucky_syq11 小时前
流式处理,为什么Flink比Spark Streaming好?
大数据·flink·spark