SDN4,Neo4j OGM @QueryResult Session.query和Pagable

时间:2017-01-09 19:11:27

标签: cypher spring-data-neo4j spring-data-neo4j-4 neo4j-ogm

目前由于Neo4j OGM中的GitHub问题https://github.com/neo4j/neo4j-ogm/issues/215,我必须使用以下解决方法将org.neo4j.ogm.session.Session.query结果返回@QueryResult转换为此{{1}的列表}}:

Cypher查询示例:

@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); } 我必须返回List<WeightedDecision>

怎么做?请帮我更改代码,以便将queryResult转换为Page<WeightedDecision>

另外,如何在此逻辑中提供Page<WeightedDecision>

1 个答案:

答案 0 :(得分:1)

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

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

背景:据我所知,目前无法在SDN(Paging and sorting in Spring Data Neo4j 4)中对自定义查询使用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;