Spring ElasticSearch - 如何将整个子对象存储在父对象中?

时间:2021-04-26 12:23:35

标签: java elasticsearch spring-data-elasticsearch

我正在尝试将数据插入到 elasticsearch 中。我有这样的 Java 类,

@Data
public class Common {

    @Id
    private String id;
}

@Getter @Setter
public class Student extends Common {

    private String name;
    private String phone;
    private String password;
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime createdAt;
}

@Getter @Setter
@Document(indexName = "table")
public class Table extends Common {

    @Field(type = FieldType.Auto, includeInParent = true)
    private Student student;

    private String attends;
}

我需要的是,我需要在 elasticsearch Student 索引中的 Table 对象中存储 table 数据(完整数据对象)。我不想将 Student 单独存储为另一个索引。相反,我需要将该数据也存储在该表中。我尝试了很多方法,包括nested and object field types。但他们都没有工作。它没有将整个 student 对象存储在 Table 对象中。它只是插入学生的 id

那么我如何才能做到这一点?如何将学生对象存储在表对象中。任何人都可以帮助我吗?提前致谢。

1 个答案:

答案 0 :(得分:1)

将属性定义为 @FieldType.Object

@Document(indexName = "table")
public class Table extends Common {

    @Field(type = FieldType.Object)
    private Student student;

    private String attends;
}

编辑:

我不应该在没有先尝试的情况下回答。字段定义没问题,需要为类的属性定义getter和setter方法。