简体   繁体   English

Neo4J密码查询“继承的”术语

[英]Neo4J cypher query for “inherited” terms

Using the community edition of neo4j v2.1.7, I am creating nodes to represent licenses. 使用neo4j v2.1.7的社区版本,我正在创建代表许可证的节点。

Each license has terms and can "inherit" terms from another license. 每个许可证都有条款,可以从其他许可证“继承”条款。 Terms local to a license shadow the same terms in the inherited license. 许可证本地的术语在继承的许可证中带有相同的术语。

I am attempting to write a cypher query that will match on local and inherited terms. 我正在尝试编写一个对本地和继承术语都匹配的密码查询。

This is easier to demonstrate with some code. 使用某些代码更容易演示。

I create a license that inherits from another license: 我创建了一个从另一个许可证继承的许可证:

create 
(l1:License)-[:inherits]->(l2:License),
(l1)-[:term]->(:Foo{value:"A"}),
(l1)-[:term]->(:Lar{value:"C"}),
(l2)-[:term]->(:Lar{value:"D"}),
(l2)-[:term]->(:Bar{value:"B"})

I want a query that will find licenses that have or inherit the following terms: Foo = A, Bar = B, Lar = C 我想要一个查询,以查找具有或继承以下术语的许可证:Foo = A,Bar = B,Lar = C

So l1 would match since it has the right values for Lar and Foo and inherits Bar with the right value. 所以l1将匹配,因为它具有适合Lar和Foo的值,并继承了具有正确值的Bar。

l2 wouldn't match since it does not have the right Lar and is missing Foo. l2不匹配,因为它没有合适的Lar,并且缺少Foo。

I tried the following, but it seems cumbersome and has at least two issues: 我尝试了以下操作,但看起来很麻烦,并且至少有两个问题:

  1. I added "optional match (l1)-[:inherits]->(l2:License)" because I wanted to match on licenses that did not inherit other license terms. 我添加了“可选匹配项(l1)-[:inherits]->(l2:License)”,因为我想匹配不继承其他许可条款的许可。 However, for some reason I don't grok, the optional match gives me both License nodes in the graph. 但是,由于某些原因我不满意,可选匹配为我提供了图中的两个License节点。

  2. How can I test whether a property is in a range? 如何测试属性是否在范围内? In other words, what if a License has a property that is a date like "20150101" and I want to test for values between 20140101 and 20140101? 换句话说,如果许可证的日期为“ 20150101”之类的属性,并且我要测试20140101和20140101之间的值怎么办? Since my "where" is testing the existence of paths, I don't see how to test whether a property is greater than or less than another value. 由于我的“ where”正在测试路径的存在,因此我看不到如何测试某个属性是大于还是小于另一个值。

Incorrect cypher query: 不正确的密码查询:

match (l1:License)
optional match (l1)-[:inherits]->(l2:License)
where 
( 
   (l1)-[:term]->(:Foo{value:"A"}) OR 
   (
       not((l1)-[:term]->(:Foo{value:"A"}))
       and
       (l2)-[:term]->(:Foo{value:"A"})
   )
)
AND
(
   (l1)-[:term]->(:Bar{value:"B"}) or 
   (   
      not((l1)-[:term]->(:Bar{value:"B"}))
      and 
      (l2)-[:term]->(:Bar{value:"B"})
    )
)
AND
(
   (l1)-[:term]->(:Lar{value:"C"}) or 
   (
      not((l1)-[:term]->(:Lar{value:"C"})) and
      (l2)-[:term]->(:Lar{value:"C"})
   )
)
return count(l1)

Thanks in advance! 提前致谢!

I think you still think too much in relational terms, your query looks like a lot of joins. 我认为您在关系方面仍然考虑过多,您的查询看起来像很多联接。

I think something like this will suffice. 我认为这样就足够了。

// find the 3 terms
MATCH (t1:Foo {value:"A"}),(t2:Bar {value:"B"}), (t3:Lar {value:"C"})
UNWIND [t1,t2,t3] as term
// use them as starting point
MATCH path = (l:License)-[:INHERITS*0..]->(l2)-[:TERM]->(term)
RETURN l,l2,term, path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM