NEO4J First Use learning simple operation-cypher language use

Source: Internet
Author: User
Tags unique id neo4j

Neo4j using the Cypher language

Cypher language is the use of database operation language (DML) when learning neo4j, including the deletion and modification of graph data

The concept of neo4j database simple and violent comprehension:

There is no concept of table in neo4j, there are only two types: node and association (Relation), which can be easily understood as the points and edges inside the graph.
In a data query, a node is usually enclosed in parentheses (), with brackets [].
Of course, the concept of implicit paths is represented by nodes and associations, such as: (a)-[r]-> (b), which represents a path from Node A through association R to Node B.

To back up neo4j data:

1 ) to stop the database. 2) Backup D:\Neo4J\neo4j-Enterprise-1.9. 1 all content in the Graph.db directory under the \data directory. 3) Copy the contents of the Graph.db directory on the server to the same directory as the new server and start.

Basic operation of Cypher

1) Create a node

Create (             a) creates an empty node create (            A:person) creates a label (which can be understood as a class) for the person's node create            (A:person {name: ' Kaine ', Age: 28}) Create a node with a label of person, a property name value of Kaine, and a property with an age value of 28

2) Create an association

            Match (a), (b)            where a.name= ' Kaine ' and b.name=' Sharon '            create (a)-[r]-> (b) Creates a path to A and B nodes, at which time the variable r represents the association, and it can have a label

3) Query Keywords

Match         : matches a certain pattern, which can be a simple node, an association, or a complex path        where: To qualify a condition, it is generally a property        of the variable that qualifies the match in return  : Returns the result start        : Start node, typically used for indexed nodes or associations

        return  ...        If match has multiple objects separated by commas,        if where has multiple conditions, connect with and,        if return has more than one variable, separated by commas

4) Query Examples

 match (N) return   n 
Query all nodes and associated match (a) -[r]-> (b) where a.name= ' Kaine ' return b
The value of the query property name is Kaine node, and all its associated nodes match (a) -[*1..3]-> (b) where a.name= ' Kaine ' return b
the query property name value is a node of Kaine and all associated nodes with a distance of 1 to 3, match (a) - [*2]-> (b) where a.name= ' Kaine ' and not (a)-[*1]-> (b) return A, b
The value of the query property name is the node for Kaine, and all of its distances are 2 and the node with the distance of 1 is removed.
(used when calculating a friend's friend, that is, if a, B, c three people know, if only to calculate with a distance of 2 people will also count B, C, because A->b->c, or A->c->b are all pathways) Note: The meaning of the number within the associative brackets is n. n the maximum distance is n N. The minimum distance is N m. n distance between M to n

A. Create

CREATE (Id:label {key:value})
ID: Add a unique ID for the node, do not set the system to set one automatically, not set when the CREATE (: Label ...
Label: tag, life node type
{}: property definition, key/value format

B. Relationship

-[role:label {roles: ["Neo"]}]->--  represents a non-pointing relationship--Indicates a directed relationship [] can add ID, attribute, type, etc. information

See also: http://blog.csdn.net/wangweislk/article/details/47661863

Query by ID (the ID here is automatically created by the system): Start n=Node -)returnm; query all nodes: Start n=Node*)returnN; Query properties, Relationship: Start n=Node9)returnN,n.name,n.id,n. Level;//View the specified node, return the required properties start n=Node*) match (N)-[r:subclassof] -MreturnM,n,n.name,n.id,r;//find a specified relationship query multiple nodes by relationship: Start a=Node -) Match B-[R]<->AreturnR,b;start a=Node0) Match C-[: KNOWS] -B-[: KNOWS] -AreturnA,b,c;//find node for two-tier knows relationship start a=Node +) Match B-[*] -Areturnb;//find all nodes that are related to a node use the Where condition to query: (You can use it without establishing index) start n=Node*)whereN.name="Activity"returnN; and can use a specific symbol: Start n=Node*)whereN.id?="A*"returnN; start n=Node*)whereHas (N.type)returnN,n.name,n.id,n.type;//If a property type exists and starts with a, the node is output. The configuration file is automatically indexed: Modify the contents of the Neo4j.properties file in the Conf directory as follows, restart the neo4j and take effect on the new node after the restart. # Enable Auto-Indexing forNodesdefault  isfalsenode_auto_indexing=true# the Node property keys toBe auto-Indexedifenablednode_keys_indexable=name,id# Enable Auto-Indexing forRelationships,default  isfalserelationship_auto_indexing=true# The Relationship property keys toBe auto-Indexedifenabledrelationship_keys_indexable=knows,subclassof can be queried with Node_auto_index by attribute value after indexing: Start n=Node:node_auto_index (name="C")returnN,n.name; Modify a property value: Start a=Node*)whereA.name=ASetA.name=Areturna,a.name; start n=Node0)SetN.name="Root", n.id="001" ;//Add the Name,id property to the default root node for easy querying. Delete: Delete all nodes and relationships: START n=Node*) match n-[R]-()DeleteN,r;

Delete all nodes The above method is obsolete, the later version will be abandoned-the following method is recommended
MATCH (N)
OPTIONAL MATCH (n)-[r]-()
DELETE N,r

Graphical Database relationships

  A concept

Node:

(a)//actors (m)//movies ()//some anonymous nod 

Relationship:

-[r]->//a relationship referred to as "R" (a)-[r]-> (m)//actors have a relationship referred to as "R" to movies-[ :acted_in]->//the Relationship type is acted_in (a)-[:acted_in]-> (m)//actors that acted_in some movie (d)-[:D Irected]-> (m)//directors that DIRECTED some movie

Property:

(M {title: "The Matrix"})//movie with a title property (a {name: "Keanu Reeves", born:1964})//actor with name and born Prope Rty
(a)-[:ACTED_IN {roles:["Neo"]}]->(m)Relationship acted_in with roles property (an array of character names)

Label:

(A:person)//a person (A:person {name: ' Keanu Reeves '})//a person with properties (A:person)-[:acted_in]-> (M:movie)// A person that acted_in some movie

Query language used by neo4j Cypher

Http://www.uml.org.cn/sjjm/201203063.asp

The query language contains

Start: The starting point in the diagram is obtained by the ID of the element or so find. Match: The matching pattern of the graph, tied to the start point. WHERE: Filter conditions. Return: Returns the required.

Share site: http://www.cnblogs.com/rongyux/p/5537028.html

NEO4J First Use learning simple operation-cypher language use

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.