NEO4J Basic Usage

Source: Internet
Author: User
Tags neo4j

Cypher provides a convenient way to express queries and other neo4j behaviors, cypher can not only complete the work, but also in the product has a fast enough speed. You can also work with query cases based on the Java language and use the Java API to customize the traversal method.

The steps for querying the Cypher language are: parsing and validating queries, generating execution scenarios, locating the initial nodes, selecting and traversing relationships, changing or returning results. Brute force search in the graph database is very time consuming, preferably with an initial node, or with a corresponding index.

In the graph database, a linked node and a pattern of connections are the most valuable things that provide the most information. Cypher language is a pattern-based language that can be very intuitive to express patterns.

Cypher Basic syntax

node Syntax : (), (Matrix), (: Movie), (Matrix:movie{title: "The Matix", released:1997})

relational Syntax :-[role:acted_in{roles:["Neo"]}]->

pattern Syntax : (: People)-[:like]-> (:p eople)

patterns can be assigned to identifiers: Like= (: People)-[:like]-> (:p eople)

The node used in the pattern represents 0 to more physical nodes in the database, and the relationship represents 0 to more relationships in the database, and the schema represents 0 to multiple paths.

Cypher clause

Create clause: Creating a new node, relationship, pattern

CREATE (A:person {name: "Tom Hanks", born:1956})-[r:acted_in {roles: ["Forrest"]}]-> (M:movie {title: "Forrest Gump" , released:1994})

CREATE (D:person {name: "Robert Zemeckis", born:1951})-[:D irected]-> (m) RETURN a,d,r,m

Match clause: similar to find

MATCH (P:person {name: "Keanu Reeves"}) RETURN P
MATCH (P:person {name: "Tom Hanks"})-[r:acted_in]-> (M:movie)
RETURN M.title, R.roles

Combined clauses:

MATCH (P:person {name: "Tom Hanks"})

CREATE (M:movie {title: "Cloud Atlas", released:2012})

CREATE (p)-[r:acted_in {roles: [' Zachry ']}]-> (m)

RETURN p,r,m

Merge clause: Combining the Create and match clauses, first look for a node or path that conforms to the pattern, and if there are no results that match the pattern condition, create a node or path that conforms to the pattern. An on Create clause is also provided to perform a specific operation in the case of creating a node or relationship. Merge clause

MERGE (M:movie {title: "Cloud Atlas"})

On CREATE SET m.released = 2012

RETURN m

MATCH (M:movie {title: "Cloud Atlas"})

MATCH (P:person {name: "Tom Hanks"})

MERGE (P)-[r:acted_in]-> (m)

On CREATE SET r.roles =[' Zachry ']

RETURN p,r,m

Get the results you want

Filter Result: WHERE clause

MATCH (M:movie)

WHERE M.title = "The Matrix"

RETURN m

Equivalent usage

MATCH (M:movie {title: "The Matrix"})
RETURN m
Returning actors who have not directed films and films they have played
MATCH (P:person)-[:acted_in]-> (m)
WHERE not (p)-[:D irected]-> ()//The person who has not directed the film meets
RETURN p,m

Using Expressions in results: The form of results returned by an organization

RETURN p, p.name as name, Upper (P.name), COALESCE (P.nickname, "N/A") as nickname, {name:p.name, Label:head (Labels (p))} As Person

Aggregated data: Statistics on the returned results

Count the number of actors and directors working together
RETURN Actor,director,count (*) as collaborations

Sort

ORDER by appearances DESC LIMIT 10;

Mixed clauses: Union

MATCH (P:person)-[r:acted_in]-> (M:movie) RETURN P,type (R) as Rel,m
MATCH (P:person)-[r:directed]-> (M:movie) RETURN P,type (R) as Rel,m
tags, constraints, and indexes

Tags can be used to organize nodes in the diagram database. They are also used to qualify queries, define constraints, and build indexes.

CREATE CONSTRAINT on (movie:movie) ASSERT Movie.title is UNIQUE

NEO4J Basic Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.