1
votes

When I make a call to the Neo4j REST API, of the format results = gdb.query(query, data_contents=constants.DATA_GRAPH), I get back many more results, and results that are more complex, than I had expected.

Cypher version: CYPHER 2.2

For example, in a graph that has this arrangement of nodes...

(Bob) --> (Amy) --> (Cal)

... and the query ...

MATCH (n)
OPTIONAL MATCH (a)-[r]-(b)
RETURN DISTINCT n, r

... one of the results returned is as follows:

{ "relationships": [
    { "id":"270"
    , "type":"LIKES"
    , "startNode":"134"
    , "endNode":"136"
    , "properties":{}
    }
  ]
, "nodes": [
    { "id":"134"
    , "labels":["Person"]
    , "properties":{"name":"Amy"}
    }
  , { "id":"135"
    , "labels":["Person"]
    , "properties":{"name":"Bob"}
    }
  , { "id":"136"
    , "labels":["Person"]
    , "properties":{"name":"Cal"}
    }
  ]
}

If I understand correctly, this indicates a direct relationship between Amy (134) and Cal (136). As far as I can see, Bob has no place in the path between Amy and Cal. So why is Bob appearing in this entry at all?

I also get duplicate entries. For example, this entry appears twice:

{ "relationships": [
    { "id":"264"
    , "type":"LIKES"
    , "startNode": "134"
    , "endNode":"136"
    ,"properties":{}
    }
  ]
, "nodes": [
    { "id":"134"
    , "labels":["Person"]
    , "properties":{"name":"Amy"}
    }
  , { "id":"136"
    , "labels":["Person"]
    , "properties":{"name":"Cal"}
    }
  ]
}

In my tests, I see rows with 2 or 3 nodes. Is it ever possible to see more nodes in one row? Is it safe to assume that, if a relationship entry includes a startNode and an endNode, that there is a direct link from one to the other, and that any additional nodes that appear in the nodes section for that row can be ignored?

Is there somewhere where I can find a complete explanation of how the graph output is calculated?

1

1 Answers

1
votes

Because you have a typo and don't refer to n at all in your optional match.

Also you should use labels!

MATCH (n)
OPTIONAL MATCH (n)-[r]-()
RETURN DISTINCT n, r