连接类型实现
Spring Data Elasticsearch 支持 Join 数据类型,用于创建相应的索引映射并存储相关信息。
准备数据
要让一个实体用于父子连接关系,它必须有一个 JoinField 类型的属性,并且该属性必须加注解。假设我们有一个 Statement 实体,其中一条陈述可以是一个 question(问题)、一个 answer(回答)、一条 comment(评论)或一个 vote(投票)(本例中还展示了一个 Builder,它不是必需的,但后面的示例代码会用到):
@Document(indexName = "statements")
@Routing("routing") 1
public class Statement {
@Id
private String id;
@Field(type = FieldType.Text)
private String text;
@Field(type = FieldType.Keyword)
private String routing;
@JoinTypeRelations(
relations =
{
@JoinTypeRelation(parent = "question", children = {"answer", "comment"}), 2
@JoinTypeRelation(parent = "answer", children = "vote") 3
}
)
private JoinField<String> relation; 4
private Statement() {
}
public static StatementBuilder builder() {
return new StatementBuilder();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRouting() {
return routing;
}
public void setRouting(String routing) {
this.routing = routing;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public JoinField<String> getRelation() {
return relation;
}
public void setRelation(JoinField<String> relation) {
this.relation = relation;
}
public static final class StatementBuilder {
private String id;
private String text;
private String routing;
private JoinField<String> relation;
private StatementBuilder() {
}
public StatementBuilder withId(String id) {
this.id = id;
return this;
}
public StatementBuilder withRouting(String routing) {
this.routing = routing;
return this;
}
public StatementBuilder withText(String text) {
this.text = text;
return this;
}
public StatementBuilder withRelation(JoinField<String> relation) {
this.relation = relation;
return this;
}
public Statement build() {
Statement statement = new Statement();
statement.setId(id);
statement.setRouting(routing);
statement.setText(text);
statement.setRelation(relation);
return statement;
}
}
}
有关路由的相关信息,请参阅路由值
一个问题可以有回答和评论
一个回答可以有投票
JoinField属性用于将关系的名称(question、answer、comment 或 vote)与父 id 组合在一起。泛型类型必须与@Id注解的属性相同。
Spring Data Elasticsearch 将为此类构建以下映射:
{
"statements": {
"mappings": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"routing": {
"type": "keyword"
},
"relation": {
"type": "join",
"eager_global_ordinals": true,
"relations": {
"question": [
"answer",
"comment"
],
"answer": "vote"
}
},
"text": {
"type": "text"
}
}
}
}
}
存储数据
假设有了此类的仓库,以下代码插入一个问题、两个回答、一条评论和一个投票:
void init() {
repository.deleteAll();
Statement savedWeather = repository.save(
Statement.builder()
.withText("How is the weather?")
.withRelation(new JoinField<>("question")) 1
.build());
Statement sunnyAnswer = repository.save(
Statement.builder()
.withText("sunny")
.withRelation(new JoinField<>("answer", savedWeather.getId())) 2
.build());
repository.save(
Statement.builder()
.withText("rainy")
.withRelation(new JoinField<>("answer", savedWeather.getId())) 3
.build());
repository.save(
Statement.builder()
.withText("I don't like the rain")
.withRelation(new JoinField<>("comment", savedWeather.getId())) 4
.build());
repository.save(
Statement.builder()
.withText("+1 for the sun")
.withRouting(savedWeather.getId())
.withRelation(new JoinField<>("vote", sunnyAnswer.getId())) 5
.build());
}
创建一条问题陈述
该问题的第一个回答
第二个回答
对该问题的一条评论
对第一个回答的投票,这需要将路由设置为 weather 文档的路由,请参阅路由值。
检索数据
目前必须使用原生查询来查询数据,因此标准仓库方法不提供支持。可以改用自定义仓库实现。
以下代码作为示例,展示了如何使用 ElasticsearchOperations 实例检索所有具有 vote 的条目(这些条目必须是 answers,因为只有回答才能有投票):
SearchHits<Statement> hasVotes() {
Query query = NativeQuery.builder()
.withQuery(co.elastic.clients.elasticsearch._types.query_dsl.Query.of(qb -> qb
.hasChild(hc -> hc
.type("answer")
.queryName("vote")
.query(matchAllQueryAsQuery())
.scoreMode(ChildScoreMode.None)
)))
.build();
return operations.search(query, Statement.class);
}