什么是一个节点的密码查询,而不是另一个节点?

时间:2013-05-14 01:01:54

标签: neo4j cypher

我的图表看起来像这样 -

customer-hasOrder->order
order-hasLineItem->orderLineItem
orderLineItem-hasService->service
orderLineItem-providedOn->providedOnDate
orderLineItem-providedBy->vendor

我需要确定某个特定服务,例如名为“A”的服务是否在某个日期提供给某个客户而没有其他服务,比如名为“B”的服务,也是由同一供应商在同一天提供的。无论客户在特定日期提交了多少订单,这都应该是真的。换句话说,当客户订购“A”时,服务“A”应始终由同一供应商提供服务“B”。

以下是我必须在同一天找到“A”但不是“B”的客户 -

START  
    s1 = node:service(id="A"),
    s2 = node:service(id = "B"),
MATCH
    customer-[:hasOrder]->(o1)-[:hasLineItem]->li1-[:hasService]->s1,
    customer-[:hasOrder]->(o2)-[:hasLineItem]->li2-[r?:hasService]->s2,
    ol1-[:providedBy]->p1,
    ol2-[:providedBy]->p2,
    ol1-[:providedOn]->d1,
    ol2-[:providedOn]->d2
WHERE
    d1 = d2 AND p1 = p2 AND r is null
    RETURN customer

我决定在我的鼻子开始出血后来到这里试图找出Cypher的查询。

1 个答案:

答案 0 :(得分:1)

从不同的角度来看,聚合在相关节点(客户,提供商,日期)上

START  
    service = node:service("id:(A B)"),
MATCH
    customer-[:hasOrder]->(order)-[:hasLineItem]->item-[:hasService]->service,
    item-[:providedBy]->provider,
    item-[:providedOn]->date
WITH
    customer,provider,date,count(distinct service) as service_count
WHERE
    service_count = 1
RETURN customer,provider,date

您还可以查看路径表达式,以检查相同的提供者和日期是否存在另一个行项目,但不存在服务B.

START  
    s1 = node:service(id="A"),
    s2 = node:service(id="B"),
MATCH
    customer-[:hasOrder]->(o1)-[:hasLineItem]->li1-[:hasService]->s1,
    customer-[:hasOrder]->(o2)-[:hasLineItem]->li2
    li1-[:providedBy]->provider,
    li2-[:providedBy]->provider,
    li1-[:providedOn]->date,
    li2-[:providedOn]->date
WHERE
    NOT (li2-[:hasService]->s2)

RETURN customer