栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

大数据量的索引springdata-elasticsearch 操作最佳实践

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

大数据量的索引springdata-elasticsearch 操作最佳实践

前言

对于索引结构一旦创建 就不能增加分片 除非重建索引

那对于这种需求我们该怎么办呢?

步骤

  1. blog取⼀个别名blog_latest, blog_latest作为对外使⽤
  2. 新增⼀个索引blog_20220101,结构复制于nba索引,根据业务要求修改字段
  3. 将blog数据同步到blog_20220101
  4. 给blog_20220101添加别名blog_latest,删除blog别名blog_latest
  5. 删除blog索引

我们对外提供访问blog索引时使⽤的是blog_latest别名

下面举两个例子来说明

准备工程代码



    4.0.0
    com.haoran
    blog-es
    0.0.1-SNAPSHOT
    blog-es
    demo project for spring boot

    
        1.8
        
        
    


    
        
            org.projectlombok
            lombok
            1.18.24
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch






        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-starter-test
        

    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                2.7.1
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


blog系统

假如我们有个blog系统 里面有 美食类  技术类  新闻类  历史类等等类型

我们可以这么设计一个索引映射如下:

主键  标题  类型 作者  发布日期 内容等字段

package com.sixkery.entity.es;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@TypeAlias("Blog")
@Document(indexName = "Blog")
public class AbstractBlog {

    @Id
    @Field(type = FieldType.Long)
    private Long id;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    private String title;

    @Field(type = FieldType.Keyword)
    private String type;

    @Field(type = FieldType.Keyword)
    private String author;

    @Field(type = FieldType.Date, pattern = "epoch_millis || yyyy-MM-dd HH:mm:ss")
    private Long publishDate;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    private String content;
}

美食类blog对象

@Data
@Document(indexName = "index_food_blog")
@Accessors
public class FoodBlog extends AbstractBlog {


}

技术类bolg对象

package com.sixkery.entity.es;

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;

@Data
@Document(indexName = "index_technology_blog")
public class TechnologyBlog extends AbstractBlog {


}

对应的mapping如下:

{
	"index_food_blog": {
		"aliases": {
			"Blog": {}
		},
		"mappings": {
			"properties": {
				"_class": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"author": {
					"type": "keyword"
				},
				"content": {
					"type": "text",
					"analyzer": "ik_max_word"
				},
				"id": {
					"type": "keyword"
				},
				"publishDate": {
					"type": "date",
					"format": "epoch_millis || yyyy-MM-dd HH:mm:ss"
				},
				"title": {
					"type": "text",
					"analyzer": "ik_max_word"
				},
				"type": {
					"type": "keyword"
				}
			}
		},
		"settings": {
			"index": {
				"refresh_interval": "1s",
				"number_of_shards": "5",
				"provided_name": "index_food_blog",
				"creation_date": "1654742590809",
				"store": {
					"type": "fs"
				},
				"number_of_replicas": "1",
				"uuid": "HA1i6JWgSfyz04aaZcI48Q",
				"version": {
					"created": "7040299"
				}
			}
		}
	},
	"index_technology_blog": {
		"aliases": {
			"Blog": {}
		},
		"mappings": {
			"properties": {
				"_class": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"author": {
					"type": "keyword"
				},
				"content": {
					"type": "text",
					"analyzer": "ik_max_word"
				},
				"id": {
					"type": "keyword"
				},
				"publishDate": {
					"type": "date",
					"format": "epoch_millis || yyyy-MM-dd HH:mm:ss"
				},
				"title": {
					"type": "text",
					"analyzer": "ik_max_word"
				},
				"type": {
					"type": "keyword"
				}
			}
		},
		"settings": {
			"index": {
				"refresh_interval": "1s",
				"number_of_shards": "5",
				"provided_name": "index_technology_blog",
				"creation_date": "1654742591150",
				"store": {
					"type": "fs"
				},
				"number_of_replicas": "1",
				"uuid": "gp1G1m7dRFCCflBJq3SA8g",
				"version": {
					"created": "7040299"
				}
			}
		}
	}
}

 看我们的查询和编辑操作

package com.sixkery.controller;


