此版本仍在开发中,尚未视为稳定版。如需最新稳定版本,请使用 Spring Data Elasticsearch 6.0.4spring-doc.cadn.net.cn

Join-Type 实现

Spring Data Elasticsearch 支持Join 数据类型,用于创建相应的索引映射并存储相关信息。spring-doc.cadn.net.cn

设置数据

要使一个实体能够用于父子连接关系,它必须拥有一个类型为 JoinField 且已添加注解的属性。 假设我们有一个 Statement 实体,其中语句可以是问题答案评论投票(本示例中也展示了构建器,虽然并非必需,但将在后续示例代码中使用):spring-doc.cadn.net.cn

@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;
        }
    }
}
1 有关路由相关信息,请参阅 路由值
2 一个问题可以包含回答和评论
3 一个答案可以有投票
4 JoinField 属性用于将关系的名称(问题答案评论投票)与父 ID 组合起来。 泛型类型必须与标注为 @Id 的属性相同。

Spring Data Elasticsearch 将为该类构建以下映射:spring-doc.cadn.net.cn

{
  "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"
        }
      }
    }
  }
}

存储数据

给定该类的存储库,以下代码将插入一个问题、两个答案、一条评论和一个投票:spring-doc.cadn.net.cn

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());
}
1 创建一个问题陈述
2 问题的第一个答案
3 第二个答案
4 对问题的评论
5 为第一个回答投一票,这需要将路由设置为天气文档,请参阅 路由值

正在检索数据

目前必须使用原生查询来检索数据,因此标准仓库方法暂不支持此功能。可以改用 自定义仓库实现spring-doc.cadn.net.cn

以下代码作为一个示例,展示了如何使用 ElasticsearchOperations 实例检索所有具有投票的条目(这些必须是回答,因为只有回答才能拥有投票):spring-doc.cadn.net.cn

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);
}