Home > database >  Spring data elastic nested relation between document
Spring data elastic nested relation between document

Time:09-17

I have a simple springboot app with three entities with one to many relation ,That I want to save them as document using elasticsearch ,reading the documentation i choose the nested object mapping here's my entities :

    Category --*> product --*> reviews 

    @Data
    @Builder
    @Document(indexName = "product-store")
    public class Category {
        private UUID id ;
        private String name ;
        private String description ;
        @Field(type = FieldType.Nested, includeInParent = true)
        private List<Product> products ;
    }

@Data
@Builder
@ToString
@Document(indexName = "product-store")
public class Product  {

    @Id
    private UUID id ;
    private String name ;
    private String description ;
    private String ImageUrl ;
    private Double price;

    @Field(type = FieldType.Nested, includeInParent = true)
    private List<ProductExperience> productExperience ;
}

@Data
@Builder
@Document(indexName = "product-store")
public class ProductExperience {
    @Id
    private UUID id ;
    private String reviewTitle;
    private String reviewBody;
    private int rate ;
}

I'm working with spring data elastic I create a repository for each entity ,I'm little confused what order should in save the three entities so they will be saved in the same elastic document :

Is this the correct order using the repositories :

  1. create and save category
  2. create and save product it list of categories
  3. update the product with review

CodePudding user response:

Only the top level class - Category - must have the @Document annotation.

ad 1) You then save category objects that contain all their products and these contain all the reviews.

ad 2) a product does not have a list of categories, it just has one. Or you have to add it to different category objects, then you will have to store it redundantly multiple times with every category

ad 3) find the category object(s) that contains the product, update the contained product and then save the category object(s)

Elasticsearch is not a relational DB system, so it might not be the bets solution to store your data.

  • Related