我如何在 neo4j 中编写这个查询?

发布于 2024-11-27 22:43:21 字数 355 浏览 0 评论 0原文

我对 neo4j 和图形数据库都很陌生。我正在制作一个应用程序原型,我不知道应该如何编写这些查询

我有这个域:

User 餐厅 审查 TypeOfFood

因此一家餐馆有一个或多个 TypeOfFood,用户留下有关餐馆的评论。用户有一些喜欢的食物,与餐厅销售的食物类型相匹配。用户之间也以典型的朋友关系相互关联。

我想写的一些查询:

  • 给我所有我的朋友评价为 3 星或以上的餐厅,这些餐厅制作的食物是我喜欢的(不包括我已经评论过的那些餐厅)

  • 向我推荐我可能认识的朋友(我想这应该是这样的“所有是我朋友的朋友但还不是的朋友我的,按某事排序)

I'm very new to neo4j and to graph database in general. I'm prototyping an app, and I don't know how should i write these queries

I've this domain:

User
Restaurant
Review
TypeOfFood

So a Restarurant have one or many TypeOfFood, the User leaves reviews about restaurants. The User have some preferred foods, matching the TypeOfFood a restaurant sell. Also Users are related to each other with the typically friend relationship.

Some of the queries I'm trying to write:

  • Give me all the restaurants that my friends have rated with 3 or more stars that make the kind of food I like (exclude those restaurants that I already reviewed)

  • Suggest me friends I may know (I guess this should be something like "all the friends that are friends of my friends but no yet mine, order by something)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-12-04 22:43:21

使用 Neo4j 的 Cypher 查询语言,您可以像这样编写查询:

选择评分最高的 20 家餐厅,按星级和评论数量排序

start user=(users,name,'Nico')
match user-[:FRIEND]->friend-[r,:RATED]->restaurant-[:SERVES]->food,
      user-[:LIKES]->food,user-[:RATED]->rated_by_me
where r.stars > 3
return restaurant.name, avg(r.stars), count(*)
order by avg(r.stars) desc, count(*) desc 
limit 20

朋友的朋友

start user=(users,name,'Nico')
match user-[:FRIEND]->friend->[:FRIEND]->foaf
return foaf, foaf.name

您可以在 Neo4j Webadmin Console 在您的数据集上,也可以在 Neo4j-shell 中,远程通过通过 Spring Data 的 Cypher-Rest-Plugin图表

还有一个讨论 cypher 中类似查询的截屏

您还可以使用 GremlinNeo4j-Traversers 或通过 getRelationships 手动遍历,如果你愿意。

Using Neo4j's Cypher query language you could write your queries like this:

Selecting the top-20 best rated restaurants, sorted by stars and number of reviews

start user=(users,name,'Nico')
match user-[:FRIEND]->friend-[r,:RATED]->restaurant-[:SERVES]->food,
      user-[:LIKES]->food,user-[:RATED]->rated_by_me
where r.stars > 3
return restaurant.name, avg(r.stars), count(*)
order by avg(r.stars) desc, count(*) desc 
limit 20

Friends of a Friend

start user=(users,name,'Nico')
match user-[:FRIEND]->friend->[:FRIEND]->foaf
return foaf, foaf.name

You can execute these cypher queries in the Neo4j Webadmin Console on your dataset, but also in the neo4j-shell, remotely via the Cypher-Rest-Plugin via Spring Data Graph.

There is also a screencast discussing similar queries in cypher.

You can also use Gremlin, Neo4j-Traversers or manual traversing via getRelationships if you'd like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文