如何以不愉快的方式在密码中找到路径 [英] How to find a path in cypher the ungreedy way

查看:70
本文介绍了如何以不愉快的方式在密码中找到路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我是在图形中建模TEI编码的XML文本(单词是节点链),我想找到图形中的shortestPaths和仅最短路径. 我的查询看起来像

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);

我只需要最短的路径,但是neo4j给了我所有的可能性,直到第六步.结果应为节点Vorträgeüber des Freiherrn

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给我所有可能的组合,直到第六步为止.

Neo4j gives me back all possible combinations until to the 6th step.

如果有人需要访问示例数据库,请告诉我.

If someone needs access to a sample-database just let me know.

推荐答案

当您在同一路径中使用多个可变长度关系时,这有点困难.但是,您可以按路径长度对结果进行排序,并过滤长度最小的结果.

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);

这篇关于如何以不愉快的方式在密码中找到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