Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-05-2021 06:59 PM
How do I get properties of edges and not just nodes from a query to the python driver?
I have a query to try and find variable length paths between two nodes, like
match path = (n1:page{name:'start-page'})-[*..2]->(n2:page) return path limit 5
In the neo4j browser, table view I can see a table with a segments
property in the middle with all the data on each connecting edges (see below)
But when I send the same query to cypher/python driver, I don't get any segments, just the start/end nodes.
What query can I use to get the same detail on the path / edges as below?
Do I need to use subgraphs or somehow unwind the paths between?
{
"start": {
"identity": 95, "labels": ["page"],
...
"segments": [
{
"start": {
"identity": 95,
"relationship": {
"identity": 152,
"start": 95,
"end": 97,
"type": "nav",
"properties": {
"start_page": "start-page",
"weight": 2,
...
"length": 1.0
}
I did try a more complex with
type query, which works well in neo4j browser, but from the python driver I still just get the pages, and no data or properties on the edges between each node:
// unex charge to end by weight
match path = (n1:page{name:'start-page'})-[*..5]->(n2:page{name:'end_session'})
with n1 as startPage,
relationships(path) as edges,
n2 as endPage
return startPage, edges, endPage
order by edges[0].weight desc
limit 50
This just seems to put page
data into the edges
too:
{
'edges': [({
'flow': 'BILL',
'name': 'start-page',
'type': 'page',
'weight': 18
},
'nav', {
'flow': 'BILL',
'name': 'end_session',
'type': 'page'
})
],
'endPage': {
'flow': 'BILL',
'name': 'end_session',
'type': 'page'
},
'startPage': {
'flow': 'BILL',
'name': 'start-page',
'type': 'page',
'weight': 18
}
}
Confused why I can't get edge data, and also what magic the neo4j query browser is doing to get queries to work, in a completely different way than the python driver. >.<
07-06-2021 02:35 AM
Hello @D_C
You should have a look at the properties() function:
MATCH path = (n1:page {name:'start-page'})-[*..5]->(n2:page {name:'end_session'})
WITH n1 AS startPage, [p IN relationships(path) | properties(p)] AS edges, n2 AS endPage
RETURN startPage, edges, endPage
ORDER BY edges[0].weight DESC
LIMIT 50
Regards,
Cobra
07-06-2021 03:49 AM
Thanks @Cobra actually didn't know about this. That does get a bit further.. it allows me to get the list of edges data, but not any data on in-between nodes.
eg I can get
{startNode},
[ {edge}, {edge}, {edge} ... ]
{ endNode }
The benefit of the patterns like -[*..2]-
is that I can have a varying number of edges AND nodes.
What I really want is just a sequence of objects, of varying types, eg:
[
[ {node}-{edge}-{node} ], // first path
[ {node}-{edge}-{node}-{edge}-{node} ] // longer path
]
Which is exactly what I see in the neo browser in the table view as 'segments'
I'm confused why cypher queries return one type of data structure in the neo-browser, but completely different and less useful data when run from the python driver?
Do I need to use subgraphs or something more complex?
07-06-2021 01:16 PM
Hi,
there is an API documentation on paths that you can use to deconstruct them
https://neo4j.com/docs/api/python-driver/current/api.html#path
07-06-2021 08:44 PM
I can't really deconstruct the paths if there's no data there.
There is literally just start
and end
nodes and nothing in-between when I look at .data()
.
I did look at .graph
but that also seems not to work as expected
The super-frustrating thing is the neo browser shows all the data I want in the table view.
So neo4j core DB must be capable of giving the data...
Is this a case of "private APIs" that return data via.. maybe bolt protocol?
Are the cypher queries that I type into the neo browser somehow modified/augmented to get useful data?
does the official python lib not support paths?
How about the node drivers, do they behave closer to expectation?
neo4j works well as a kind of lightweight data exploration app but using those same queries via the API is another matter.
07-07-2021 02:28 AM
I don't know me being a python-noob I didn't have problems following the API docs to come up with this:
https://neo4j.com/docs/api/python-driver/current/api.html#graph-data-types
from neo4j import GraphDatabase,basic_auth
driver = GraphDatabase.driver("bolt://localhost",auth=basic_auth("neo4j","test"))
cypher_query = "MATCH p=(n)-[r]->(m) RETURN p LIMIT 5"
with driver.session(database="neo4j") as session:
g = session.read_transaction(
lambda tx: tx.run(cypher_query,).graph())
for n in g.nodes:
print("id %s labels %s props %s" % (n.id, n.labels, n.items()))
for r in g.relationships:
print("id %s type %s start %s end %s props %s" % (r.id, r.type, r.start_node.id, r.end_node.id, r.items()))
output
id 10 labels frozenset({'LittleSisEntity', 'Person'}) props dict_items([('littleSisId', '102193'), ('name', 'DONALD J. TRUMP'), ('pageRank', 0.15000000000000002), ('wcc_partition', 9)])
id 473 labels frozenset({'LittleSisEntity', 'Person'}) props dict_items([('littleSisId', '13443'), ('name', 'JOHN SIDNEY MCCAIN'), ('pageRank', 0.15000000000000002), ('wcc_partition', 472)])
id 41 labels frozenset({'LittleSisEntity', 'Person'}) props dict_items([('littleSisId', '13191'), ('name', 'HILLARY DIANE RODHAM'), ('pageRank', 0.15000000000000002), ('wcc_partition', 40)])
id 2388 labels frozenset({'LittleSisEntity', 'Person'}) props dict_items([('littleSisId', '34136'), ('name', 'WILLARD MILTON ROMNEY'), ('pageRank', 0.15000000000000002), ('wcc_partition', 2387)])
id 15368 labels frozenset({'LittleSisEntity', 'Person'}) props dict_items([('littleSisId', '13536'), ('name', 'CHARLES B. RANGEL'), ('pageRank', 0.15000000000000002), ('wcc_partition', 15367)])
id 16822 labels frozenset({'Organization', 'LittleSisEntity'}) props dict_items([('littleSisId', '28778'), ('name', 'REPUBLICAN NATIONAL COMMITTEE')])
id 47647 type DONATION start 10 end 473 props dict_items([('relDetail', 'Campaign Contribution'), ('source', 'LittleSis')])
id 72375 type DONATION start 10 end 41 props dict_items([('relDetail', 'Campaign Contribution'), ('source', 'LittleSis')])
id 72385 type DONATION start 10 end 2388 props dict_items([('relDetail', 'Campaign Contribution'), ('source', 'LittleSis')])
id 72377 type DONATION start 10 end 15368 props dict_items([('relDetail', 'Campaign Contribution'), ('source', 'LittleSis')])
id 72384 type DONATION start 10 end 16822 props dict_items([('relDetail', 'Campaign Contribution'), ('source', 'LittleSis')])
All the sessions of the conference are now available online