Query data from procedures result

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

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 ?

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:

  • only procedure (text) photo shows that the relationships are empty fields (However the graph visualisation is built as i want to build it)
  • procedure with processing photo shows a simple example of what i want to do. But I'm not getting results. The match clause seems not querying from the procedure results.
    Note: My procedure returns Stream of GraphResult (created by apoc).
    Hope you can help me and thanks again.

Hassan

  • "Relationships are empty fields" -> sorry, this is something happening inside your procedure's code so hard to say what's happening from the outside
  • "Match clause not querying from the procedure results" -> Yes indeed, in the MATCH, you're not referencing any of the results from your procedure.
    You could try replacing b by n to reference your procedure's output nodes (that might be confusing though), or removing the UNWIND, and adding a "WHERE b in nodes" after your MATCH.

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.

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

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.

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.

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

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.

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

Use cypher-shell if you want to see the relationship types in the output.

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

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.

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?

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

  • 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?