• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

elasticsearchTemplate UpdateQuery 更新数据

武飞扬头像
风情客家__
帮助1

  1.  
    /**
  2.  
    * update数据
  3.  
    */
  4.  
    public void update(String brand) {
  5.  
    // 示例一
  6.  
    Map<String, Object> params = new HashMap<>();
  7.  
    params.put("price", new BigDecimal("800.00"));
  8.  
    UpdateRequest updateRequest = new UpdateRequest();
  9.  
    updateRequest.doc(params);
  10.  
     
  11.  
    UpdateQueryBuilder updateQueryBuilder = new UpdateQueryBuilder();
  12.  
    updateQueryBuilder.withId("999872");
  13.  
    updateQueryBuilder.withUpdateRequest(updateRequest);
  14.  
    updateQueryBuilder.withClass(ItemEntity.class);
  15.  
     
  16.  
    UpdateQuery updateQuery = updateQueryBuilder.build();
  17.  
    UpdateResponse updateResponse = elasticsearchTemplate.update(updateQuery);
  18.  
    log.info(updateResponse.toString());
  19.  
     
  20.  
    // 示例二
  21.  
    updateRequest = new UpdateRequest();
  22.  
    ItemEntity itemEntity = new ItemEntity();
  23.  
    itemEntity.setPrice(2.0);
  24.  
    updateRequest.doc(itemEntity);
  25.  
     
  26.  
    updateQueryBuilder = new UpdateQueryBuilder();
  27.  
    updateQueryBuilder.withId("999872");
  28.  
    updateQueryBuilder.withUpdateRequest(updateRequest);
  29.  
    updateQueryBuilder.withClass(ItemEntity.class);
  30.  
     
  31.  
    updateQuery = updateQueryBuilder.build();
  32.  
    UpdateResponse updateResponse2 = elasticsearchTemplate.update(updateQuery);
  33.  
    }
学新通
  1.  
    package org.fiend.entity;
  2.  
     
  3.  
    import org.springframework.data.annotation.Id;
  4.  
    import org.springframework.data.elasticsearch.annotations.DateFormat;
  5.  
    import org.springframework.data.elasticsearch.annotations.Document;
  6.  
    import org.springframework.data.elasticsearch.annotations.Field;
  7.  
    import org.springframework.data.elasticsearch.annotations.FieldType;
  8.  
     
  9.  
    import java.util.Date;
  10.  
     
  11.  
     
  12.  
    /**
  13.  
    * @author langpf 2019/2/27
  14.  
    */
  15.  
    // indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
  16.  
    // type类型 可以理解为表名
  17.  
    // shards:分片数量,默认5
  18.  
    // replicas:副本数量,默认1
  19.  
    // @Document(indexName = “test-smq”, type = “test”, refreshInterval = “1s”, createIndex = false)
  20.  
    @Document(indexName = "item", type = "docs", shards = 1, replicas = 0)
  21.  
    public class ItemEntity {
  22.  
    /**
  23.  
    * @Description: @Id注解必须是springframework包下的
  24.  
    * org.springframework.data.annotation.Id
  25.  
    */
  26.  
    @Id // 必须
  27.  
    // @JsonProperty("id")
  28.  
    private Long id;
  29.  
     
  30.  
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
  31.  
    private String title; //标题
  32.  
     
  33.  
    @Field(type = FieldType.Keyword)
  34.  
    private String category;// 分类
  35.  
     
  36.  
    @Field(type = FieldType.Keyword)
  37.  
    private String brand; // 品牌
  38.  
     
  39.  
    @Field(type = FieldType.Double)
  40.  
    private Double price; // 价格
  41.  
     
  42.  
    @Field(index = false, type = FieldType.Keyword)
  43.  
    private String images; // 图片地址
  44.  
     
  45.  
    // @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
  46.  
    // @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
  47.  
    // @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT 8")
  48.  
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
  49.  
    private Date myDate;
  50.  
     
  51.  
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
  52.  
    private Date myDate2;
  53.  
     
  54.  
    public ItemEntity() {
  55.  
    }
  56.  
     
  57.  
    public ItemEntity(Long id, String title, String category, String brand, Double price, String images) {
  58.  
    this.id = id;
  59.  
    this.title = title;
  60.  
    this.category = category;
  61.  
    this.brand = brand;
  62.  
    this.price = price;
  63.  
    this.images = images;
  64.  
    }
  65.  
     
  66.  
    public Long getId() {
  67.  
    return id;
  68.  
    }
  69.  
     
  70.  
    public void setId(Long id) {
  71.  
    this.id = id;
  72.  
    }
  73.  
     
  74.  
    public String getTitle() {
  75.  
    return title;
  76.  
    }
  77.  
     
  78.  
    public void setTitle(String title) {
  79.  
    this.title = title;
  80.  
    }
  81.  
     
  82.  
    public String getCategory() {
  83.  
    return category;
  84.  
    }
  85.  
     
  86.  
    public void setCategory(String category) {
  87.  
    this.category = category;
  88.  
    }
  89.  
     
  90.  
    public String getBrand() {
  91.  
    return brand;
  92.  
    }
  93.  
     
  94.  
    public void setBrand(String brand) {
  95.  
    this.brand = brand;
  96.  
    }
  97.  
     
  98.  
    public Double getPrice() {
  99.  
    return price;
  100.  
    }
  101.  
     
  102.  
    public void setPrice(Double price) {
  103.  
    this.price = price;
  104.  
    }
  105.  
     
  106.  
    public String getImages() {
  107.  
    return images;
  108.  
    }
  109.  
     
  110.  
    public void setImages(String images) {
  111.  
    this.images = images;
  112.  
    }
  113.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbgiaa
系列文章
更多 icon
同类精品
更多 icon
继续加载