Return all relationships - neo4j

Is it possible to get all relationships in a graph without the nodes?
I'm trying
MATCH [r:MEMBER]
RETURN r
But it gives me the error
Invalid input '[': expected whitespace, comment or a pattern (line 1, column 7 (offset: 6))
"MATCH [r:MEMBER] reTURN r"
^

You need to match a relationship pattern:
MATCH ()-[r:MEMBER]-()
RETURN r

Since Neo4j's relationships always have a direction, this is the fastest.
MATCH ()-[r:MEMBER]->()
RETURN r
Neo4j's browser will not display a graph, but just return the property maps of the relationships.

Related

Returning multiple columns

Hi All newbie here in Neo4J,
I am trying to return keys or properties using the following simple query in the neo4J broswer environment.
MATCH (n:movies),(m:people)
RETURN properties(n,m)
What I am trying to achieve is to return the properties for both the movies and people nodes
However, I would always get an error
Too many parameters for function 'properties' (line 2, column 9 (offset: 36))
" RETURN properties(n,m)"
I have tried,
MATCH (n:movies),(m:people)
RETURN properties(k) in [n,m]
The error I would get
Variable `k` not defined (line 2, column 20 (offset: 47))
" RETURN properties(k) in [n,m]"
I am trying to pass a list here into k but NEO4J is not permitting me to do so. Is it even possible to pass a list into the function properties() ??
Thank you in advance.
The properties function takes exactly one node or a relationship as input.
MATCH (n:movies),(m:people) RETURN properties(n), properties(m)
will create a Cartesian Product.
i.e. If you have five movies and ten people, you will get a result of all 50 combinations.
If you aren't looking for a cartesian product, you would have to define a specific pattern or restrict the MATCH clause further.
If you want just the individual properties without combining them, consider Union.
MATCH (n:Movie)
RETURN properties(n) as `Properties`
UNION ALL
MATCH (m:Person)
RETURN properties(m) as `Properties`
Why am I using aliases for a seemingly simple query? To avoid this:
All sub queries in an UNION must have the same column names (line 3,
column 1 (offset: 39))
For working with lists:
The collect function lets you create/construct a list from the results while UNWIND expands a list into a sequence of rows.
properties() only takes one argument, you can try
MATCH (n:movies),(m:people) RETURN properties(n) as prop_n, properties(m) as prop_m
or more optimal query would be
MATCH (n:movies) optional match (m:people) RETURN properties(n) as prop_n, properties(m) as prop_m
MATCH (n:movies),(m:people)
RETURN properties(k) in [n,m]
since you have not defined k so you are getting the error. Also according to doc properites() takes "An expression that returns a relationship, a node, or a map" as an argument. Your query is not supported.

PatternExpressions are not allowed to introduce new variables

I have two types of relationship that can exist between two same nodes. I want to extract the nodes that only have type1 and not type2 relationship. My query is:
Match (n) where (n)-[:type1]-(m) and (not (n)-[:type2]-(m)) return n
This gives error:
PatternExpressions are not allowed to introduce new variables: 'm'. (line 1, column 32 (offset: 31))
"Match (n) where (n)-[:type1]-(m) and (not (n)-[:type2]-(m)) return n"
^
Neither Googling around nor the documentation Patterns - Neo4j Cypher Manual give me any useful help. Do you know why is this?
How about this.
match(n)-[:type1]-(m)
where not (n)-[:type2]-(m)
return n

cypher to combine nodes and relationships into a single column