import com.sixkery.entity.es.AbstractBlog;
import com.sixkery.entity.es.FoodBlog;
import com.sixkery.entity.es.TechnologyBlog;
import com.sixkery.repository.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.MultiGetItem;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Resource
    private FoodBlogRepository foodBlogRepository;
    @Resource
    private TechnologyBlogRepository technologyBlogRepository;

    private AtomicLong id = new AtomicLong(0);

    @PostMapping("/add")
    public ResponseEntity addFoodBlog(@RequestBody BlogDTO blogDTO) {

        if (blogDTO.getType().equalsIgnoreCase("food")) {
            FoodBlog target = new FoodBlog();
            BeanUtils.copyProperties(blogDTO, target);
            target.setId(id.addAndGet(1));
            return ResponseEntity.ok(foodBlogRepository.save(target));
        } else {
            TechnologyBlog target = new TechnologyBlog();
            BeanUtils.copyProperties(blogDTO, target);
            target.setId(id.addAndGet(1));
            return ResponseEntity.ok(technologyBlogRepository.save(target));
        }
    }

    @GetMapping("/foodBlogs")
    public ResponseEntity> foodBlogs() {
        return ResponseEntity.ok(foodBlogRepository.findAll());
    }

    @GetMapping("/technologyBlog")
    public ResponseEntity> technologyBlog() {
        return ResponseEntity.ok(technologyBlogRepository.findAll());
    }

    @GetMapping("/allTypeBlog")
    public ResponseEntity allTypeBlog() {
//        elasticsearchRestTemplate.indexOps(FoodBlog.class).

        IndexCoordinates blog = IndexCoordinates.of("Blog");
//        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(blog);
        Query query = elasticsearchRestTemplate.matchAllQuery();
        SearchHits search = elasticsearchRestTemplate.search(query, AbstractBlog.class, blog);
//        List multiGetItems = elasticsearchRestTemplate.multiGet(query, AbstractBlog.class);


        return ResponseEntity.ok(search);
    }

//    @DeleteMapping("/deleteIndex")
//    public ResponseEntity deleteIndex() {
//        boolean deleteIndex1 = elasticsearchRestTemplate.deleteIndex(FoodBlog.class);
//        boolean deleteIndex2 = elasticsearchRestTemplate.deleteIndex(TechnologyBlog.class);
//        return ResponseEntity.ok(deleteIndex1 && deleteIndex2);
//    }


//    @PutMapping("/createIndex")
//    public ResponseEntity createIndex() {
//        boolean deleteIndex1 = elasticsearchRestTemplate.createIndex(FoodBlog.class);
//        boolean deleteIndex2 = elasticsearchRestTemplate.createIndex(TechnologyBlog.class);
//        boolean putMapping1 = elasticsearchRestTemplate.putMapping(FoodBlog.class);
//        boolean putMapping2 = elasticsearchRestTemplate.putMapping(TechnologyBlog.class);
//        boolean addAlias1 = ElasticsearchIndexOps.addAlias(FoodBlog.class.getAnnotation(Document.class).indexName(),
//                FoodBlog.class.getAnnotation(TypeAlias.class).value());
//        boolean addAlias2 = ElasticsearchIndexOps.addAlias(TechnologyBlog.class.getAnnotation(Document.class).indexName(),
//                TechnologyBlog.class.getAnnotation(TypeAlias.class).value());
//        return ResponseEntity.ok(String.join("  ", Boolean.toString(deleteIndex1), Boolean.toString(deleteIndex2),
//                Boolean.toString(putMapping1), Boolean.toString(putMapping2), Boolean.toString(addAlias1), Boolean.toString(addAlias2)));
//    }


