【问题标题】:Search all relationships/Nodes in the path and Filter by a property list Neo4j搜索路径中的所有关系/节点并按属性列表过滤 Neo4j
【发布时间】:2020-08-23 20:04:59
【问题描述】:

我有 2 个问题。

1) 如何在属性列表中搜索所有关系并过滤 PID (ProductID)。

2) 如何搜索路径中的所有节点并在属性列表中过滤 PID (ProductID)

Here is what the graph look like

以下查询成功过滤路径,但我已将列表索引硬编码为 0

 match path= (n1) -[rel*..10] ->(n2) 
where all(rel in relationships(path)where rel.PID[0]=2)
return path

1) 如何在列表中搜索所有关系并过滤 PID (ProductID)

我尝试使用以下查询将硬编码索引值 PID[0] 替换为循环,但它没有返回任何内容。请帮助我了解查询有什么问题。

match path= (n1) -[rel*..10] ->(n2) 
where all(rel in relationships(path)where [x in rel.PID  where x=2])
return rel

2) 如何搜索路径中的所有节点并在列表中过滤 PID (ProductID)

同样,节点的查询是什么。

以下查询运行成功,但我已将列表索引硬编码为 0

match path= (n1:Product) -[rel*..10] ->(n2) 
where all(r in nodes(path)where  r.PID[0]=1)
return path

替换了硬编码的索引值 PID[0],它什么也没给我。

match path= (n1:Product) -[rel*..10] ->(n2) 
where all(r in nodes(path)where [x in  r.PID where x=1])
return nodes(path)

任何帮助将不胜感激。

谢谢

图表的密码

    CREATE 
  (`0` :Supplier {name:"Supplier1"}) ,
  (`3` :Airport {name:"Johannesburg",city:"Johannesburg",country:"South Africa",PID:'[1,2]'}) ,
  (`5` :Warehouse {name:"WareHouse1",PID:'[1,2]'}) ,
  (`8` :Airport {name:"Dusseldorf",city:"Dusseldorf",PID:'[1]'}) ,
  (`9` :Airport {name:"Brisbane",city:"Brisbane",country:"Australia",PID:'[1]'}) ,
  (`10` :Airport {name:"Perth",city:"Perth",country:"Australia",PID:'[1,2]'}) ,
  (`11` :Product {name:"Product1",PID:'[2]'}) ,
  (`12` :Product {name:"Product 2",PID:'[2]'}) ,
  (`20` :Airport {name:"Frankfurt",city:"Germany",country:"Germany",PID:'[2]'}) ,
  (`9`)-[:`TRANSPORT` {PID:'[1]'}]->(`10`),
  (`20`)-[:`TRANSPORT` {PID:'[2]'}]->(`3`),
  (`3`)-[:`TRANSPORT` {PID:'[2]'}]->(`10`),
  (`5`)-[:`RECEIVES` ]->(`10`),
  (`12`)-[:`TRANSPORT` {PID:'[2]'}]->(`20`),
  (`0`)-[:`TRANSPORT` ]->(`12`),
  (`0`)-[:`TRANSPORT` ]->(`11`),
  (`11`)-[:`TRANSPORT` {PID:'[2]'}]->(`8`),
  (`8`)-[:`TRANSPORT` {PID:'[1]'}]->(`3`),
  (`3`)-[:`TRANSPORT` {PID:'[1]'}]->(`9`)

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    我找到了更简单的方法。将以下查询中的 1 替换为您要搜索的 PID。

    1) 如何在属性列表中搜索所有关系并过滤 PID (ProductID)。

    match path= (n1) -[rel*..8] -(n2)<-[:RECEIVES]-(n3)
    where   all(rel in relationships(path) where 1 in rel.PID)
    return path
    

    2) 如何搜索路径中的所有节点并在属性列表中过滤 PID (ProductID)

    match path= (n1) -[rel*..8] -(n2)<-[:RECEIVES]-(n3)
    where   all(rel in nodes(path) where 1 in rel.PID)
    return path
    

    【讨论】:

      【解决方案2】:

      使用 Movie Graph 示例数据库,我编写了下面的语句,我认为它大致相当于您要执行的操作。 Movie Graph 的 :ACTED_IN 关系具有属性 roles,它是一个字符串列表。该声明与在电影中扮演“Neo”角色的所有演员相匹配。在这种情况下,它会找到以基努·里维斯为演员的所有三部 Matrix 电影。希望这对您有所帮助。

      MATCH (a:Person)-[ai:ACTED_IN]->(m:Movie)
      WITH a AS actor, m AS movie, ai.roles AS roles
      UNWIND roles AS role
      WITH actor, movie, role
      WHERE role = "Neo"
      RETURN actor.name, movie.title, role
      

      【讨论】:

        猜你喜欢
        • 2023-01-09
        • 1970-01-01
        • 1970-01-01
        • 2014-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多