使用官方的“Neo4j过程模板”travers.findCoActors时调用过程时出错

n53p2ov0  于 2022-10-21  发布在  其他
关注(0)|答案(1)|浏览(148)

我正在使用这个官方的neo4j模板来了解如何编写定制程序https://github.com/neo4j-examples/neo4j-procedure-template
我已经将编译后的JAR文件加载到了一个新的4j容器(社区版本4.4.4)上的插件目录中
定制功能和聚合功能都能正常工作。但当我调用定制过程时

MATCH (n:Person)
CALL travers.findCoActors(n.name);

我收到了一个错误

Procedure call inside a query does not support naming results implicitly (name explicitly using `YIELD` instead) (line 2, column 1 (offset: 17))
"CALL travers.findCoActors(n.name);"
 ^

我知道我需要在Cypher查询中使用Year和Return,但我不确定根据Java中的函数定义,findCoActor的正确输出格式是什么。不是很熟悉所有的流API...

@Procedure(value = "travers.findCoActors", mode = Mode.READ)
@Description("traverses starting from the Person with the given name and returns all co-actors")
public Stream<CoActorRecord> findCoActors(@Name("actorName") String actorName) {

    Node actor = tx.findNodes(PERSON, "name", actorName)
            .stream()
            .findFirst()
            .orElseThrow(IllegalArgumentException::new);

    final Traverser traverse = tx.traversalDescription()
            .depthFirst()
            .evaluator(Evaluators.fromDepth(1))
            .evaluator(Evaluators.toDepth(2))
            .evaluator(Evaluators.includeIfAcceptedByAny(new PathLogger(), new LabelEvaluator(PERSON)))
            .traverse(actor);

    return StreamSupport
            .stream(traverse.spliterator(), false)
            .map(Path::endNode)
            .map(CoActorRecord::new);
}

我如何使查询起作用?

h7appiyu

h7appiyu1#

在neo4j模板的示例中,该过程返回Stream<RelationshipTypes>,其中RelationshipTypes类定义为:

public static class RelationshipTypes {
    public List<String> outgoing;
    public List<String> incoming;

    public RelationshipTypes(List<String> incoming, List<String> outgoing) {
         this.outgoing = outgoing;
         this.incoming = incoming;
    }
}

所以当使用这个过程时,我们使用YIELD incoming, outcoming,基本上是RelationshipTypes类的字段。类似地,现在,在本例中,您应该生成CoActorRecord类的字段。例如,如果节点是一个字段,则如下所示:

MATCH (n:Person)
CALL travers.findCoActors(n.name)
YIELD node
RETURN node

相关问题