//    @PutMapping("/indexAlias")
//    public ResponseEntity>> indexAlias() {
//        Map> map = new HashMap<>();
//        String index1 = FoodBlog.class.getAnnotation(Document.class).indexName();
//        Set alias1 = ElasticsearchIndexOps.getAlias(index1);
//        map.put(index1, alias1);
//        String index2 = FoodBlog.class.getAnnotation(Document.class).indexName();
//        Set alias2 = ElasticsearchIndexOps.getAlias(index2);
//        map.put(index2, alias2);
//        return ResponseEntity.ok(map);
//    }
//
//    @PutMapping("/deleteAlias")
//    public ResponseEntity>> deleteAlias() {
//        Map> map = new HashMap<>();
//        String index1 = FoodBlog.class.getAnnotation(Document.class).indexName();
//        Set alias1 = ElasticsearchIndexOps.getAlias(index1);
//        ElasticsearchIndexOps.delAlias(index1, alias1.toArray(new String[]{}));
//        String index2 = FoodBlog.class.getAnnotation(Document.class).indexName();
//        Set alias2 = ElasticsearchIndexOps.getAlias(index2);
//        ElasticsearchIndexOps.delAlias(index2, alias2.toArray(new String[]{}));
//        return ResponseEntity.ok(map);
//    }
}

@Data
class BlogDTO extends AbstractBlog {

}
 

2 大数据量

项目实战

订单记录 增加字段的一次 迭代升级

v0  版本  createTime  ....

 需要增加一个支付时间 

v1  版本  createTime  ....  paymentTime

package com.sixkery.entity.es;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.elasticsearch.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Data
@Document(indexName = "charge_detail_record", createIndex = false)
@TypeAlias("charge_detail_record_latest")
@Setting(settingPath = "elastic/setting/ChargeDetailRecordBill.json")
public class ChargeDetailRecord {

    
    public interface OrderView {

    }

    public interface OrderDetail extends OrderView {

    }

    public interface InvoiceBill {

    }


    
    public interface TradeView {

    }

    


    
    @Id
    @Field(type = FieldType.Long)
    @JsonView({TradeView.class, OrderView.class})
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long id;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class})
    private String uid;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})
    private LocalDate startDateTime;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private LocalDate stopDateTime;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    private String authId;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    private String authMethod;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    private String meterId;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    @JsonView({TradeView.class, OrderDetail.class, InvoiceBill.class})
    private String currency;

    
    @Field(type = FieldType.Keyword)

    @JsonView({TradeView.class, OrderView.class, OrderDetail.class, InvoiceBill.class})
    private String currencySymbol;

    
    @Field(type = FieldType.Double)

    @JsonView({InvoiceBill.class})

    private BigDecimal totalCost;

    
    @Field(type = FieldType.Double)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})


    private BigDecimal totalEnergy;

    
    @Field(type = FieldType.Double)

    private BigDecimal totalTime;

    
    @Field(type = FieldType.Double)

    private BigDecimal totalParkingTime;

    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    @JsonView({OrderView.class})

    private String remark;

    @Field(type = FieldType.Text, index = false)
    @JsonView({OrderView.class})

    private String chargingPeriods;


    @Field(type = FieldType.Text, index = false)
    private String tariffs;

    

    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private LocalDate lastUpdated;

    


    

    
    @Field(type = FieldType.Long)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long billId;
    

    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private LocalDate startCallbackTime;

    

    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private LocalDate stopCallbackTime;
    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderDetail.class, OrderView.class})

    private String orderSeq;

    
    @Field(type = FieldType.Long)

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long evseId;

    
    @Field(type = FieldType.Long)
    @JsonView({TradeView.class, OrderView.class})

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long locationId;

    
    @Field(type = FieldType.Long)

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long memberId;


    
    @Field(type = FieldType.Keyword)
    @JsonView({OrderView.class, TradeView.class, InvoiceBill.class})

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long userId;

    
    @Field(type = FieldType.Long)

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long operatorId;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({OrderView.class, TradeView.class})

    private String cardNo;


    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer orderStatus;


    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer orderType;

    
    @Field(type = FieldType.Text)
    @JsonView({OrderView.class, TradeView.class})

    private String errorCause;


    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})
    private String evseSn;


    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})
    private String orderNumber;

    
    @Field(type = FieldType.Integer)

    @JsonView({TradeView.class, InvoiceBill.class})
    private Integer payType;


    
    @Field(type = FieldType.Integer)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private Integer payStatus;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class})

    private String paySeq;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class})

    private String posSeq;

    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer chargingMode;


    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer chargingType;


    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer businessType;

    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    @JsonView({OrderView.class})

    private String stopReason;

    
    @Field(type = FieldType.Long)

    @JsonView({TradeView.class, OrderView.class, OrderView.class})
    private Long duration;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})

    private BigDecimal startSoc;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})

    private BigDecimal stopSoc;

    
    @Field(type = FieldType.Long)
    @JsonView({OrderDetail.class, TradeView.class, OrderView.class})

    private Long startMeterValue;

    
    @Field(type = FieldType.Long)
    @JsonView({OrderDetail.class, TradeView.class, OrderView.class})

    private Long stopMeterValue;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})

    private BigDecimal totalEnergyFee;

    
    @Field(type = FieldType.Long)
    @JsonView({OrderDetail.class, OrderView.class})

    private Long sumPower;

    
    @Field(type = FieldType.Double)
    @JsonView({TradeView.class, OrderView.class})

    private BigDecimal actualTotalEnergyFee;


    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class})


    private BigDecimal timeFee;


    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class})
    private BigDecimal startFee;


    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal totalAmount;

    @Field(type = FieldType.Double, index = false)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal discount;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal discountAmount;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal freeAmount;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal refundAmount;

    
    @Field(type = FieldType.Double)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})
    private BigDecimal paymentAmount;

    @Field(type = FieldType.Boolean)
    @JsonView({TradeView.class})

    private Boolean invoiced;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})

    private String timeZone;


    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView(TradeView.class)

    private String payFailReason;


    @Field(type = FieldType.Integer)
    private Integer evseType;

    
    @Field(type = FieldType.Long)
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long createBy;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private LocalDate createTime;

    
    @Field(type = FieldType.Long)
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long updateBy;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private LocalDate updateTime;

    

    

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, OrderDetail.class})

    private String telephone;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private String email;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private String memberName;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private String locationName;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})

    private String pileName;

    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})

    private String gunNo;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, OrderDetail.class})
    private String pileSn;


    @Field(type = FieldType.Integer)

    private Integer year;

    @Field(type = FieldType.Integer)

    private Integer month;

    @Field(type = FieldType.Integer)

    private Integer quarter;

    @Field(type = FieldType.Text)

    private String pileAddress;

    @Field(type = FieldType.Boolean)

    private Boolean ocpiEnabled = false;

    
    @JsonView({OrderDetail.class})
    @Field(type = FieldType.Object, index = false)
    private List measures = new ArrayList<>();

    

}

