Spring ElasticSearch - 如何将整个子对象存储在父对象中?
Spring ElasticSearch - How to store whole child object inside parent object?
我正在尝试将数据插入到 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 table
索引中的 Table
对象中存储 Student
数据(完整数据对象)。我不想将 Student 单独存储为另一个索引。相反,我需要将该数据也存储在 Table 中。我尝试了很多方法,包括 nested and object field types
。但其中 none 正在工作。它没有将整个 student
对象存储在 Table
对象中。它只是插入学生的id
。
那么我该如何实现呢?如何将学生对象存储在 table 对象中。任何人都可以帮助我吗?提前致谢。
将 属性 定义为 @FieldType.Object
:
@Document(indexName = "table")
public class Table extends Common {
@Field(type = FieldType.Object)
private Student student;
private String attends;
}
编辑:
我不应该先尝试一下再回答。字段定义就可以了,需要为类.
的属性定义getter和setter方法
我正在尝试将数据插入到 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 table
索引中的 Table
对象中存储 Student
数据(完整数据对象)。我不想将 Student 单独存储为另一个索引。相反,我需要将该数据也存储在 Table 中。我尝试了很多方法,包括 nested and object field types
。但其中 none 正在工作。它没有将整个 student
对象存储在 Table
对象中。它只是插入学生的id
。
那么我该如何实现呢?如何将学生对象存储在 table 对象中。任何人都可以帮助我吗?提前致谢。
将 属性 定义为 @FieldType.Object
:
@Document(indexName = "table")
public class Table extends Common {
@Field(type = FieldType.Object)
private Student student;
private String attends;
}
编辑:
我不应该先尝试一下再回答。字段定义就可以了,需要为类.
的属性定义getter和setter方法