So as a complication to this question, I basically want to do
MATCH (n:TEST) OPTIONAL MATCH (n)-[r]->() RETURN DISTINCT n, r
And I want to return n and r as one column with no repeat values. However, running
MATCH (n:TEST) OPTIONAL MATCH (n)-[r]->() UNWIND n+r AS x RETURN DISTINCT x
gives a "Type mismatch: expected List but was Relationship (line 1, column 47)" error. And this query
MATCH (n:TEST) RETURN DISTINCT n UNION MATCH ()-[n]->() RETURN DISTINCT n
Puts nodes and relationships in the same column, but the context from the first match is lost in the second half.
So how can I return all matched nodes and relationships as one minimal list?
UPDATE:
This is the final modified version of the answer query I am using
MATCH (n:TEST)
OPTIONAL MATCH (n)-[r]->()
RETURN n {.*, rels:collect(r {properties:properties(r), id:id(r), type:type(r), startNode:id(startNode(r)), endNode:id(endNode(r))})} as n
There are a couple ways to handle this, depending on if you want to hold these within lists, or within maps, or if you want a map projection of a node to include its relationships.
If you're using Neo4j 3.1 or newer, then map projection is probably the easiest approach. Using this, we can output the properties of a node and include its relationships as a collected property:
MATCH (n:TEST)
OPTIONAL MATCH (n)-[r]->()
RETURN n {.*, rels:collect(r)} as n
Here's what you might do if you wanted each row to be its own pairing of a node and a single one of its relationships as a list:
...
RETURN [n, r] as pair
And as a map:
...
RETURN {node:n, rel:r} as pair
EDIT
As far as returning more data from each relationship, if you check the Code results tab, you'll see that the id, relationship type, and start and end node ids are included, and accessible from your back-end code.
However, if you want to explicitly return this data, then we just need to include it in the query, using another map projection for each relationship:
MATCH (n:TEST)
OPTIONAL MATCH (n)-[r]->()
RETURN n {.*, rels:collect(r {.*, id:id(r), type:type(r), startNode:startNode(r), endNode:endNode(r)})} as n

Comparision operator with in properies of query of Neo4j

I am in need of get data of whether there is no relation exists between two labels and condition based data on one of labels. I found an answer following ::
MATCH (n:Label1)
WHERE NOT (n)-[:REL_1]-(:Label2)
OR (n)-[:REL_1]-(e:Label2 {id:1})
RETURN count(DISTINCT(n))
But What I need is like all of id>=5 data should come in result
If I perform a query like ::
MATCH (n:Label1)
WHERE NOT (n)-[:REL_1]-(:Label2)
OR (n)-[:REL_1]-(e:Label2)
WHERE e.id >= 5
RETURN count(DISTINCT(n))
It is producing error ::
Invalid input 'H': expected 'i/I' (line 1, column 94 (offset: 93))
[UPDATED]
A Cypher query cannot have 2 WHERE clauses in a row. In addition, you have not defined the e identifier.
This query should work (I assume you only want to count n if it does not have such a relationship OR if it has at least one in which e.id is at least 5):
MATCH (n:Label1)
OPTIONAL MATCH (n)-[:REL_1]-(e:Label2)
WITH n, e
WHERE e IS NULL OR e.id >= 5
RETURN count(DISTINCT n);
You can nest WHERE filters, you just have to give up syntactic sugar to do it.
MATCH (n:Label1)
WHERE ALL(p IN (n) - [:REL_1] - (:Label2) WHERE LAST(NODES(p))['id'] >= 5)
RETURN COUNT(n)
You reconstruct any match-filter construct you can dream of with the ANY, ALL, NONE toolset, which allow you to apply filters internally as well and nest the iterable component (between IN and `WHERE) to multiple depths.

Show all Nodes and Relationships in Data Browser Tab

How can I show all nodes and relationships in Data Browser tab?
What are sample index queries that I can type in in search field?
You may also want to try a cypher query such as:
START n=node(*) RETURN n;
It's very obvious, and it will return all the existing nodes in the database.
EDIT : the following displays the nodes and the relationships :
START n=node(*) MATCH (n)-[r]->(m) RETURN n,r,m;
More simple way is
MATCH (n) RETURN (n)
MATCH (n) OPTIONAL MATCH (n)-[r]-() RETURN n, r;
You can show everything with simple MATCH (n) RETURN n, as offical documentation suggests.
START n=node(*) RETURN n from Neo4j 2.0 is deprecated:
The START clause should only be used when accessing legacy indexes
(see Chapter 34, Legacy Indexing). In all other cases, use MATCH
instead (see Section 10.1, “Match”).
There is a little help icon beside the search field, if you hoover over it it shows the syntax.
If a property of your nodes and relationships is indexed you can search for all of them like this.
node:index:indexname:fieldname:*
rels:index:indexname:fieldname:*
I found that this worked, retrieving all nodes including orphans, and all relationships:
MATCH (n) MATCH ()-[r]->() RETURN n, r
Other good way for get ALL nodes (and nodes without relationship) :
MATCH (n) RETURN n UNION START n = rel(*) return n;

Resources