ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Cypher Fundamentals-Filtering Queries

2022-05-05 00:01:32  阅读:199  来源: 互联网

标签:Cypher name born Fundamentals Movie Person Queries WHERE MATCH


Cypher Fundamentals

Reading Data from Neo4j

Writing Data to Neo4j

QUIZ

Filtering Queries

VideoTranscript  

Filtering queries

Earlier, you learned that the WHERE clause is used to tell the query engine to filter what nodes are retrieved from the graph. In this lesson you will learn about some of the ways that you can filter your queries.

You have already learned how you can test equality for properties of a node and how you can use logical expressions to further filter what you want to retrieve.

For example, this query retrieves the Person nodes and Movie nodes where the person acted in a movie that was released in 2008 or 2009:

cypher  
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE m.released = 2008 OR m.released = 2009
RETURN p, m

Filtering by node labels

You have already seen this type of query. It returns the names of all people who acted in the movie, The Matrix.

cypher  
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE m.title='The Matrix'
RETURN p.name

An alternative to this query is the following where we test the node labels in the WHERE clause:

cypher  
MATCH (p)-[:ACTED_IN]->(m)
WHERE p:Person AND m:Movie AND m.title='The Matrix'
RETURN p.name

Both queries execute the same way, but you may want to use one style of filtering over another in your code.

Filtering using ranges

You can specify a range for filtering a query. Here we want to retrieve Person nodes of people who acted in movies released between 2000 and 2003:

cypher  
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE 2000 <= m.released <= 2003
RETURN p.name, m.title, m.released

Filtering by existence of a property

Recall that by default, there is no requirement that a node or relationship has a given property. Here is an example of a query where we only want to return Movie nodes where Jack Nicholson acted in the movie, and the movie has the tagline property.

cypher  
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name='Jack Nicholson' AND m.tagline IS NOT NULL
RETURN m.title, m.tagline

Filtering by partial strings

Cypher has a set of string-related keywords that you can use in your WHERE clauses to test string property values. You can specify STARTS WITHENDS WITH, and CONTAINS.

For example, to find all actors in the graph whose first name is Michael, you would write:

cypher  
MATCH (p:Person)-[:ACTED_IN]->()
WHERE p.name STARTS WITH 'Michael'
RETURN p.name

String tests are case-sensitive so you may need to use the toLower() or toUpper() functions to ensure the test yields the correct results. For example:

cypher  
MATCH (p:Person)-[:ACTED_IN]->()
WHERE toLower(p.name) STARTS WITH 'michael'
RETURN p.name

Filtering by patterns in the graph

Suppose you wanted to find all people who wrote a movie but did not direct that same movie. Here is how you would perform the query:

cypher  
MATCH (p:Person)-[:WROTE]->(m:Movie)
WHERE NOT exists( (p)-[:DIRECTED]->(m) )
RETURN p.name, m.title

Filtering using lists

If you have a set of values you want to test with, you can place them in a list or you can test with an existing list in the graph. A Cypher list is a comma-separated set of values within square brackets.

You can define the list in the WHERE clause. During the query, the graph engine will compare each property with the values IN the list. You can place either numeric or string values in the list, but typically, elements of the list are of the same type of data. If you are testing with a property of a string type, then all the elements of the list will be strings.

In this example, we only want to retrieve Person nodes of people born in 19651970, or 1975:

cypher  
MATCH (p:Person)
WHERE p.born IN [1965, 1970, 1975]
RETURN p.name, p.born

You can also compare a value to an existing list in the graph.

We know that the :ACTED_IN relationship has a property, roles that contains the list of roles an actor had in a particular movie they acted in. Here is the query we write to return the name of the actor who played Neo in the movie The Matrix:

cypher  
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE  'Neo' IN r.roles AND m.title='The Matrix'
RETURN p.name, r.roles

Check your understanding

1. Filtering a value in a list

Suppose you want to retrieve all movies that have a released property value that is 2000, 2002, 2004, 2006, or 2008. Here is an incomplete Cypher example to return the title property values of all movies released in these years. What keyword do you specify in the WHERE clause?

Once you have selected your option, click the Check Results query button to continue.

cypher  
MATCH (m:Movie)
WHERE m.released [2000, 2002, 2004, 2006, 2008]
RETURN m.title

2. Finding people born in the seventies.

We want to write a MATCH clause to retrieve all Person nodes for people born in the seventies.

Select the WHERE clauses below that will filter this query properly:

MATCH (a:Person) RETURN a.name, a.born

  • WHERE a.born >= 1970 AND a.born < 1980

  • WHERE 1970 <= a.born < 1980

  • WHERE 1970 < a.born <= 1980

  • WHERE a.born IN [1970,1971,1972,1973,1974,1975,1976,1977,1978,1979]

Finding Emil 35% Finding Specific Actors  

标签:Cypher,name,born,Fundamentals,Movie,Person,Queries,WHERE,MATCH
来源: https://www.cnblogs.com/z-cm/p/16223036.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有