参考文章: elasticsearchTemplate 更新数据_鶸者为何战斗-CSDN博客

/**
 * update数据
 */
public void update(String brand) {
	// 示例一
	Map<String, Object> params = new HashMap<>();
	params.put("price", new BigDecimal("800.00"));
	UpdateRequest updateRequest = new UpdateRequest();
	updateRequest.doc(params);

	UpdateQueryBuilder updateQueryBuilder = new UpdateQueryBuilder();
	updateQueryBuilder.withId("999872");
	updateQueryBuilder.withUpdateRequest(updateRequest);
	updateQueryBuilder.withClass(ItemEntity.class);

	UpdateQuery updateQuery = updateQueryBuilder.build();
	UpdateResponse updateResponse = elasticsearchTemplate.update(updateQuery);
	log.info(updateResponse.toString());

	// 示例二
	updateRequest = new UpdateRequest();
	ItemEntity itemEntity = new ItemEntity();
	itemEntity.setPrice(2.0);
	updateRequest.doc(itemEntity);

	updateQueryBuilder = new UpdateQueryBuilder();
	updateQueryBuilder.withId("999872");
	updateQueryBuilder.withUpdateRequest(updateRequest);
	updateQueryBuilder.withClass(ItemEntity.class);

	updateQuery = updateQueryBuilder.build();
	UpdateResponse updateResponse2 = elasticsearchTemplate.update(updateQuery);
}
package org.fiend.entity;

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.util.Date;


/**
 * @author langpf 2019/2/27
 */
// indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
// type类型 可以理解为表名
// shards:分片数量,默认5
// replicas:副本数量,默认1
// @Document(indexName = “test-smq”, type = “test”, refreshInterval = “1s”, createIndex = false)
@Document(indexName = "item", type = "docs", shards = 1, replicas = 0)
public class ItemEntity {
    /**
     * @Description: @Id注解必须是springframework包下的
     * org.springframework.data.annotation.Id
     */
    @Id  // 必须
    // @JsonProperty("id")
    private Long id;

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

    @Field(type = FieldType.Keyword)
    private String category;// 分类

    @Field(type = FieldType.Keyword)
    private String brand; // 品牌

    @Field(type = FieldType.Double)
    private Double price; // 价格

    @Field(index = false, type = FieldType.Keyword)
    private String images; // 图片地址

    // @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
    // @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    // @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date myDate;

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date myDate2;

    public ItemEntity() {
    }

    public ItemEntity(Long id, String title, String category, String brand, Double price, String images) {
        this.id       = id;
        this.title    = title;
        this.category = category;
        this.brand    = brand;
        this.price    = price;
        this.images   = images;
    }

    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Double getPrice() {
        return price;
    }

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

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