知识图谱Neo4j相关——Cypher查询

Cypher Query Language

  • Introduction to Cypher
    • Get started: nodes, relationships, variables, properties, patterns
    • Querying with Cypher: match, return
    • Updating: create, update, delete
    • Filtering Query Results
    • Controlling Query Process
    • Dates, datetimes, and durations

Introduction to Cypher

Cypher is Neo4j’s graph query language that allows users to store and retrieve data from the graph database. Neo4j wanted to make querying graph data easy to learn, understand, and use for everyone, but also incorporate the power and functionality of other standard data access languages. This is what Cypher aims to accomplish.

Cypher’s syntax provides a visual and logical way to match patterns of nodes and relationships in the graph. It is a declarative, SQL-inspired language for describing visual patterns in graphs using ASCII-Art syntax. It allows us to state what we want to select, insert, update, or delete from our graph data without a description of exactly how to do it. Through Cypher, users can construct expressive and efficient queries to handle needed create, read, update, and delete functionality.

Cypher is not only the best way to interact with data and Neo4j - it is also open source! The openCypher project provides an open language specification, technical compatibility kit, and reference implementation of the parser, planner, and runtime for Cypher. It is backed by several companies in the database industry and allows implementors of databases and clients to freely benefit from, use, and contribute to the development of the openCypher language.

Get started: nodes, relationships, variables, properties, patterns

Example: Nodes

()                  //anonymous node (no label or variable) can refer to any node in the database
(p:Person)          //using variable p and label Person
(:Technology)       //no variable, label Technology
(work:Company)      //using variable work and label Company

Example: Representing Relationships

//data stored with this direction
CREATE (p:Person)-[:LIKES]->(t:Technology)

//query relationship backwards will not return results
MATCH (p:Person)<-[:LIKES]-(t:Technology)

//better to query with undirected relationship unless sure of direction
MATCH (p:Person)-[:LIKES]-(t:Technology)

Example: Relationship Types

[:LIKES] - makes sense when we put nodes on either side of the relationship (Jennifer LIKES Graphs)

[:IS_FRIENDS_WITH] - makes sense when we put nodes with it (Jennifer IS_FRIENDS_WITH Michael)

[:WORKS_FOR] - makes sense with nodes (Jennifer WORKS_FOR Neo4j)

Relationship Variables

you could use either -[rel]-> or -[rel:LIKES]-> and call the rel variable later in your query to reference the relationship and its details.

Node or Relationship Properties

Node property: (p:Person {
     name: 'Jennifer'})

Relationship property: -[rel:IS_FRIENDS_WITH {
     since: 2018}]->

Patterns

(p:Person {
     name: "Jennifer"})-[rel:LIKES]->(g:Technology {
     type: "Graphs"})

Querying with Cypher: match, return

MATCH (p:Person)
RETURN p
MATCH (jenn:Person {
     name: 'Jennifer'})
RETURN jenn
MATCH (:Person {
     name: 'Jennifer'})-[:WORKS_FOR]->(company:Company)
RETURN company
MATCH (:Person {
     name: 'Jennifer'})-[:WORKS_FOR]->(company:Company)
RETURN company.name
//poorly-named property
MATCH (kristen:Customer {
     name:'Kristen'})-[rel:PURCHASED]-(order:Order)
RETURN order.orderId, order.orderDate, kristen.customerIdNo, order.orderTotalNoOfItems

//cleaner printed results with aliasing
MATCH (kristen:Customer {
     name:'Kristen'})-[rel:PURCHASED]-(order:Order)
RETURN order.orderId AS OrderID, order.orderDate AS `Purchase Date`,
       kristen.customerIdNo AS CustomerID, order.orderNumOfLineItems AS `Number Of Items`

Updating: create, update, delete

Filtering Query Results

Controlling Query Process

Dates, datetimes, and durations

你可能感兴趣的:(知识图谱,知识图谱,neo4j,mysql)