简体   繁体   English

Cypher / Neo4j:如何匹配与n个“连续”节点相关的节点

[英]Cypher/Neo4j: How to match nodes related to n “consecutive” nodes

I'm trying to match listing availabilities in my graph (Airbnb-like). 我正在尝试匹配图表中的列表可用性(类似于Airbnb)。

  • The graph has 1 node per date, and consecutive days are linked by a NEXT_DAY edge. 该图每个日期有1个节点,并且连续的天数由NEXT_DAY边链接。
  • Listings are linked to day nodes by an AVAILABLE edge (if they are available) 清单通过可用边缘(如果可用)链接到日节点

I'm trying to write a query which returns all listings which are available from start_date to end_date. 我正在尝试编写一个查询,该查询返回从start_date到end_date可用的所有列表。

I know the following FOREACH syntax is incorrect but I may help giving the general idea: 我知道以下FOREACH语法不正确,但可以帮助您大致理解:

MATCH period=(a)-[:NEXT_DAY*]->(c)
WHERE a.date="2013-01-20" AND c.date="2013-01-24"

MATCH (listing:Listing)-[:AVAILABLE]->(d:Date)
FOREACH (d IN nodes(period))

RETURN listing

Bonus question: How could I proceed if I wanted to score listings proportionally to their availability match with the query (eg 100% if the listing is available during the whole period, 50% if it is only available half of the period) ? 额外的问题:如果我想按与查询匹配的可用性按比例对列表进行评分(例如,如果列表在整个期间都可用,则为100%;如果仅在该期间的一半可用,则为50%)?

Thanks in advance :) 提前致谢 :)

Benjamin 本杰明

You can use UNWIND to get get the nodes from your date path and MATCH them to Listing . 您可以使用UNWIND来获取日期路径中的节点并将其MATCHListing

MATCH period=(a)-[:NEXT_DAY*]->(c)
WHERE a.date="2013-01-20" AND c.date="2013-01-24"

// UNWIND gets a collection of ndoes and transforms them to single/matchable nodes
UNWIND nodes(period) as nodes_in_period
MATCH (nodes_in_period)<-[:AVAILABLE]-(listing:Listing)

RETURN listing

And for the bonus question: If I get it right, you can count the number of AVAILABLE relationships from Listing to Date and compare it to the number of Date nodes in your period. 还有一个额外的问题:如果我做对了,您可以计算从ListingDateAVAILABLE关系数,并将其与您期间内的Date节点数进行比较。 OPTIONAL MATCH might help: OPTIONAL MATCH可能会有所帮助:

MATCH period=(a)-[:NEXT_DAY*]->(c)
WHERE a.date="2013-01-20" AND c.date="2013-01-24"
UNWIND nodes(period) as nodes_in_period

OPTIONAL MATCH (nodes_in_period)<-[a:AVAILABLE]-(listing:Listing)

// return the listing, the number of available relationships 
// and the number of days in period
RETURN listing, count(a), count(nodes_in_period)

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

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