简体   繁体   English

使用 nodejs 从 Neo4j 获取数据

[英]Fetching data from Neo4j using nodejs

I am using Neo4j for the first time with nodejs.我第一次在 nodejs 中使用 Neo4j。 I am trying a particular usecase which is " User can search a movie, for eg: Titanic, for recommendation of other similar movies the result must contain movies of the same genre, movies from the same director. " When i search for a movie, i am able to get the related nodes like actor, director and genre of the movie.我正在尝试一个特定的用例,它是“用户可以搜索电影,例如:泰坦尼克号,为了推荐其他类似电影,结果必须包含相同类型的电影,来自同一导演的电影。”当我搜索电影时,我能够获得电影的演员、导演和流派等相关节点。 what i want next are the other movies related to this director and genre.接下来我想要的是与这个导演和类型相关的其他电影。

Here 'title' is the name of the movie i am passing in REST API Get method.这里的“标题”是我在 REST API Get 方法中传递的电影的名称。

session.run("MATCH (n:movie{name : $title}) -[:Acted_In|:Directed |: Belong_To]-(r) Return r ",
                        { title: req.params.name }

                        )

        .then(function (data) {

            data.records.forEach(function(record){
            console.log("record._fields[0].properties = ",record._fields[0].properties.name);
            result.push(record._fields[0].properties.name);  
              })
                 console.log(result);

          })
                        ```

This Cypher query should work for your use case ( "User can search a movie, for eg: Titanic, for recommendation of other similar movies the result must contain movies of the same genre, movies from the same director." ):此 Cypher 查询应该适用于您的用例( “用户可以搜索电影,例如:泰坦尼克号,对于其他类似电影的推荐,结果必须包含相同类型的电影,来自同一导演的电影。” ):

MATCH (m:movie)
WHERE m.name = $title
MATCH (m)-[:Belong_To]->()<-[:Belong_To]-(x)
WITH m, COLLECT(x) AS xs
MATCH (m)<-[:Directed]-()-[:Directed]->(y)
WITH m, xs, COLLECT(y) AS ys
UNWIND (xs + ys) AS otherMovie
RETURN m, COLLECT(DISTINCT otherMovie) AS recommendations

I assume the Belong_To relationship points to a genre.我假设Belong_To关系指向一个流派。 I also assume all movies have at least one genre and one director;我还假设所有电影都至少有一种类型和一位导演; if that is not true, then the relevant MATCH should be an OPTIONAL MATCH .如果这不是真的,那么相关的MATCH应该是OPTIONAL MATCH

This query uses the aggregating function COLLECT to separately collect the movies having the same genres ( xs ), and the same directors ( ys ).此查询使用聚合 function COLLECT来分别收集具有相同类型 ( xs ) 和相同导演 ( ys ) 的电影。 It then UNWIND s the combination of xs and ys so that it can use COLLECT(DISTINCT...) to get a list of distinct movies.然后它UNWINDxsys的组合,以便它可以使用COLLECT(DISTINCT...)来获取不同电影的列表。

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

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