【问题标题】:Elastic search with Spring Data for Reactive Repository - Deleting based on nested attributes使用 Spring Data 进行 Reactive Repository 的弹性搜索 - 基于嵌套属性进行删除
【发布时间】:2021-10-22 04:22:00
【问题描述】:

我将 Spring 数据用于 Elastic Search,并使用 ReactiveCrudRepository 进行查找和删除等操作。我注意到对于根中的属性并且是简单对象,删除工作(deleteByAttributeName)。但是,如果我有嵌套对象,则它不起作用。

这是我的实体

图书

@Data
@TypeAlias("book")
@Document(indexName = "book")
public class EsBook{
  @Field(type = FieldType.Long)
  private Long id;

  @Field(type = FieldType.Nested)
  private EsStats stats;

  @Field(type = FieldType.Date, format = DateFormat.date)
  private LocalDate publishDate;
}

统计数据

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class EsStats{
  @Field(type = FieldType.Double)
  private Double averageRating;

  @Field(type = FieldType.Integer)
  private Double totalRatings;

  @Field(type = FieldType.Keyword)
  private String category; //this can be null
}

这是我尝试过的,有效的,无效的 我使用 ReactiveCrudRepository 删除索引中的文档。对于书籍级别的所有常规字段,例如 id 或带有 id 和 publishDate 的删除工作完美。一旦我使用像 Stats 这样的嵌入式对象,它就会停止工作。我看到我发送的文档和统计数据至少在视觉上匹配,但从未找到或删除它们。 我试图在 Stats 中使用 EqualsAndHashcode 假设可能由于某种原因在某种程度上不认为相等。我还尝试将 double 数据类型更改为 int,因为在查看弹性搜索文档时,如果整数(如 3)另存为 3,我会看到平均评论,但是当我们从 Java 发送它时,我在调试 3 中看到显示为3.0,所以我怀疑是不是这样,但似乎不是这样。即使将数据类型更改为 int 删除也不起作用。

public interface ReactiveBookRepository extends ReactiveCrudRepository<EsBook, String> {
  Mono<Void> deleteById(long id); //working

  Mono<Void> deleteByIdAndPublishDate(long id, LocalDate publishDate); //Nor working

  Mono<Void> deleteByIdAndStats(long id, LocalDate startDate);
}

任何帮助将不胜感激

【问题讨论】:

    标签: elasticsearch spring-data spring-data-elasticsearch


    【解决方案1】:

    您是否已验证您的 Elasticsearch 索引映射与您的 Spring Data 注释匹配?

    验证索引映射是否将stats 字段定义为nested field type。

    如果没有,请尝试将 Spring 注释更改为:

    @Field(type = FieldType.Object)
    private EsStats stats;
    

    【讨论】:

    • 我更改了 FieldType.Object 仍然无法正常工作。如果我将所有内容展平,那么一切正常
    猜你喜欢
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多