美文网首页
Neo4j Notes - Cypher Manual Chap

Neo4j Notes - Cypher Manual Chap

作者: letsgetdrunk | 来源:发表于2019-03-20 10:13 被阅读0次

    3 Clauses

    See simpler version at this doc
    See full introduction here

    3.1 MATCH

    [图片上传失败...(image-e8a3c3-1553048005237)]

    3.1.1 Intro

    3.1.2 Basic node finding

    Too simple, omitted.

    3.1.3 Relationship basics

    Too simple, omitted.

    3.1.4 Relationships in depth

    3.1.4.1 Relationship types with uncommon characters

    MATCH (charlie:Person { name: 'Charlie Sheen' }),(rob:Person { name: 'Rob Reiner' })
    CREATE (rob)-[:`TYPE
    WITH SPACE`]->(charlie)
    

    Which leads to the following graph:


    Graph 2.png

    3.1.4.2 Multiple relationships

    MATCH (charlie { name: 'Charlie Sheen' })-[:ACTED_IN]->(movie)<-[:DIRECTED]-(director)
    RETURN movie.title, director.name
    

    3.1.4.3. Variable length relationships

    using the following syntax: -[:TYPE*minHops..maxHops]→. minHops and maxHops are optional and default to 1 and infinity respectively.

    MATCH (martin { name: 'Charlie Sheen' })-[:ACTED_IN*1..3]-(movie:Movie)
    RETURN movie.title
    

    result:

    movie.title
    "Wall Street"
    "The American President"
    "The American President"
    

    3.1.4.4 Relationship variable in variable length relationships

    When the connection between two nodes is of variable length, the list of relationships comprising the connection can be returned using the following syntax:

    MATCH p =(actor { name: 'Charlie Sheen' })-[:ACTED_IN*2]-(co_actor)
    RETURN relationships(p)
    
    relationships(p)
    [:ACTED_IN[0]{role:"Bud Fox"},:ACTED_IN[1]{role:"Carl Fox"}]
    [:ACTED_IN[0]{role:"Bud Fox"},:ACTED_IN[2]{role:"Gordon Gekko"}]
    

    3.1.4.5. Match with properties on a variable length path

    MATCH (charlie:Person { name: 'Charlie Sheen' }),(martin:Person { name: 'Martin Sheen' })
    CREATE (charlie)-[:X { blocked: FALSE }]->(:UNBLOCKED)<-[:X { blocked: FALSE }]-(martin)
    CREATE (charlie)-[:X { blocked: TRUE }]->(:BLOCKED)<-[:X { blocked: FALSE }]-(martin)
    
    image.png
    MATCH p =(charlie:Person)-[* { blocked:false }]-(martin:Person)
    WHERE charlie.name = 'Charlie Sheen' AND martin.name = 'Martin Sheen'
    RETURN p
    

    result:

    p
    (0)-[X,20]->(20)<-[X,21]-(1)
    

    3.1.4.6 Zero length paths

    If the path length between two nodes is zero, they are by definition the same node.

    MATCH (wallstreet:Movie { title: 'Wall Street' })-[*0..1]-(x)
    RETURN x
    

    Returns the movie itself as well as actors and directors one relationship away

    3.1.4.7 Named paths

    MATCH p =(michael { name: 'Michael Douglas' })-->()
    RETURN p
    

    result:

    p
    (2)-[ACTED_IN,5]->(6)
    (2)-[ACTED_IN,2]->(5)
    

    3.1.4.8. Matching on a bound relationship

    MATCH (a)-[r]-(b)
    WHERE id(r)= 0
    RETURN a,b
    
    a                                                  b
    Node[0]{name:"Charlie Sheen"}                      Node[5]{title:"Wall Street"}
    
    Node[5]{title:"Wall Street"}                       Node[0]{name:"Charlie Sheen"}
    

    3.1.5 Shortest path

    3.1.5.1. Single shortest path

    MATCH (martin:Person { name: 'Martin Sheen' }),(oliver:Person { name: 'Oliver Stone' }), p = shortestPath((martin)-[*..15]-(oliver))
    RETURN p
    

    3.1.5.2. Single shortest path with predicates

    MATCH (charlie:Person { name: 'Charlie Sheen' }),(martin:Person { name: 'Martin Sheen' }), p = shortestPath((charlie)-[*]-(martin))
    WHERE NONE (r IN relationships(p) WHERE type(r)= 'FATHER')
    RETURN p
    

    3.1.5.3. All shortest paths

    MATCH (martin:Person { name: 'Martin Sheen' }),(michael:Person { name: 'Michael Douglas' }), p = allShortestPaths((martin)-[*]-(michael))
    RETURN p
    

    3.1.6. Get node or relationship by id

    3.1.6.1 Node by id

    3.1.6.2 Relationship by id

    3.1.6.3 Multiple nodes by id

    MATCH (n)
    WHERE id(n) IN [0, 3, 5]
    RETURN n
    

    3.2 OPTIONAL MATCH

    3.2.1 Introduction

    OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.

    3.2.2 Optional Relationships

    MATCH (a:Movie { title: 'Wall Street' })
    OPTIONAL MATCH (a)-->(x)
    RETURN x
    

    return <null>

    3.2.3 Properties on optional elements

    MATCH (a:Movie { title: 'Wall Street' })
    OPTIONAL MATCH (a)-->(x)
    RETURN x, x.name
    

    return <null> <null>

    3.2.4. Optional typed and named relationship

    MATCH (a:Movie { title: 'Wall Street' })
    OPTIONAL MATCH (a)-[r:ACTS_IN]->()
    RETURN a.title, r
    

    return "Wall Street" <null>

    3.4 RETURN

    Too simple, omitted part content.

    3.4.5. Return all elements

    MATCH p =(a { name: 'A' })-[r]->(b)
    RETURN *
    

    will return two nodes, the relationship and the path used in the query.

    3.4.10. Unique results

    MATCH (a { name: 'A' })-->(b)
    RETURN DISTINCT b
    

    3.5 WITH

    3.5.1 Introduction

    Using WITH, you can manipulate the output before it is passed on to the following query parts.
    Three common usage:

    • to limit the number of entries that are then passed on to other MATCH clauses.
    • to filter on aggregated values. WITH is used to introduce aggregates which can then be used in predicates in WHERE.
    • to separate reading from updating of the graph.

    3.5.2 Filter on aggregate function results

    MATCH (david { name: 'David' })--(otherPerson)-->()
    WITH otherPerson, count(*) AS foaf
    WHERE foaf > 1
    RETURN otherPerson.name
    

    return "Anders"

    3.5.3 Sort results before using collect on them

    MATCH (n)
    WITH n
    ORDER BY n.name DESC LIMIT 3
    RETURN collect(n.name)
    

    return ["George","David","Ceasar"]

    3.5.4. Limit branching of a path search

    MATCH (n { name: 'Anders' })--(m)
    WITH m
    ORDER BY m.name DESC LIMIT 1
    MATCH (m)--(o)
    RETURN o.name
    

    return

    "Bossman"
    "Anders"
    

    3.6 UNWIND

    Read Later

    3.7 WHERE

    3.7.1 Introduction

    In the case of WITH and START, WHERE simply filters the results.
    For MATCH and OPTIONAL MATCH on the other hand, WHERE adds constraints to the patterns described. It should not be seen as a filter after the matching is finished.

    image.png

    3.7.2 Basic usage

    Too simple, most part omitted.

    3.7.2.2 Filter on node label

    To filter nodes by label, write a label predicate after the WHERE keyword using WHERE n:foo.

    MATCH (n)
    WHERE n:Swedish
    RETURN n.name, n.age
    

    3.7.2.6. Property existence checking

    Use the exists() function to only include nodes or relationships in which a property exists.

    MATCH (n)
    WHERE exists(n.belt)
    RETURN n.name, n.belt
    

    3.7.3 String matching

    3.7.3.1. Prefix string search using STARTS WITH

    MATCH (n)
    WHERE n.name STARTS WITH 'Pet'
    RETURN n.name, n.age
    

    3.7.3.2. Suffix string search using ENDS WITH

    MATCH (n)
    WHERE n.name ENDS WITH 'ter'
    RETURN n.name, n.age
    

    3.7.3.3. Substring search using CONTAINS

    MATCH (n)
    WHERE n.name CONTAINS 'ete'
    RETURN n.name, n.age
    

    3.7.3.4. String matching negation

    use the NOT keyword to exclude all matches on given string

    MATCH (n)
    WHERE NOT n.name ENDS WITH 'y'
    RETURN n.name, n.age
    

    3.7.4 Regular expressoiins

    =~

    3.7.5 Using path patterns in WHERE

    3.7.5.1 Filter on patterns

    MATCH (timothy { name: 'Timothy' }),(others)
    WHERE others.name IN ['Andy', 'Peter'] AND (timothy)<--(others)
    RETURN others.name, others.age
    

    return "Andy" 36

    3.7.5.2 using NOT

    3.7.5.3 Filter on patterns with properties

    MATCH (n)
    WHERE (n)-[:KNOWS]-({ name: 'Timothy' })
    RETURN n.name, n.age
    

    return "Andy" 36

    3.7.5.4. Filter on relationship type

    MATCH (n)-[r]->()
    WHERE n.name='Andy' AND type(r)=~ 'K.*'
    RETURN type(r), r.since
    

    3.7.6 Lists

    MATCH (a)
    WHERE a.name IN ['Peter', 'Timothy']
    RETURN a.name, a.age
    
    "Timothy"  25
    "Peter"  35
    

    3.7.7 Missing properties and values

    Read Later

    3.7.8 Using ranges

    相关文章

      网友评论

          本文标题:Neo4j Notes - Cypher Manual Chap

          本文链接:https://www.haomeiwen.com/subject/djpwmqtx.html