分词器索引

{

    "index": {
      "max_ngram_diff": "30",
      "refresh_interval": "1s",
      "number_of_shards": "3",
      "store": {
        "type": "fs"
      },
      "analysis": {
        "normalizer": {
          "lowercase": {
            "filter": [
              "lowercase"
            ],
            "type": "custom"
          }
        },
        "analyzer": {
          "index_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "my_tokenizer"
          },
          "search_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "whitespace"
          }
        },
        "tokenizer": {
          "my_tokenizer": {
            "token_chars": [
              "letter",
              "digit",
              "punctuation",
              "symbol"
            ],
            "min_gram": "1",
            "type": "ngram",
            "max_gram": "30"
          }
        }
      },
      "number_of_replicas": "1"
    }

}

v1

package com.sixkery.entity.es;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.elasticsearch.annotations.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;


@Data
@Document(indexName = "charge_detail_record_v1")
@TypeAlias("charge_detail_record_latest")
@Setting(settingPath = "elastic/setting/ChargeDetailRecordBill.json")
public class ChargeDetailRecord_V1 {

    
    public interface OrderView {

    }

    public interface OrderDetail extends OrderView {

    }

    public interface InvoiceBill {

    }


    
    public interface TradeView {

    }

    


    
    @Id
    @Field(type = FieldType.Long)
    @JsonView({TradeView.class, OrderView.class})
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long id;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class})
    private String uid;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})
    private Date startDateTime;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private Date stopDateTime;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    private String authId;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    private String authMethod;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    private String meterId;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")

    @JsonView({TradeView.class, OrderDetail.class, InvoiceBill.class})
    private String currency;

    
    @Field(type = FieldType.Keyword)

    @JsonView({TradeView.class, OrderView.class, OrderDetail.class, InvoiceBill.class})
    private String currencySymbol;

    
    @Field(type = FieldType.Double)

    @JsonView({InvoiceBill.class})

    private BigDecimal totalCost;

    
    @Field(type = FieldType.Double)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})


    private BigDecimal totalEnergy;

    
    @Field(type = FieldType.Double)

    private BigDecimal totalTime;

    
    @Field(type = FieldType.Double)

    private BigDecimal totalParkingTime;

    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    @JsonView({OrderView.class})

    private String remark;

    @Field(type = FieldType.Text, index = false)
    @JsonView({OrderView.class})

    private String chargingPeriods;


    @Field(type = FieldType.Text, index = false)
    private String tariffs;

    

    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private Date lastUpdated;

    


    

    
    @Field(type = FieldType.Long)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long billId;
    

    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private Date startCallbackTime;

    

    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private Date stopCallbackTime;
    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderDetail.class, OrderView.class})

    private String orderSeq;

    
    @Field(type = FieldType.Long)

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long evseId;

    
    @Field(type = FieldType.Long)
    @JsonView({TradeView.class, OrderView.class})

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long locationId;

    
    @Field(type = FieldType.Long)

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long memberId;


    
    @Field(type = FieldType.Keyword)
    @JsonView({OrderView.class, TradeView.class, InvoiceBill.class})

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long userId;

    
    @Field(type = FieldType.Long)

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long operatorId;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({OrderView.class, TradeView.class})

    private String cardNo;


    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer orderStatus;


    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer orderType;

    
    @Field(type = FieldType.Text)
    @JsonView({OrderView.class, TradeView.class})

    private String errorCause;


    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})
    private String evseSn;


    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})
    private String orderNumber;

    
    @Field(type = FieldType.Integer)

    @JsonView({TradeView.class, InvoiceBill.class})
    private Integer payType;


    
    @Field(type = FieldType.Integer)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private Integer payStatus;

    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})
    private Date paymentTime;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class})

    private String paySeq;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class})

    private String posSeq;

    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer chargingMode;


    
    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer chargingType;


    @Field(type = FieldType.Integer)
    @JsonView({OrderView.class, TradeView.class})

    private Integer businessType;

    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    @JsonView({OrderView.class})

    private String stopReason;

    
    @Field(type = FieldType.Long)

    @JsonView({TradeView.class, OrderView.class, OrderView.class})
    private Long duration;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})

    private BigDecimal startSoc;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})

    private BigDecimal stopSoc;

    
    @Field(type = FieldType.Long)
    @JsonView({OrderDetail.class, TradeView.class, OrderView.class})

    private Long startMeterValue;

    
    @Field(type = FieldType.Long)
    @JsonView({OrderDetail.class, TradeView.class, OrderView.class})

    private Long stopMeterValue;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})

    private BigDecimal totalEnergyFee;

    
    @Field(type = FieldType.Long)
    @JsonView({OrderDetail.class, OrderView.class})

    private Long sumPower;

    
    @Field(type = FieldType.Double)
    @JsonView({TradeView.class, OrderView.class})

    private BigDecimal actualTotalEnergyFee;


    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class})


    private BigDecimal timeFee;


    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class})
    private BigDecimal startFee;


    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal totalAmount;

    @Field(type = FieldType.Double, index = false)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal discount;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal discountAmount;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal freeAmount;

    
    @Field(type = FieldType.Double)
    @JsonView({OrderDetail.class, OrderView.class})
    private BigDecimal refundAmount;

    
    @Field(type = FieldType.Double)
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})
    private BigDecimal paymentAmount;

    @Field(type = FieldType.Boolean)
    @JsonView({TradeView.class})

    private Boolean invoiced;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})

    private String timeZone;


    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView(TradeView.class)

    private String payFailReason;


    @Field(type = FieldType.Integer)
    private Integer evseType;

    
    @Field(type = FieldType.Long)
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long createBy;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private Date createTime;

    
    @Field(type = FieldType.Long)
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long updateBy;

    
    @Field(type = FieldType.Date, format = DateFormat.epoch_millis, pattern = "epoch_millis")
    private Date updateTime;

    

    

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, OrderDetail.class})

    private String telephone;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private String email;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private String memberName;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, InvoiceBill.class})

    private String locationName;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})

    private String pileName;

    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class})

    private String gunNo;

    
    @Field(type = FieldType.Keyword, normalizer = "lowercase")
    @JsonView({TradeView.class, OrderView.class, OrderDetail.class})
    private String pileSn;


    @Field(type = FieldType.Integer)

    private Integer year;

    @Field(type = FieldType.Integer)

    private Integer month;

    @Field(type = FieldType.Integer)

    private Integer quarter;

    @Field(type = FieldType.Text)

    private String pileAddress;

    @Field(type = FieldType.Boolean)

    private Boolean ocpiEnabled = false;

    
    @JsonView({OrderDetail.class})
    @Field(type = FieldType.Object, index = false)
    private List measures = new ArrayList<>();

    

}
package com.sixkery.controller;

