Elastic search with Spring Data for Reactive Repository - Deleting based on nested attributes

Viewed 109

I am using Spring data for Elastic Search and am using the ReactiveCrudRepository for stuff like finding and deleting. I noticed that with attributes that are in root and are simple objects, the deletion works (deleteByAttributeName). However if I have nested objects then it does not work.

Here's my entities

Book

@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;
}

Stats

@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
}

Here is what I have tried and is working and not working I used ReactiveCrudRepository to delete documents in index. For all the regular fields on Book Level like id or with id and publishDate deletion works perfectly. As soon as I use embedded object like Stats, it stops working. I see the documents and the stats that I am sending match atleast visually but never finds or deletes them. I tried to use EqualsAndHashcode in the Stats assuming maybe iternally somehow does not consider equal for some reason. I also tried changing double data type to int, because on looking at the elastic search document, I see that average review if whole number like 3 is save as 3 but when we send it from Java, i see in the debug 3 being shown as 3.0, so I was doubting if that is the case, but does not seem so. Even changing the datatype to int deletion does not work.

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);
}

Any help will be appreciated

1 Answers

Have you verified that your Elasticsearch index mapping matches your Spring Data annotations?

Verify that the index mapping defines the stats field as a nested field type.

If not, then try changing your Spring annotation to:

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