问题
Actually i'm modelling TEI-encoded XML-Text in a graph (words as a chain of nodes) and i want to find shortestPaths and only the very shortest path in a graph. My query looks like
MATCH (w0:XmlWord)-[:NEXT*..6]->(n:XmlTag {_name:'lb'})-[:NEXT*..6]->(w1:XmlWord)
RETURN id(w0), id(w1);
And I need only the shortest possible path but neo4j gives me all possibilities until to the 6th step. The result should be the nodes Vorträge, über, des, and Freiherrn
Neo4j gives me back all possible combinations until to the 6th step.
If someone needs access to a sample-database just let me know.
回答1:
This is a little tough when you're using multiple variable-length relationships in the same path. You can however order the results by path length and filter for the ones with the minimum length.
MATCH path = (:XmlWord)-[:NEXT*..6]->(:XmlTag {_name:'lb'})-[:NEXT*..6]->(:XmlWord)
WITH length(path) as length, collect(path) as paths
ORDER BY length ASC
LIMIT 1
UNWIND paths as path
WITH head(nodes(path)) as first, last(nodes(path)) as last
RETURN id(first), id(last);
来源:https://stackoverflow.com/questions/47637780/how-to-find-a-path-in-cypher-the-ungreedy-way