puf*_*bun 5 java spring elasticsearch spring-data spring-data-elasticsearch
在当前项目中,我正在使用 Elasticsearch Spring Data (3.0.9) 和 ES 5.5.0,并且我正在研究父子关系的使用(更具体地说,我正在测试哪种方法[在性能方面]更好)使用 - 嵌套对象或父子关系]。这是我所得到的
父实体:
@Document(indexName = "testIndex", type="parentItem", createIndex = false)
public class ParentItem {
@Id
private String id;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
子实体:
@Document(indexName = "testIndex", type="childItem", createIndex = false)
public class ChildItem {
@Id
private String id;
@Field(type = FieldType.Keyword, store = true)
@Parent(type = "parentItem")
private String parentId;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
然后 ofc 我有简单的存储库类(没有主体只是扩展ElasticsearchRepository),我只是创建父子对,例如
ParentItem parent = createParent(); // creating parent instance
ChildItem child = createChild(); // creating child instance
child.setParentId(parent.getId());
Run Code Online (Sandbox Code Playgroud)
使用存储库保存/索引后,它被存储到 ES 节点 - 很棒:)
现在的问题是:Spring Data 中是否有可能将子对象保留在父实体内部,并在获取父实体期间急切地加载它,例如
@Document(indexName = "testIndex", type="parentItem", createIndex = false)
public class ParentItem {
@Id
private String id;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// can I do anything like this?
// @WhatAnnotation?
private ChildItem child;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
或者另一种方式 - 急切地“加入”父实例到子实例?
@Document(indexName = "testIndex", type="childItem", createIndex = false)
public class ChildItem {
@Id
private String id;
@Field(type = FieldType.Keyword, store = true)
@Parent(type = "parentItem")
private String parentId;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// can I do anything like this?
// @WhatAnnotation?
private ParentItem parent;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
Spring Data中有类似的东西吗?还是我需要实现自己的机制?
另一个问题是,在保存这些文档期间,我只需要使用存储库对 ES 进行两次“调用”。所以回到创建示例 - 我需要进一步做
ParentItem parent = createParent(); // creating parent instance
ChildItem child = createChild(); // creating child instance
child.setParentId(parent.getId());
parentRepository.index(parent);
childRepository.index(child);
Run Code Online (Sandbox Code Playgroud)
这意味着它不能在一笔交易中完成。这对于性能来说似乎不太好?
ES Spring Data 是否支持类似的东西?说实话,官方文档并不是很有帮助 - 可用的示例也没有显示我刚刚实现的更多内容:(
| 归档时间: |
|
| 查看次数: |
1188 次 |
| 最近记录: |