Results not appearing neo4j query - neo4j

I have only about 16000 nodes in my database and when I do match n return n I never get any graph back, any reason why or how to fix?

Either you have no relation between the nodes - when using the browser GUI check the result as table.
You can limit this for testing e.g. match (n) return n limit 25.

Related

Unexpected relation and node COUNT in Neo4j

Whenever I am using a query to get the count of a specific node, I always get the number greater than 1 even though there is only one distinct type of that node existing.
Sample query:
MATCH (p)-[rel]->(v:myDistinctNode) RETURN COUNT(v)
Output: 80
MATCH (p)-[rel]->(v:myDistinctNode) RETURN COUNT(DISTINCT v)
Output: 1
I see different results while using DISTINCT, but I cannot use DISTINCT all the time. Why I am seeing this and how can I avoid it? Thanks!
Neo4j Kernel-Version: 3.5.14
The short answer is that you need to use a collect statement to make it work.
MATCH (p)<-[rel]-(v:myDistinctNode) WITH collect(v) AS nodes RETURN count(nodes)
This should return one.
I'm not a cypher expert, but I believe the reason it doesn't work is that the cypher result seems more like a table where in one row you have p, another row you have r, and the last row you have v. Even though v is a unique entity, there are still 80 rows that have v.

How connect Result nodes Neo4j

Neo4j has a tick box option 'connect results nodes' which i gather runs a second query to connect nodes after your initial query.
eg
MATCH (n:User)
where n.Verified = 'false'
return n
order by n.followers DESC
Limit 40
This query returns 40 nodes which are connected to each other. While this works in the Neo4j browser, I cant quite get it to connect in Neo4j bloom. So question is whats the second query thats run to connect the result nodes under the hood?
Thanks
For anyone who falls into the same problem. The answer is a subquery which checks if node ids are in the original set. In the first query you return a list of node ids using the built in ID function, then collect the nodes. In the subquery you unwind the nodes and in the subquery where clause filter the using the list of IDs.
Match (b:User)
where b.Verified = 'false' and b.followers > 60
with collect(b) as users, collect(ID(b)) as listUsers
CALL{
with users,listUsers
unwind users as x
match(x)-[r]-(c:User)
where ID(c) in listUsers
return x,r,c
}
return x,r,c

Neo4J order by count relationships extremely slow

I'm trying to model a large knowledge graph. (using v3.1.1).
My actual graph contains only two types of Nodes (Topic, Properties) and a single type of Relationships (HAS_PROPERTIES).
The count of nodes is about 85M (47M :Topic, the rest of nodes are :Properties).
I'm trying to get the most connected node:Topic for this. I'm using the following query:
MATCH (n:Topic)-[r]-()
RETURN n, count(DISTINCT r) AS num
ORDER BY num
This query or almost any query I try to perform (without filtering the results) using the count(relationships) and order by count(relationships) is always extremely slow: these queries take more than 10 minutes and still no response.
Am i missing indexes or is the a better syntax?
Is there any chance i can execute this query in a reasonable time?
Use this:
MATCH (n:Topic)
RETURN n, size( (n)--() ) AS num
ORDER BY num DESC
LIMIT 100
Which reads the degree from a node directly.

Neo4j: How to limit subqueries

I just imported the English Wikipedia into Neo4j and am playing around. I started by looking up the pages that link into the Page "Berlin"
MATCH p=(p1:Page {title:"Berlin"})<-[*1..1]-(otherPage)
WITH nodes(p) as neighbors
LIMIT 500
RETURN DISTINCT neighbors
That works quite well. What I would like to achieve next is to show the 2nd degree of relationships. In order to be able to display them correctly, I would like to limit the number of first degree relationship nodes to 20 and then query the next level of relationship.
How does one achieve that?
I don't know the Wikipedia model, but I'm assuming that there are many different relationship types and that is why that -[*1..1]-, I think that is analogous to -[]- or even --. I doubt it has any serious impact though.
You can collect up the first level matches and limit them to 20 using a WITH with a LIMIT. You can then perform a second match using those (<20) other pages as the start point.
MATCH (p1:Page {title:"Berlin"})<-[*1..1]-(otherPage:Page)
WITH p1, otherPage
LIMIT 20
MATCH (otherPage)<-[*1..1]-(secondDegree:Page)
WHERE secondDegree <> p1
WITH otherPage, secondDegree
LIMIT 500
RETURN otherPage, COLLECT(secondDegree)
There are many ways to return the data, this just returns the first degree match with an array of the subsequent matches.
If the only type of relationship is :Link and you want to keep the start node then you can change the query to this:
MATCH (p1:Page {title:"Berlin"})<-[:Link]-(otherPage:Page)
WITH p1, otherPage
LIMIT 20
MATCH (otherPage)<-[:Link]-(secondDegree:Page)
WHERE secondDegree <> p1
WITH p1, otherPage, secondDegree
LIMIT 500
RETURN p1, otherPage, COLLECT(secondDegree)

neo4j: Is there a way/how to select random nodes?

I would like to retrieve a specific number of random nodes. The graph consists of 3 000 000 nodes where some of them are sources, some are target and some are both.
The aim is to retrieve random sources and as I don't know how to select random, the program generates k random numbers from 1 to 3 000 000 which represent node IDs and then discards all randomly selected nodes that are not sources. As this procedure is time-consuming, I wonder whether it is possible to directly select random sources with cypher query.
In case to select all sources, the query would be the following
START t=node(*) MATCH (a)-[:LEADS_TO]->(t) RETURN a
Does anyone know how would it be possible to select the limited number of random nodes directly with a cypher or, if not possible, suggest any workaround?
You can use such construction:
MATCH (a)-[:LEADS_TO]->(t)
RETURN a, rand() as r
ORDER BY r
It should return you random set of object.
Tested with Neo4j 2.1.3
You can limit your query with skip/limit so you could do
START t=node(*)
MATCH (a)-[:LEADS_TO]->(t)
RETURN a
SKIP {randomoffset} LIMIT {randomcount}
Otherwise you can also create a set of random node-id's and pass them as parameter to the cypher statement.
Another way of the one suggested here, for case you want a random Start nodes with all there connections is:
MATCH (a)-[:LEADS_TO]->[]
WITH a,rand() AS rand
ORDER BY rand LIMIT {YourLimit}
MATCH (a)-[l:LEADS_TO]->(t)
RETURN a,l,t
MATCH (n:Label)
WITH n, rand() AS r
ORDER BY r
RETURN n LIMIT <no. of random nodes>

Resources