Why don't some query results have graph representation in Neo4j? - neo4j

In the Neo4j Browser, I performed one query as follows:
match (subject:User {name:{name}})
match (subject)-[:works_for]->(company:Company)<-[:works_for]-(person:User),
(subject)-[:interested_in]->(interest)<-[:interested_in]-(person)
return person.name as name, count(interest) as score,
collect(interest.name) as interests order by score DESC
The result only has the "table" and "text" views, without the "graph". Normally, a query can generate a subgraph. Right?

If you look at your return, you're not returning any actual nodes or relationships. You're returning property values (strings), counts (longs), and collections of strings. If you returned person instead you would probably be able to see a graphical result, as the return would include data (id, labels, properties) that could be used to display graph elements.

Related

Finding unique values of a node property

I have a neo4j database with several nodes each having many properties. I am trying to find a list of unique values available for each property.
Currently, I can search for nodes that have a certain value 'xxx' with a query in this manner below however, I want to find all the unique values 'xxx','yyy', etc. that may exist in all the nodes in my database.
match (n:Db_Nodes) where n.stringProperty = "xxx" return n
How should I go about structuring the query desired?
You can use the DISTINCT Clause to return all the unique values for this property.
There are two ways to get all the values:
Get all the values in a list. Here result will be one single record with all the unique values in the form of a list.
MATCH (n:Db_Nodes) RETURN COLLECT(DISTINCT n.stringProperty) as propertyList
Get one value per record. Here multiple records will be returned(One per unique property value).
MATCH (n:Db_Nodes) RETURN DISTINCT n.stringProperty

(Neo4j) Query does not display a graph

I have a query that I am trying to execute. The query works, but there isn't an option to see this data in graph format. Instead the data is returned in table/text format.
When I simplify the query, the output is displayed in graph format - No idea why,
This is the query that is giving me the issue:
MATCH (p:Person)-[hi:hasIdentity]->(i:Identity)
MATCH (j:Person)-[hi2:hasIdentity]->(i2:Identity)
MATCH (i)-[bl:Linked]->(i2)
WHERE NOT p=j
return DISTINCT(p.id), COUNT(DISTINCT(j))
LIMIT 5
Does anyone have any idea why that might be the case?
You'll need to return variables associated with nodes and/or relationships for it to display as a graph. As it is now you're returning properties of nodes (p.id), probably integers or strings. Try this return instead:
...
RETURN p, COUNT(DISTINCT j)
LIMIT 5
By the way, DISTINCT isn't a function, no need for parenthesis, and when you have a RETURN or WITH that has an aggregation, you don't need to use DISTINCT for that line since the non-aggregation variables become distinct since they act as the grouping key for the aggregation.

How filtering two distinct lists synchronously by a node attribute in Neo4j?

The result of a more complex Cypher query is a list of nodes and a list of relationships, where each position/ row of one list is content-wise related to the same position/ row of the other list. The relationship consists exclusively of the position in the two lists, there is no graph between the according elements.
Because I need to filter both lists by an attribute of the nodes, I combined both lists in a map and tried to realize my intention by list comprehension.
MATCH
<complex query>
WITH collect(labelA) AS nodesList, collect(relation) AS relationList
WITH {nodes:nodesList, relations:relationList} AS data
WITH [x IN data WHERE x.nodes.attributName <> „text“] AS filteredData
RETURN filteredData;
Which understandably leads to the following error in line 5.
Neo.ClientError.Statement.TypeError: Type mismatch: expected a map but was List{(4538063), (4538063), (4538063)}
When I unwind the x.nodes part in a line before it works from a technical point of view, but I’m loosing my dependencies to the second table.
So how can I filter both lists synchronously by a node attribute of the first list?
You have a list of pairs, so you should collect them as such and then filter them as a single item.
Example of what I mean...
MATCH
<complex query>
WITH COLLECT({node:labelA, relation:relation}) AS data
// or you can do
// WITH COLLECT([labelA, relation]) AS data
WITH filter(x IN data WHERE x.node.attributName <> "text") AS filteredData
RETURN filteredData;

Grouping Sequential items in cypher

So here's the deal, I'm using Neo4J 3.01 and I have a graph with nodes of type Action which have amongst other links and properties the following two properties:
Type: [Comment,Reply,Vote etc]
Date:[epoch timestamp]
I am attempting to run a cypher query that returns a sorted list of nodes ordered by the date field but collapsing (Collect?) sequential items of the same type
So for the following nodes:
{type:'Comment',date:1}
{type:'Comment',date:2}
{type:'Vote',date:3}
{type:'Comment',date:4}
{type:'Comment',date:5}
{type:'Reply',date:6}
{type:'Reply',date:7}
{type:'Vote',date:8}
{type:'Vote',date:9}
I would hope to get something like:
{type:'Comment',actions:[{type:'Comment',date:1},{type:'Comment',date:2}]}
{type:'Vote',actions:[{type:'Vote',date:3}]}
{type:'Comment',actions:[{type:'Comment',date:4},{type:'Comment',date:5}]}
{type:'Reply',actions:[{type:'Reply',date:6},{type:'Reply',date:7}]}
{type:'Vote',actions:[{type:'Vote',date:8},{type:'Vote',date:9}]}
I attempted a simple collect and order cypher query:
Match (a:Action)
with a.type as type, a order by a.date limit 20
return type, collect(a) as actions
but this seems to collect each type in its own group regardless of the sequence, so the actual result is something like:
{type:'Comment',actions:[{type:'Comment',date:1},{type:'Comment',date:2},{type:'Comment',date:4},{type:'Comment',date:5}]}
{type:'Vote',actions:[{type:'Vote',date:3},{type:'Vote',date:8},{type:'Vote',date:9}]}
{type:'Reply',actions:[{type:'Reply',date:6},{type:'Reply',date:7}]}

Neo4j Cypher finding the average of node properties which have another property in common

I have a "Gene" Label/node type with properties "value" and "geneName"
I have a separate Label/node type called Pathway with property "
I want to go through all the different geneName's and find the average of all the Gene's value with that Gene name. I need all those Gene's displayed as different rows. Bearing in mind I have a a lot of geneName's so I can't name them all in the query. I need to do this inside a certain Pathway.
MATCH (sample)-[:Measures]->(gene)-[:Part_Of]->(pathway)
WHERE pathway.pathwayName = 'Pyrimidine metabolism'
WITH sample, gene, Collect (distinct gene.geneName) AS temp
I have been trying to figure this out all day now and all I can manage to do is retrieve all the rows of geneNames. I'm lost from there.
RETURN extract(n IN temp | RETURN avg(gene.value))
Mabye?
This query should return the average gene value for each distinct gene name:
MATCH (sample)-[:Measures]->(gene)-[:Part_Of]->(pathway:Pathway)
WHERE pathway.pathwayName = 'Pyrimidine metabolism'
RETURN sample, gene.geneName AS name, AVG(gene.value) AS avg;
When you use an aggregation function (like AVG), it automatically uses distinct values for the non-aggregating values in the same WITH or RETURN clause (i.e., sample and gene.geneName in the above query).
For efficiency, I have also added the label to the pathway nodes so that neo4j can start off by scanning just Pathway nodes instead of all nodes. In addition, you should consider creating an index on :Pathway(pathwayName), so that the search for the Pathway is as fast as possible.

Resources