Neo4j Cypher查询生成器

seb*_*tij 7 dsl fluent query-builder neo4j cypher

我一直试图遇到Neo4j的查询语言Cypher的查询构建器,理想情况下使用流畅的API.我没有找到太多,并决定花一些时间自己建一个.

到目前为止,结果是Cypher 1.9规范的流畅的API查询构建器.

在发布代码之前,我想使用StackOverflow开始讨论,看看有什么想法.

这是一个演示查询,您希望使用Cypher发送给Neo4j.

向我展示John知道谁知道谷歌软件工程师的所有人(Google公司代码假定为12345).John和将他连接到Google员工的人之间的关系强度至少应为3(假设范围为1-5).返回John的所有关系以及他们在Google上认识的人,包括这些人之间的关系.按约翰的连接名称按升序对结果进行排序,然后按关系强度按降序排序.

使用Fluent-Cypher:

Cypher
    .on(Node.named("john").with(Index.named("PERSON_NAMES").match(Key.named("name").is("John"))))
    .on(Node.named("google").with(Id.is(12345)))

    .match(Connection.named("rel1").andType("KNOWS").between("john").and("middle"))
    .match(Connection.named("rel2").andType("KNOWS").between("middle").and("googleEmployee"))
    .match(Connection.withType("WORKS_AT").from("googleEmployee").to("google"))

    .where(Are.allOfTheseTrue(Column.named("rel1.STRENGTH").isGreaterThanOrEqualTo(3)
            .and(Column.named("googleEmployee.TITLE").isEqualTo("Software Engineer"))))

    .returns(Columns.named("rel1", "middle", "rel2", "googleEmployee"))
    .orderBy(Asc.column("middle.NAME"), Desc.column("rel1.STRENGTH"))
Run Code Online (Sandbox Code Playgroud)

这会产生以下查询:

START john=node:PERSON_NAMES(name='John'),google=node(12345) MATCH john-[rel1:KNOWS]-middle,middle-[rel2:KNOWS]-googleEmployee,googleEmployee-[:WORKS_AT]->google WHERE ((rel1.STRENGTH >= '3' AND googleEmployee.TITLE = 'Software Engineer')) RETURN rel1,middle,rel2,googleEmployee ORDER BY middle.NAME ASC,rel1.STRENGTH DESC
Run Code Online (Sandbox Code Playgroud)

Dmi*_*kin 0

我喜欢你的第一个例子,你只使用文本来描述查询。说实话,第二个选项对我来说并不比构建 Cypher 查询本身容易多少。该语言非常易于使用并且有很好的文档记录。添加另一层抽象只会增加复杂性。但是,如果您找到一种方法将此自然语言请求转换为 Cypher 请求,那就太酷了:)

\n\n

另外,为什么不开始直接使用 Cypher 2.0 呢?

\n\n

最后,在这里查看: http: //github.com/noduslabs/infranodus \xe2\x80\x93 我正在解决类似的问题,但是将节点添加到数据库中,而不是查询它们。我选择使用 #hashtags 是为了让人们更容易理解他们的查询应该如何构建(因为我们已经使用了它们)。所以在你的情况下它可能会变成这样

\n\n
@show-all #people who #John :knows who :know #software-engineers :at #Google. \n\n@relationship-strength between #John and the #people who are @linked to #Google #software-engineers should be at least @3 \n\n@return @all of #John\'s @connections and the #people they :know at #Google, including the @relationships-between those #people. \n\n@sort the @results @by-name of #John\'s @connections in @ascending order and then by @relationship-strength in @descending order.\n
Run Code Online (Sandbox Code Playgroud)\n\n

(假设 #hashtags 指的是节点,@at 指的是对它们的操作)

\n\n

如果你能完成这样的事情,我认为这将是对已经易于使用的 Cypher 的更好、更有用的简化。

\n