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;
Related
You are given two arrays one for some labels and another for some relationships and you are asked to return the nodes and their relationships which are found only in the arrays you where given. I tried different approach to it but I couldn't get a better cipher to return the graph with respect to both arrays
MATCH (n)-[r]-(m) where n in ["username"] and r in ["knows"] return n,r
The code above, I know its completely wrong but it kinda shows the idea, share your thoughts 😁
This should work:
MATCH (n)-[r]-(m)
WHERE ANY(l IN labels(n) WHERE l IN ['username','label2'])
AND type(r) IN ['knows','relType2']
RETURN n,r,m
NOT RELEVANT - SKIP TO Important Edit.
I have the following query:
MATCH (n)
WHERE (n:person) AND n.id in ['af97ab48544b'] // id is our system identifier
OPTIONAL MATCH (n)-[r:friend|connected|owner]-(m)
WHERE (m:person OR m:dog OR m:cat)
RETURN n,r,m
This query returns all the persons, dogs and cats that have a relationship with a specific person. I would like to turn it over to receive all the nodes & relationships that NOT includes in this query results.
If it was SQL it would be
select * from graph where id NOT IN (my_query)
I think that the OPTIONAL MATCH is the problematic part. I How can I do it?
Any advice?
Thanks.
-- Important Edit --
Hey guys, sorry for changing my question but my requirements has been changed. I need to get the entire graph (all nodes and relationships) connected and disconnected except specific nodes by ids. The following query is working but only for single id, in case of more ids it isn't working.
MATCH (n) WHERE (n:person)
OPTIONAL MATCH (n)-[r:friend|connected|owner]-(m) WHERE (m:person OR m:dog OR m:cat)
WITH n,r,m
MATCH (excludeNode) WHERE excludeNode.id IN ['af97ab48544b']
WITH n,r,m,excludeNode WHERE NOT n.id = excludeNode.id AND (NOT m.id = excludeNode.id OR m is null)
RETURN n,m,r
Alternatively I tried simpler query:
MATCH (n) WHERE (n:person) AND NOT n.id IN ['af97ab48544b'] return n
But this one does not returns the relationships (remember I need disconnected nodes also).
How can I get the entire graph exclude specific nodes? That includes nodes and relationships, connected nodes and disconnected as well.
try this:
match (n) where not n.id = 'id to remove' optional match (n)-[r]-(m)
where not n.id in ['id to remove'] and not m.id in ['id to remove']
return n,r,m
You've gotta switch the 'perspective' of your query... start by looping over every node, then prune the ones that connect to your person.
MATCH (bad:person) WHERE bad.id IN ['af97ab48544b']
WITH COLLECT(bad) AS bads
MATCH path = (n:person) - [r:friend|:connected|:owner] -> (m)
WHERE n._id = '' AND (m:person OR m:cat OR m:dog) AND NOT ANY(bad IN bads WHERE bad IN NODES(path))
RETURN path
That said, this is a problem much more suited to SQL than to a graph. Any time you have to loop over every node with a label, you're in relational territory, the graph will be less efficient.
I'm trying to list all of the properties for a set of nodes.
Match (n:"Indicator")
return properties(n), ID(n)
I'm unsure of the syntax and couldn't find the answer in the refcard or docs.
In Neo4j version 3.0.0 you may do:
Match (n:Indicator) return properties(n), ID(n)
To return the ID and properties of nodes.
At the moment you can't do this using cypher but it is on the top five on the ideas board.
MATCH (n)
RETURN DISTINCT keys(n), size(keys(n))
ORDER BY size(keys(n)) DESC
Properties(n) works if you need the properties of the node with the key and value, but if you only need to see the properties name's in a easy way you can do this.
Example:
MATCH (n:Indicator) return ID(n), keys(n), size(keys(n))
Results:
Results from Neo4j browser
You can quit ID(n) and size(keys(n)) without problem but is good if you need to identify a node that dont have the required properties or is not complete.
Also you can use a DISTINCT if you have generic and repetitive properties on the same type of Node like this.
MATCH (n:Indicator) return DISTINCT ID(n), keys(n), size(keys(n))
As I have said this also works without problems and give you the array of properties that you need.
MATCH (n:Indicator) return keys(n)
Result only returning keys
But you can resume this long list of results, with a DISTINCT
MATCH (n:Indicator) return DISTINCT keys(n)
Result with only the differents lists of properties that the Node (n) have
If you have APOC installed, I use this to get a distinct list of all node property combinations:
MATCH (n)
RETURN DISTINCT apoc.coll.sort(keys(n)) as props
,size(keys(n)) as key_size
Note: you don't have to use APOC, but if you don't you'll get duplicates where values are same, but ordering is different, so sorting combines them
in the case where the keys are returned in different orders, this does the trick:
MATCH (n)
UNWIND keys(n) AS allProps
RETURN COLLECT(DISTINCT allProps) as distinctProps
MATCH (n:Label)
WITH DISTINCT(keys(n)) as key_sets
UNWIND(key_sets) as keys
RETURN DISTINCT(keys) as key
This will return a clean list of distinct keys
I have two nodes.
How do I express the pattern "nodes without relationships"? Cannot find it in official doc!
A picture to illustrate.
MATCH (n)
WHERE NOT (n)--()
RETURN n;
I have a very simple data model with source-[:link]->target. I'd like to find out all the nodes, which have no incoming links e.g. the "roots" of my data model. How do I do that in Cypher?
You can filter on null values
START target=node(*)
MATCH target<-[r?:link]-source
WHERE r is null
RETURN target
For details, refer Cypher where clause documentation
Alternatively, you can also do
START target=node(*)
WHERE not(target<-[:link]-source)
RETURN target
*Note: not tested
start n=node(*)
match n<-[?]-m
with n, count(m) as c
where c=0
return n