简体   繁体   English

使用 neo4jphp 获取用户帖子,包括喜欢和评论

[英]use neo4jphp to get user posts, with likes and comments

I am new to neo4j and currently using Neo4jphp library with cypher to store and retrieve nodes.我是 Neo4j 的新手,目前使用 Neo4jphp 库和 cypher 来存储和检索节点。 My logic is fairly simple.我的逻辑很简单。 I want to get a node (post) and get all its likes (count of likes) and all comments (nodes) with their commenters (nodes of label user) and be able to parse the ResultSet in a php array to later encode it to Json.我想获取一个节点(帖子)并获取其所有喜欢(喜欢计数)和所有评论(节点)及其评论者(标签用户的节点),并且能够解析 php 数组中的 ResultSet 以便稍后将其编码为杰森。

The cypher statement I'm using is:我使用的密码语句是:

MATCH (user:User)-[:POSTED]->(post) 
WHERE post.UserID = '13' 
AND id(post) = 106  
OPTIONAL MATCH ()-[rel:LIKES]-(post)
OPTIONAL MATCH (post)-[:HAS]-(comment:Comment)<-[:COMMENTED]-(commenter:User) 
RETURN user, post, count(rel) AS Likes, rel, comment, commenter 

The match retrieves all nodes correctly (this post has no likes though):匹配正确检索所有节点(尽管这篇文章没有赞):

Neo4j
(source: bookintransit.com ) (来源: bookintransit.com

How do I get the correct representation of this graph using the library?如何使用库获得此图的正确表示? Also note that for simplicity I retrieved one post node (id:106) in this example.另请注意,为简单起见,我在此示例中检索了一个帖子节点 (id:106)。 But I'd like to retrieve a list of posts when I use the code in my application.但是我想在我的应用程序中使用代码时检索帖子列表。

This is the php code I'm using:这是我正在使用的 php 代码:

$queryString = "MATCH (user:User)-[:POSTED]->(post) WHERE post.UserID = {qUser} OPTIONAL MATCH ()-[rel:LIKES]-(post) OPTIONAL MATCH (post)-[:HAS]-(comment:Comment)<-[:COMMENTED]-(commenter:User) RETURN user, post,count(rel) AS Likes, rel, comment, commenter ORDER BY post.TS DESC LIMIT ".$NumberOfPosts;
        $query = new Everyman\Neo4j\Cypher\Query($client, $queryString, array('qUser' => $UserID));
        $result = $query->getResultSet();
        if(count($result)>0){
            foreach($result AS $row){

                // How do I get the correct graph in a php array to parse to json
            }

With neo4jphp I have no idea if it is feasible.使用 neo4jphp 我不知道它是否可行。 BTW it is possible with neoclient :顺便说一句, neoclient是可能的:

NeoClient use a response formatter handling the graph result data content, so you get in result object what your graph looks like in the image you posted : NeoClient 使用响应格式化程序处理图形结果数据内容,因此您可以在结果对象中获得您发布的图像中图形的外观:

$result = $client->sendCypherQuery($yourQuery, $parameters);

$posts = $result->getNodes('Post');
$post = $result->getSingleNode('Post');
$comments = $post->getInboundRelationships('HAS');
foreach ($comments->getEndNode() as $comment) {
   echo $comment->getProperty('text');
   $user = $comment->getSingleRelationship('COMMENTED')->getEndNode();
   echo $user->getProperty('username');
}

As you can see, the result set is just a graph representation of your response, you just need to navigate through it.如您所见,结果集只是您响应的图形表示,您只需要浏览它即可。

I think you are looking for something like this.我想你正在寻找这样的东西。 Find all the posts of a user.查找用户的所有帖子。 With each post optionally find all of the comments and like for each;每个帖子都可以选择找到所有评论和每个帖子的赞; put the comments and related users into a collection;将评论和相关用户收藏; count the number of likes;计算点赞数; and return it all per post.并在每个帖子中全部返回。

MATCH (u:User {name: "13})-[:POSTED]-(p:Post)
WITH p
OPTIONAL MATCH (p)-[:HAS]-(c:Comment)-(u:User)
OPTIONAL MATCH (p)-[:HAS]-(l:Like)
WITH p
, [c, u] AS comment
, count(l) AS likes
RETURN p, collect(comment), likes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM