SDN 4、Neo4j OGM @QueryResult Session.query 和 Pageable

SDN4, Neo4j OGM @QueryResult Session.query and Pagable

现在由于 Neo4j OGM 中一个未解决的 GitHub 问题 https://github.com/neo4j/neo4j-ogm/issues/215,我必须使用以下解决方法才能将 org.neo4j.ogm.session.Session.query 结果转换为 returns @QueryResult 进入列表 @QueryResult:

示例 Cypher 查询:

 MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User) WHERE id(parentD) = {decisionId}  OPTIONAL MATCH (childD)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(c) IN {criteriaIds} WITH childD, ru, u, vg.avgVotesWeight as weight  OPTIONAL MATCH (childD)<-[:SET_FOR]->(sortValue375:Value)-[:SET_ON]->(sortCharacteristic375:Characteristic) WHERE id(sortCharacteristic375) = 375 RETURN childD AS decision, ru, u, toFloat(sum(weight)) as weight, sortValue375 ORDER BY weight DESC, sortValue375.value DESC, childD.createDate DESC SKIP 0 LIMIT 1000

解决方法:

@QueryResult
public class WeightedDecision {

    private Decision decision;

    private Double weight;

...
}

Result queryResult = session.query(characteristicCypher, parameters);

List<WeightedDecision> weightedDecisions = new ArrayList<>();

for (Map<String, Object> result : queryResult) {
    WeightedDecision weightedDecision = new WeightedDecision();
    weightedDecision.setDecision((Decision) result.get("decision"));
    weightedDecision.setWeight((double) result.get("weight"));
    weightedDecisions.add(weightedDecision);
}

现在我必须 return Page<WeightedDecision>

而不是 List<WeightedDecision>

如何实现?请帮我更改代码,以便将 queryResult 翻译成 Page<WeightedDecision>

另外,这个逻辑中如何提供一个countQuery

参考此 post Conversion of List to Page in Spring,您可以使用 org.springframework.data.domain.PageImpl 实现从结果列表到 Page 实例的映射。在您的情况下,将结果映射到列表后,您可以执行以下操作:

PageRequest pageable = new PageRequest(0, 2);
Page<WeightedDecision> page = new PageImpl<>(weightedDecisions, pageable, weightedDecisions.size());

背景:据我所知目前无法在 SDN 中对自定义查询使用 Pageable ()。因此,您必须自己进行映射。

你的第二个问题是关于计数逻辑的。在我看来,你必须根据你想要计算的内容再次调用你的查询。例如计算 childD 个节点:

MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User) WHERE id(parentD) = {decisionId}  
OPTIONAL MATCH (childD)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(c) IN {criteriaIds} WITH childD, ru, u, vg.avgVotesWeight as weight  
OPTIONAL MATCH (childD)<-[:SET_FOR]->(sortValue375:Value)-[:SET_ON]->(sortCharacteristic375:Characteristic) WHERE id(sortCharacteristic375) = 375 RETURN count(childD) AS result;