Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-07-2021 12:16 AM
Hello,
I'm extending Neo4j with some user-defined procedures. Is there any way to return a graph and query data from the returned graph?
At the moment i'm able to return a graph (using GraphResult class) from a called procedure but i can't query nodes and edges from it.
In the following an example on what i need to do:
CALL procedureThatReturnGraphResult(param1, param2)
MATCH (a)-[:rel]->(b)
WHERE a.name = 'something'
RETURN a
Thanks in advance
04-07-2021 01:42 AM
Hello,
First things first, you should add a YIELD clause to your query, and then query on that.
Let's take an example from an apoc procedure that returns a graph. apoc.meta.graphSample returns a sample containing nodes and relationships as you can see below
So, I could for instance YIELD nodes, to then use them in further querying :
CALL apoc.meta.graphSample()
YIELD nodes, relationships
UNWIND nodes as n
UNWIND relationships as r
//Add some processing here
RETURN n,r
Hopefully this should work for you ?
04-07-2021 02:30 AM
Hello,
Thank you for your response.
I already tried to YIELD the values, but the problem i think that is i can not get access to manipulate yielded nodes and relationships.
I'm sharing with you three photos for clarification:
Hassan
04-07-2021 03:02 AM
04-07-2021 04:40 AM
I think that Relationships empty fields are due to that these relationships don't have properties.
For querying the procedure results: i have always the same problem when referencing the nodes to 'n'.
What i'm trying to do is querying from 'nodes' and 'relationships' and not the data base since 'nodes' and 'relationships' represent the returned virtual graph.
04-12-2021 06:20 PM
Use cypher-shell if you want to see the relationship types in the output.
04-13-2021 12:30 AM
After trying to execute some queries in cypher-shell i noticed that some queries can have results in cypher-shell but empty output in neo4j browser
04-13-2021 06:57 AM
AFAIK it's a quirk of the Neo4j browser on what it displays in different output modes. Not everything in the result set is included when the results are viewed in text mode. All the data is there and you will see the full result set in table mode. Using the Movies database as an example, this query run in the Neo4j Browser:
match path=(:Person)-[:ACTED_IN]->(:Movie) RETURN path LIMIT 1
Returns this in text mode (note that no :Person
or :Movie
labels or :ACTED_IN
relationship or direction are shown):
+----------------------------------------------------------------------------------------------------------------------------------+
|"path" |
+----------------------------------------------------------------------------------------------------------------------------------+
|[{"name":"Keanu Reeves","born":1964},{"roles":["Neo"]},{"tagline":"Free your mind","title":"The Matrix Reloaded","released":2003}]|
+----------------------------------------------------------------------------------------------------------------------------------+
All the data is there when viewed in table mode (note the label and relationship data):
{
"start": {
"identity": 0,
"labels": [
"Person",
"Famous",
"Rich"
],
"properties": {
"name": "Keanu Reeves",
"born": 1964
}
},
"end": {
"identity": 8,
"labels": [
"Movie"
],
"properties": {
"tagline": "Free your mind",
"title": "The Matrix Reloaded",
"released": 2003
}
},
"segments": [
{
"start": {
"identity": 0,
"labels": [
"Person",
"Famous",
"Rich"
],
"properties": {
"name": "Keanu Reeves",
"born": 1964
}
},
"relationship": {
"identity": 263,
"start": 0,
"end": 8,
"type": "ACTED_IN",
"properties": {
"roles": [
"Neo"
]
}
},
"end": {
"identity": 8,
"labels": [
"Movie"
],
"properties": {
"tagline": "Free your mind",
"title": "The Matrix Reloaded",
"released": 2003
}
}
}
],
"length": 1.0
}
cypher-shell takes a more literal approach on what it outputs:
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| path |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| (:Person:Famous:Rich {name: "Keanu Reeves", born: 1964})-[:ACTED_IN {roles: ["Neo"]}]->(:Movie {tagline: "Free your mind", title: "The Matrix Reloaded", released: 2003}) |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
The Neo4j Browser does a lot of nice things to aid in visualization and development, but sometimes it's useful to fall back to cypher-shell.
04-13-2021 07:17 AM
Ok thank you.
What about querying patterns from virtual graph returned as an output from a user-defined procedure? is there a way to do it?
04-07-2021 11:45 PM
I noticed that the MATCH clauses after the UNWIND are not querying from the virtual nodes and relationships returned by the procedure. They query from the database.
Is there a way to query the virtual graph returned by the procedure?
Thank you
04-08-2021 12:14 AM
Yes, sorry, I misunderstood that part of the question. I have the same issue as you do with apoc.meta.sampleGraph which returns a "virtual graph".
I know I've done it once in the past (querying the virtual graph results), but cannot remember how. I'll think on it.
04-12-2021 01:34 AM
Hi,
I found the following link https://s3.amazonaws.com/artifacts.opencypher.org/website/ocim1/slides/openCypher-GraphViews.pdf
describing how to create a view from virtual graph and apply queries on it. This might be helpful for me.
Is this implemented or not yet?
If it is not released yet, is there a way to try it?
Thank you
04-13-2021 08:15 AM
That pdf is from presentation that was used for a thesis defense and does not represent an implementation pattern. You'd want to use apoc virtual nodes
04-17-2021 03:10 AM
The pdf represent the creation of a view (which is a virtual graph) and querying from the view. This will solve my problem if it is doable on Neo4j.
I didn't understand what you mean by 'want apoc virtual nodes'. And how this will help me in querying pattern from virtual graphs?
04-23-2021 12:48 AM
Hello,
Do you have an answer on how to "query the virtual graph results" ?
Thank you
04-08-2021 12:47 AM
Yes apoc.meta.graphSample return a Stream of graphResult (virtual nodes and relationships) as my procedure do.
My objective is to query patterns from the output and do some analytics.
Thank you and i will be waiting for your response.
04-08-2021 07:08 AM
Hi ,
You can use similar to below query
CALL db.propertyKeys() YIELD propertyKey AS prop
MATCH (n)
WHERE n[prop] IS NOT NULL
RETURN prop, count(n) AS numNodes
04-08-2021 07:32 AM
Hi,
Thank you for your response, but db.propertyKeys() do not return virtual graph.
And the MATCH(n) in your query matches stored nodes and not output nodes from a procedure.
My procedure returns virtual graph and i need to match patterns from this virtual graph and not patterns from the stored nodes and relationships.
All the sessions of the conference are now available online