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以便将返回@QueryResult的结果转换为此@QueryResult的列表:

密码查询示例:

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

现在我必须返回Page<WeightedDecision>而不是List<WeightedDecision>

怎么做?请帮助我更改代码,以便将查询结果转换为Page<WeightedDecision>

另外,如何在此逻辑中提供countQuery

参考这篇文章 在 Spring 中将列表转换为页面,您可以使用 org.springframework.data.domain.PageImpl 实现将结果列表映射到Page实例。在您的情况下,将结果映射到列表后,您可以执行以下操作:

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

背景:据我所知,目前无法在SDN中的自定义查询上使用Pageable(Spring Data Neo4j 4中的分页和排序)。因此,您必须自己进行映射。

你的第二个问题违背了计数逻辑。在我看来,您必须根据要计数的内容再次调用查询并进行更改。例如,要计算节点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;

最新更新