如果属性的数量大于n,则从Neo4j中的高度连通图返回

编程入门 行业动态 更新时间:2024-10-21 15:32:49
本文介绍了如果属性的数量大于n,则从Neo4j中的高度连通图返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题是我以前问过的一个问题的直接扩展。

我想返回这些不相交图中的任一个的完整路径,而在所述图中的任何位置计数 SomeProperty 大于2。

我认为这是一个常见的简单问题。例如,假设你有两个不相关的家庭,并且有人说:让我和家人在一起,有两个左撇子的人。

这个超级智能#cybersam推荐用于这个问题的简单化身,它的用法如下:

 ...如果图形更像一条直线,并且没有该集合中的每个节点都与每个其他节点相关。 

我认为#cybersam的查询无法处理这个更复杂图形的原因是因为没有终端节点。

(另一个哲学旁注):我开始想出一个理论,即图中的密集复杂关系提出了组合问题,表现和查询,我认为这可能是由于Cypher在查询时使用双向性?)

这是我的数据。任何建议是感谢和帮助我爬上学习曲线。

// match(n)detach delete n; CREATE(albert:person {gender:'Male',name:'Albert',SomeProperty:'Yes'}) CREATE(annie:person {gender:'Female',name :'Annie',SomeProperty:'No'}) CREATE(adrian:person {gender:'Female',name:'Adrian',SomeProperty:'No'}) CREATE (albert) - [:RELATED_TO] - >(annie) CREATE(annie) - [:RELATED_TO] - >(albert) CREATE(annie) - [:RELATED_TO] - >( adrian) CREATE(adrian) - [:RELATED_TO] - >(annie) CREATE(albert) - [:RELATED_TO] - >(adrian) CREATE(adrian) - [ :($)$($) $ b CREATE(bill:person {gender:'Male',name:'Bill',SomeProperty:'Yes'}) CREATE(barb:person {gender:'Female',name:'Barb',SomeProperty:'Yes'}) CREATE(barry:person {gender:'Male',name:'Barry',SomeProperty:'是')) CREATE(bart:person {gender:'Male',name:'Bart',SomeProperty:'No'}) CREATE(bartholemu:person {gender:'Male ',name:'Bartholemu',SomeProperty:'No'}) CREATE(bill) - [:RELATED_TO] - >(barb) CREATE(barb) - [:RELATED_TO ] - >(bill) CREATE(barb) - [:RELATED_TO] - >(barry) CREATE(barry) - [:RELATED_TO] - >(barb) CREATE (bry) - [:RELATED_TO] - > bartholemu) CREATE(bartholemu) - [:RELATED_TO] - >(bart) CREATE(bill) - [:RELATED_TO] - >(bartholemu) CREATE(bartholemu) - [ :RELATED_TO] - >(bill)

解决方案

是关于人员的家庭,那么最简单的解决方法是为每个关系组添加一个:Family节点,如下所示:

create (a:person {name:Adrian}) - [:RELATED_TO *] - >(b:person) merge(f:Family)< [:FAMILY] - (a)合并(f:家庭)< - [:家庭] - (b)

将Adrian替换为Barry以创建第二个家庭组。

这给了你一个中心:每个家庭组的家庭节点。然后,您可以选择足够的家庭组:person.SomeProperty =是家庭成员喜欢这样:

// //查找具有2个或更多个人的家庭:person.SomeProperty =是匹配p =(f:家庭)< - [:家庭] - (psn:人)其中psn.SomeProperty =是 with f,count(psn)as cnt where cnt> 2 //获取家庭成员 match(a:person)< - [r1:RELATED_TO] - (b:person) - [r2:RELATED_TO *] - >( c)其中(a) - [:FAMILY] - (f)和a = c //获取循环中的所有节点 //报告第一个记录哪些有两个 //家庭成员和所有关系返回a,r1,b,r2 限制1

This question is a direct extension of a question I asked previously here (and and even earlier version here).

Say I have a graph database that looks like this:

Just like the previous questions I asked, the only really interesting thing about this is that SomeProperty can be 'Yes' or 'No'.

In the top row, 1 of the three nodes has a 'Yes' for this property.

On the bottom row, 3 nodes of the five nodes have a 'Yes' for this property.

(Slight philosophical sidenote: I'm starting to suspect that this is a bad graph schema. Why? Because, within each set of nodes, each node is in connected to every other node. I'm not worried about the fact that there are two groups of nodes, but the fact that when I populate this graph, I get talkback that says, 'Returned 530 rows.' I think this means actually created 530 subpaths within the graph structure and this seems like overkill.)

