从neo4J StatementResult中提取关系

时间:2017-06-23 08:17:28

标签: java neo4j cypher

如何从StatementResult中提取关系?

目前我有这样的事情:

while (results.hasNext()) {
        Record record = results.next();
        try {
            if (record.get(0).hasType(TYPE_SYSTEM.NODE())){
                Node node = record.get(0).asNode();
                //System.out.println(node.get("name") + ": " + node.get("guid"));

                // Add block
                if (node.hasLabel(configuration.getBlock())) {
                    Block block = Block.fromRecord(node);
                    blocks.addBlock(block);
                } else
                // Add property
                if (node.hasLabel(configuration.getProp())) {
                    Property property = Property.fromRecord(node);
                    String guid = property.getGuid();
                    Block block = blocks.getBlock(guid);
                    block.addProperty(property);
                } else
                // Add output
                if (node.hasLabel(configuration.getOutput())) {
                    Output output = Output.fromRecord(node);
                    String guid = output.getGuid();
                    Block block = blocks.getBlock(guid);
                    block.addOutput(output);
                } else
                // Add input
                if (node.hasLabel(configuration.getInput())) {
                    Input input = Input.fromRecord(node);
                    inputs.add(input);
                    String guid = input.getGuid();
                }

            }

我的原始查询是这样的:

MATCH (start:Block{name:'block_3'})
CALL apoc.path.subgraphNodes(start, {relationshipFilter:'PART_OF|hasOutPort>|connectsTo>|<hasInPort'}) YIELD node as block
WITH
  block,
  [(block)-[:PART_OF]->(prop) | prop] as properties,
  [(block)-[:hasOutPort]->(output) | output] as outputs,
  [(block)-[:hasInPort]->(input) | input] as inputs
RETURN block, properties, outputs, inputs

我需要所有“connectionsTo”关系

希望这是有道理的。

1 个答案:

答案 0 :(得分:0)

首先,您需要指定这些关系&#39;别名并像节点一样返回它们。简化示例:

MATCH (a:Block)-[r:PART_OF]->(b:Block) RETURN a, r, b

这样,您的Record实例将包含a,r和b的数据(值实例)。在提取逻辑中,您可以执行以下操作来获取关系数据:

Relationship r = record.get("r").asRelationship();