NEO4J: Graph database Graphdb (ii)

Source: Internet
Author: User
Tags create index neo4j

Then the previous one, continue to introduce the graph database neo4j:http://www.cnblogs.com/rongyux/p/5537028.html

Three Advanced Find operations

1 Filter name = "Tom Hanks" person

" Tom Hanks " RETURN N;

Another way to do this:

MATCH (N:person {name:"Tom Hanks"}) RETURN N;

2 sort order, restrict limit and skip skip

Sort the output according to the actor's date of birth:

MATCH (A:person)-[:acted_in]->

Using the limit and skip pagination, the following statement shows the second page:

MATCH (a)-[:acted_in]->ten  ;

3distinct keyword Usage

Check out the top five people in all movie performers:

MATCH (a)-[:acted_in]->5

Said so much, the following two questions we can write the corresponding CQL:

(1) Query the actor Tom Hanks All the films, and the publication of the film after 2000?

MATCH (Tom:person)-[:acted_in]->(movie) WHERE tom.name="Tom Hanks"   - RETURN Movie.title;

(2) query all the films Keanu Reeves starred in, and his role is Neo?

MATCH (Keanu:person)-[r:acted_in]->(movie) WHERE keanu.name="Keanu Reeves"  "Neo" in r.rolesreturn movie.title;

3 Using comparisons to filter query results

Use of <:

MATCH (Tom:person)-[:acted_in]-> () <-[:acted_in]-(A:person) WHERE tom.name="Tom Hanks"< tom.bornreturn A.name;

A more complex statement:

MATCH (Tom:person {name:"Tom Hanks"})-[:acted_in]->(       Movie) <-[:acted_in]-<-A.born) as diff;

4 Querying using patterns

MATCH (Gene:person) WHERE gene.name="Gene Hackman"  RETURN gene;

A bit more complicated: Find all the actors who work with Gene?

MATCH (Gene:person)-[:acted_in]-> () <-[:acted_in]-(Other) WHERE gene.name="Gene Hackman  "RETURN DISTINCT Other;

A little more complicated: find a partner who works with Gene and directs his own movie?

MATCH (Gene:person)-[:acted_in]->(M), (other)-[:acted_in]->(m) WHERE gene.name=" Gene Hackman " and   (Other) -[:D irected]->() RETURN DISTINCT other;

More complicated: Find actors who have worked with Gene but have not cooperated with Robin Williams at the same time?

MATCH (Gene:person {name:"Gene Hackman"})-[:acted_in]->(movie),      (Other) -[:acted_in]->(movie),       (Robin:person {name:"Robin Williams"  }) WHERE not (Robin)-[:acted_in]->(movie) RETURN DISTINCT other;

5 Index

CREATE INDEX:

CREATE INDEX On:movie (title); CREATE INDEX on:P erson (name);

In this case, a statement based on name or title in the query will be quick. (The purpose of the index is to speed up the query)

For example, the following sentence speed will be increased:

MATCH (Gene:person)-[:acted_in]->(M), (other)-[:acted_in]->(m) WHERE gene.name=" Gene Hackman " RETURN DISTINCT Other;

To create a label index:

CREATE INDEX on:P erson (name); CREATE INDEX On:movie (title);

The retrieval speed of the tag can be improved, such as the following sentence:

MATCH (Tom:person)-[:acted_in]->(movie),      (Kevin:person)-[:acted_in]->(movie) WHERE Tom.name="Tom Hanks"  and      kevin.name="Kevin Bacon " RETURN DISTINCT movie;

6 Cluster operations

// // // // // Collect all the values to an collection

eq

MATCH (A:person)-[:acted_in]->(m) RETURN a.name, collect (m.title);

Eq2

MATCH (A:person)-[:acted_in]->;

Query we have learned a lot, the next I continue to introduce how to create a graph database, I hope to continue to pay attention.

NEO4J: Graph database Graphdb (ii)

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.