Anyway, the problem I'm trying to solve is pretty much the same as the problem I was trying to solve in the earlier, simpler, more linear context here.

I want to return the full path of either of these disjoint graphs, whereas anywhere within said graph the count the occurrences of SomeProperty is greater than 2.

I would think this is a common, simple problem. For example, say you had two unrelated families, and someone says, "Show me with family has more than 2 left handed people."

The super smart #cybersam recommended for the simpler incarnation of this problem, something along the lines of:

MATCH p=(a:person)-[:RELATED_TO*]->(b:person) WHERE NOT ()-[:RELATED_TO]->(a) AND NOT (b)-[:RELATED_TO]->() AND 2 < REDUCE(s = 0, x IN NODES(p) | CASE WHEN x. SomeProperty = 'Yes' THEN s + 1 ELSE s END) RETURN p;

...which works great if the graph resembles more of a straight line, and doesn't have each node in the set related to each other node.

I think the reason why #cybersam's query won't handle this more complex graph is because there is no terminal node.

(Another philosophical sidenote: I'm starting to come up with a theories that dense, intricate relationships in a graph pose combinatorial problems, with performance as well as querying. I think this might be due to the bidirectionality used by Cypher when querying?)

Here's my data. Any advice is appreciate and thanks for helping me climb the learning curve.

// match (n) detach delete n; CREATE (albert:person {gender: 'Male', name: 'Albert', SomeProperty: 'Yes'}) CREATE (annie:person {gender: 'Female', name: 'Annie', SomeProperty: 'No'}) CREATE (adrian:person {gender: 'Female', name: 'Adrian', SomeProperty: 'No'}) CREATE (albert)-[:RELATED_TO]->(annie) CREATE (annie)-[:RELATED_TO]->(albert) CREATE (annie)-[:RELATED_TO]->(adrian) CREATE (adrian)-[:RELATED_TO]->(annie) CREATE (albert)-[:RELATED_TO]->(adrian) CREATE (adrian)-[:RELATED_TO]->(albert) CREATE (bill:person {gender: 'Male', name: 'Bill', SomeProperty: 'Yes'}) CREATE (barb:person {gender: 'Female', name: 'Barb', SomeProperty: 'Yes'}) CREATE (barry:person {gender: 'Male', name: 'Barry', SomeProperty: 'Yes'}) CREATE (bart:person {gender: 'Male', name: 'Bart', SomeProperty: 'No'}) CREATE (bartholemu:person {gender: 'Male', name: 'Bartholemu', SomeProperty: 'No'}) CREATE (bill)-[:RELATED_TO]->(barb) CREATE (barb)-[:RELATED_TO]->(bill) CREATE (barb)-[:RELATED_TO]->(barry) CREATE (barry)-[:RELATED_TO]->(barb) CREATE (barry)-[:RELATED_TO]->(bart) CREATE (bart)-[:RELATED_TO]->(barry) CREATE (bart)-[:RELATED_TO]->(bartholemu) CREATE (bartholemu)-[:RELATED_TO]->(bart) CREATE (bill)-[:RELATED_TO]->(bartholemu) CREATE (bartholemu)-[:RELATED_TO]->(bill)

解决方案

If this is about families of people, then easiest fix is to add a :Family node for each relational group, like so:

create (f:Family) with f match (a:person {name:"Adrian"})-[:RELATED_TO*]->(b:person) merge (f:Family)<-[:FAMILY]-(a) merge (f:Family)<-[:FAMILY]-(b)

Replace "Adrian" with "Barry" to create the second family group.

That gives you a central :Family node for each family group. You can then pick the family group that has enough :person.SomeProperty = "Yes" family members like so:

// Find families with 2 or more :person.SomeProperty = "yes" match p = (f:Family)<-[:FAMILY]-(psn:person) where psn.SomeProperty = "Yes" with f, count(psn) as cnt where cnt > 2 // Get the family members match (a:person)<-[r1:RELATED_TO]-(b:person)-[r2:RELATED_TO*]->(c) where (a)-[:FAMILY]-(f) and a = c // to get all the nodes in the loop // report the first record which'll have two // family members and all the relationships return a, r1, b, r2 limit 1

更多推荐

如果属性的数量大于n,则从Neo4j中的高度连通图返回

本文发布于:2023-11-23 20:50:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   高度   数量   Neo4j

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!