【问题标题】:SDN4, Neo4j OGM @QueryResult Session.query and PagableSDN 4,Neo4j OGM @QueryResult Session.query 和 Pageable
【发布时间】:2017-05-24 03:19:49
【问题描述】:

现在由于 Neo4j OGM 中有一个开放的 GitHub 问题 https://github.com/neo4j/neo4j-ogm/issues/215,我必须使用以下解决方法才能将返回 @QueryResultorg.neo4j.ogm.session.Session.query 结果转换为此 @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);
}

现在我必须返回 Page&lt;WeightedDecision&gt; 而不是 List&lt;WeightedDecision&gt;

如何做到这一点?请帮我更改代码以便将 queryResult 转换为 Page&lt;WeightedDecision&gt;

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

【问题讨论】:

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


    【解决方案1】:

    参考这篇文章Conversion of List to Page in Spring,您可以使用org.springframework.data.domain.PageImpl 实现从结果列表到Page 实例的映射。在您的情况下,将结果映射到 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;
    

    【讨论】:

    • 感谢您的回答。是否可以将例如 new PageRequest(0, 1000) 转换为 SKIP 0 LIMIT 1000 或者我应该对 PageRequest.page, PageRequest.size 参数进行一些数学运算,然后再将其转换为相应的 Cypher SKIP 和 LIMIT ?
    • 这取决于你想要的页面。如果您唯一的任务是返回页面而不是列表,那么可以在查询中使用跳过和限制并将完整的结果数据放入页面对象中。
    • 感谢您的帮助!
    猜你喜欢
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多