过滤长距离路径中的节点属性 [英] Filtering on node property in long distance path
问题描述
从人们推荐的项目开始,有没有一种方法可以遍历长距离的友谊关系来过滤一个人的财产,例如年龄?
例如,在以下密码查询中,我只想遍历人+ 18y的节点,而不仅仅是过滤p的年龄。
Starting from an item recommended by people, is there a way to filter on a person property, such as age, while traversing at long distance friendship relationships? For instance in the following cypher query, I'd like to only traverse nodes of person +18y, not just filtering on p's age.
MATCH path = (:Fruit {Name: 'Apple'}) <-[:LIKES]- (:Person) -[:FRIENDOF*1..5]- (p:Person) -[:LIKES]-> (:Device {Name: 'iPhone'}) return path
UPDATE 更多详细信息:
在此示例中,该图包含分子和支架,这些分子和支架递归剥离了侧链和环。
In this exemple the graph contains molecules and scaffolds resulting of stripping off side chains and rings recursively.
MATCH path=(:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s:Scaffold) <-[:hasSubstructure]- (:Molecule)
RETURN path
分子显示在蓝色和红色的脚手架以其环数命名。如我们所见,从mol25开始,我们有一个在两个边缘距离处具有单个环的结构。
Molecules are shown in blue and scaffold in red named after their number of rings. As we can see, starting from mol25, we have a structure with a single ring at two edges distance.
因此,我对响铃次数进行了过滤:
Hence, I filter on the number of rings:
MATCH p=(:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s:Scaffold) <-[:hasSubstructure]- (:Molecule)
WHERE s.Num_Rings > 1
RETURN p
我们得到相同的图片!仍然存在带有单个环的支架。
We get the same picture! The scaffold bearing a single ring is still present. Worse is to come!
现在,我们从以前的查询中提取出不同的支架节点,首先不对环的数量进行过滤。
Now we extract distinct scaffold nodes from using the previous queries, first without filtering on number of rings.
MATCH p=(:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s:Scaffold) <-[:hasSubstructure]- (:Molecule)
RETURN distinct id(s) as ID, s.Name, s.Size, s.Num_Rings
$ b $的形式返回不同的ID b
我们期望返回7个脚手架节点...我们只会得到4个。对环数进行过滤会返回相同的结果。
We expect 7 scaffold nodes in return... we only get 4. Filtering on number of rings returns the same results.
MATCH p=(:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s:Scaffold) <-[:hasSubstructure]- (:Molecule)
WHERE s.Num_Rings > 1
RETURN DISTINCT id(s), s.Name, s.Size, s.Num_Rings
从不同于mol25的另一种分子开始,并使用UNWIND从路径中提取节点,它会正确返回所有支架节点,并分别过滤一个没有和带有WHERE条件的节点。
Starting from another molecule than mol25 and using UNWIND to extract nodes from the path, it properly returns all scaffold nodes and filtered one without and with the WHERE condition respectively.
MATCH p=(m:Molecule) -[:hasSubstructure*1..4]-> (s:Scaffold)
WHERE id(m)=9 and s.Num_Rings > 1
UNWIND nodes(p) as n
RETURN id(n), n.Name, n.Num_Rings, n.Size, n.Smiles
但是,再次返回路径p时,当限制-[:hasSubstructure * 1..4]->方向时,我们会得到正确的图形,但并非没有方向限制-[:hasSubstructure * 1..4]-。
But again, returning the path p instead we get the correct graph when restricting on the -[:hasSubstructure*1..4]-> direction, but not without the direction restriction -[:hasSubstructure*1..4]-.
如果有人愿意帮助解决这个问题,我将不胜感激。
I'd appreciate if anyone would help sorting this out.
推荐答案
这里有几点:
因此,我对振铃数进行了过滤:
Hence, I filter on the number of rings:
MATCH p=(:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s:Scaffold) <-[:hasSubstructure]- (:Molecule)
WHERE s.Num_Rings > 1
RETURN p
不要忘记指定可变长度路径,这完全是从 mol25
到 scaffold4
到 scaffold1
的路径可能到另一个 scaffold4
,然后到另一个分子
将是有效的,因为边缘的末端是一个分子并且该节点之前具有 Num_Rings> 1
,独立于Num_Rings的其他中间节点的值。
Don't forget that by specifying variable length paths, it is totally possible that the path going from mol25
to scaffold4
to scaffold1
to other scaffold4
and then to other molecule
will be valid, as the end of the edge is a molecule and the node before has a Num_Rings > 1
, indepedently of the other intermediate nodes values of Num_Rings.
因此,如果您想限制支架,则中间节点可以是任何东西值> 1,则应使用 ALL
子句:
So the intermediate nodes can be anything, if you want to restrict to the scaffolds with a value > 1, you should use a ALL
clause :
此查询似乎可以实现以下目的:以下Neo4j控制台: http://console.neo4j.org/r/7f5hc4 (属性值有点不同,但是是..)
This query seems to do the trick based with the following Neo4j console : http://console.neo4j.org/r/7f5hc4 (property values a bit different but yeah..)
MATCH p=(m:Molecule { id: 566 })-[r:HAS_SUBSTRUCTURE*1..3]-(s:Scaffold)<-[:HAS_SUBSTRUCTURE]-(:Molecule)
USING INDEX m:Molecule(id)
WHERE ALL (x IN tail(nodes(p))[0..size(nodes(p))-2]
WHERE 'Scaffold' IN labels(x) AND x.rings > 29)
RETURN p
说明:
- 匹配类似的路径您确实
- 使用索引,通过对PROFILE的测试,我发现未使用Molecular(id)索引,因此我帮助Cypher
-
tail(nodes(p))[0..size(nodes(p))-2]
是两个分子之间的路径中的节点,请注意,nodes(p)[1..size(nodes(p))-2]
是等效的 - 我断言这些节点的标签支架为
ALL
,Num_Rings值> 29(此处为29,因为它是从Graphgen中随机抽取的)
- Match the path like you do
- USING INDEX, by my tests with PROFILE, I saw the molecule(id) index was not used, so I help Cypher a bit
- The
tail(nodes(p))[0..size(nodes(p))-2]
are the nodes in the path between the two molecules, note thatnodes(p)[1..size(nodes(p))-2]
is equivalent - I assert that these nodes have
ALL
the label scaffold and a Num_Rings value > 29 (here 29 because it is randomized from Graphgen)
这篇关于过滤长距离路径中的节点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!