import com.sixkery.entity.es.*;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.index.AliasAction;
import org.springframework.data.elasticsearch.core.index.AliasActionParameters;
import org.springframework.data.elasticsearch.core.index.AliasActions;
import org.springframework.data.elasticsearch.core.index.AliasData;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
import org.springframework.data.elasticsearch.core.reindex.ReindexResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

@RestController
public class ElasticsearchIndexOpsController {


    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    
    @PostMapping("getAliasesForIndex")
    public Map getAliasesForIndex(@RequestBody String[] index) {
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(FoodBlog.class);
        Map aliasesForIndex = indexOperations.getAliasesForIndex(index);
        return aliasesForIndex;
    }


    @GetMapping("/search")
    public ResponseEntity> search() {
        IndexCoordinates blog = IndexCoordinates.of(ChargeDetailRecord_V1.class.getAnnotation(TypeAlias.class).value());
        Query query = elasticsearchRestTemplate.matchAllQuery();
        SearchHits search = elasticsearchRestTemplate.search(query, ChargeDetailRecord_V1.class, blog);
        return ResponseEntity.ok(search);
    }

    
    @PostMapping("createIndex")
    public Object createIndex() {
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(ChargeDetailRecord.class);
        indexOperations.create();
        return indexOperations.putMapping();
    }

    
    @PostMapping("reindex")
    public ResponseEntity reindex() {
        IndexCoordinates sourceIndex = elasticsearchRestTemplate.indexOps(ChargeDetailRecord.class).getIndexCoordinates();
        IndexCoordinates destIndex = elasticsearchRestTemplate.indexOps(ChargeDetailRecord_V1.class).getIndexCoordinates();
        ReindexRequest.ReindexRequestBuilder builder = ReindexRequest.builder(sourceIndex, destIndex);
        ReindexResponse reindexResponse = elasticsearchRestTemplate.reindex(builder.build());
        return ResponseEntity.ok(reindexResponse);
    }


    
    @PostMapping("/alias")
    public ResponseEntity addAlias() {
        AliasActions aliasActions = new AliasActions();
        AliasActionParameters.Builder builder = AliasActionParameters.builder();
        builder.withAliases(ChargeDetailRecord_V1.class.getAnnotation(TypeAlias.class).value());
        builder.withIndices(ChargeDetailRecord_V1.class.getAnnotation(Document.class).indexName());
        AliasAction aliasAction = new AliasAction.Add(builder.build());
        aliasActions.add(aliasAction);
        return ResponseEntity.ok(elasticsearchRestTemplate.indexOps(ChargeDetailRecord_V1.class).alias(aliasActions));
    }

    
    @DeleteMapping("/alias")
    public ResponseEntity removeAlias() {
        AliasActions aliasActions = new AliasActions();
        AliasActionParameters.Builder builder = AliasActionParameters.builder();
        builder.withAliases(ChargeDetailRecord.class.getAnnotation(TypeAlias.class).value());
        builder.withIndices(ChargeDetailRecord.class.getAnnotation(Document.class).indexName());
        AliasAction aliasAction = new AliasAction.Remove(builder.build());
        aliasActions.add(aliasAction);
        return ResponseEntity.ok(elasticsearchRestTemplate.indexOps(ChargeDetailRecord.class).alias(aliasActions));
    }

    
    @DeleteMapping("/index")
    public ResponseEntity deleteIndex() {
        return ResponseEntity.ok(elasticsearchRestTemplate.indexOps(ChargeDetailRecord.class).delete());
    }
}
 

 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号