Springboot-data-es 自动创建mapping(mapping的数据结构为空)以及自动添加新字段(生成mapping的数据结构)

本文详细讲解如何使用SpringBoot结合Elasticsearch自动创建Mapping,包括实体类定义、实体仓库创建及如何通过数据保存操作自动更新Mapping结构。

一步  创建mapping

要想让springboot自动启动时候自动创建mapping

1.定义实体:

package com.ljf.springboot.data.es.model;

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 java.io.Serializable;

/**
 * @className Book
 * @Description TODO
 * @Author admin
 * @Date 2019/4/24 17:11
 * @Version 1.0
 **/
// SpringBoot启动时会自动创建映射,但要注意如果已经存在相同的index,必须先删除
//@Document(indexName = "#{esMyConfig.indexName}",type="#{esMyConfig.typeName}")
@Document(indexName = "book",type="bookentity")
public class Book implements Serializable {
    @Id
    private String id;
    //@Field(type= FieldType.Text)
    private String title;
    //@Field(type= FieldType.Keyword)
    private String author;
   // @Field(format= DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss",type= FieldType.Date)
    private String createDate;
   // @Field(type= FieldType.Long)
    private double price;
  //  @Field(type= FieldType.Integer)
    private Integer age;
    public Book() {
    }

    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public double getPrice() {
        return price;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

2.创建实体仓库:注意没有这一步,即便在实体上方设置@Document(indexName = "book",type="bookentity"),springboot启动也不会创建mapping

package com.ljf.springboot.data.es.repository;

import com.ljf.springboot.data.es.model.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book,String> {
    List<Book> findByTitle(String title);
}

3.创建了mapping,但没有字段:即创建了一个空mapping

这是因为在定义book实体时候,字段没有加注解:

   @Id
    private String id;
    //@Field(type= FieldType.Text)
    private String title;
    //@Field(type= FieldType.Keyword)
    private String author;
   // @Field(format= DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss",type= FieldType.Date)
    private String createDate;
   // @Field(type= FieldType.Long)
    private double price;
  //  @Field(type= FieldType.Integer)
    private Integer age;

所以在springboot启动创建mapping时候创建了一个空mapping

4.添加一条数据:再查看mapping结构

执行添加数据请求:

新加的数据如下:

再次查看mapping结构:mapping已经有了数据结构,

结论:

spring-data-elastic:

1.映射实体类字段没有加注解

2.实体类添加了新的字段,

这两种情况,在保存数据的时候,会创建新的mapping,自动加上新的字段结构,形成新的mapping。

spring-data-elastic:实体类加了注解,springboot自启动时候,会创建具有相应字段的mapping结构,即mapping不为空。

https://blog.youkuaiyun.com/u011066470/article/details/89968673

https://www.jianshu.com/p/ae1ea1924a5d

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值