[英]How to get all node names between two nodes using neo4jPHP
Till now I was using PHP Rest Api in order to send requests with cypher queries and get a response back. 直到现在我才使用PHP Rest Api来发送带有cypher查询的请求并获得回复。 The response is a huge string which makes it difficult to parse and can not be transformed to JSON. 响应是一个巨大的字符串,这使得难以解析并且无法转换为JSON。 I now installed Neo4jPHP and I am trying to figure out how to write the same query I had in cypher. 我现在安装了Neo4jPHP,我试图弄清楚如何编写我在cypher中的相同查询。 This is my query: 这是我的查询:
MATCH (n:RealNode)-[r:contains*]-(z) WHERE n.gid='123' RETURN n,z;")
What I actually want is to get a list of all the names of the nodes (name is a property inside each node) which is related to my n node. 我真正想要的是获得与我的n节点相关的节点的所有名称(名称是每个节点内的属性)的列表。 How do I do this? 我该怎么做呢?
I can not find many examples for Neo4jPHP onnline and the ones I found seem not to work. 我找不到Neo4jPHP onnline的很多例子,我发现的那些例子似乎无法工作。 I downloaded the latest version from here ( https://github.com/jadell/neo4jphp ). 我从这里下载了最新版本( https://github.com/jadell/neo4jphp )。
Thanks D. 感谢:D。
RE-EDITED 重新编辑
I try this query in neo4j Server: 我在neo4j Server中尝试这个查询:
MATCH (n)-[r:KNOWS*]-(z) WHERE n.name='Arthur Dent' AND z.name='Ford Prefect' RETURN n,z,r;
and I get all the 3 nodes which are connected to each other. 我得到了所有相互连接的3个节点。 The same query through neo4jPHP will return only the name of one node. 通过neo4jPHP的相同查询将仅返回一个节点的名称。 Why is this happening? 为什么会这样?
$querystring="MATCH path=(n:RealNode {gid:'58731'})-[:contains*]-(z) RETURN [x in nodes(path) | x.id] as names";
$query=new Everyman\Neo4j\Cypher\Query($client,$querystring);
$result=$query->getResultSet();
print_r($result);
foreach($result as $row){
echo $row['x']->getProperty('name') . "\n";
}
On Cypher level you might use a query like: 在Cypher级别,您可以使用如下查询:
MATCH path=(n {name:'Arthur Dent'])-[:KNOWS*]-(z {name:'Ford Perfect'})
RETURN [x in nodes(path) | x.name] as names
You assign a variable to a pattern, here path
. 您将变量分配给模式,此处为path
。 In the RETURN
you iterate over all nodes along that path and extract its name
property. 在RETURN
您遍历该路径上的所有节点并提取其name
属性。
2 additional hints: 另外2个提示:
Person
to your nodes and use a declarative index ( CREATE INDEX ON :Person(name)
) to speed up the look up for start/end node of your query 考虑为您的节点分配标签,例如Person
,并使用声明性索引( CREATE INDEX ON :Person(name)
)来加速查询查询的开始/结束节点 [:KNOWS*]
consider using an upper limit, depending on the size and structure of your graph this can get rather expensive. 对于可变路径长度匹配[:KNOWS*]
考虑使用上限,根据图表的大小和结构,这可能会相当昂贵。 [:KNOWS*10]
to limit on 10th degree. [:KNOWS*10]
限制在10度。 After lots of trying and some help from Stefan Armbruster I made it work using Neo4jPHP. 经过Stefan Armbruster的大量尝试和一些帮助后,我使用了Neo4jPHP。 This is how it looks: 这是它的样子:
$client = new Everyman\Neo4j\Client();
$querystring="MATCH path=(n {gid:'58731'})-[:contains*]-(z) RETURN LAST([x in nodes(path) | x.id]) as names";
$query=new Everyman\Neo4j\Cypher\Query($client,$querystring);
$result=$query->getResultSet();
foreach($result as $resultItem){
$resultArray[] = $resultItem['n'];
}
print_r($resultArray); // prints the array
Neo4jPHP is very handy tool but not very popular. Neo4jPHP是非常方便的工具,但不是很受欢迎。 Small community and few examples online. 小社区和在线的几个例子。 Hope this helps someone. 希望这有助于某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.