Neo4J:如何从路径集合中查找唯一节点

时间:2015-04-25 11:34:23

标签: neo4j cypher

我正在使用neo4j来解决实时规范化问题。假设我有来自2个不同来源的3个位置。 1个来源45给了我2个实际上彼此重复的地方,1个来源55给了我1个正确的标识符。但是,对于任何地点标识符(重复或不重复),我想找到由Feed标识符唯一的最接近的地点集。我的数据如下:

CREATE (a: Place {feedId:45, placeId: 123, name:"Empire State", address: "350 5th Ave", city: "New York", state: "NY", zip: "10118" })
CREATE (b: Place {feedId:45, placeId: 456, name:"Empire State Building", address: "350 5th Ave", city: "New York", state: "NY"})
CREATE (c: Place {feedId:55, placeId: 789, name:"Empire State", address: "350 5th Ave", city: "New York", state: "NY", zip: "10118"})

我已通过匹配节点连接这些节点,因此我可以对数据进行一些规范化。例如:

MERGE (m1: Matching:NameAndCity { attr: "EmpireStateBuildingNewYork", cost: 5.0 })
MERGE (a)-[:MATCHES]-(m1)
MERGE (b)-[:MATCHES]-(m1)
MERGE (c)-[:MATCHES]-(m1)
MERGE (m2: Matching:CityAndZip { attr: "NewYork10118", cost: 7.0 })
MERGE (a)-[:MATCHES]-(m2)
MERGE (c)-[:MATCHES]-(m2)

当我想查找起始位置ID中最接近的匹配项时,我可以在起始节点的所有路径上运行匹配,按成本排序,即:

MATCH p=(a:Place {placeId:789, feedId:55})-[*..4]-(d:Place)
WHERE NONE (n IN nodes(p)
        WHERE size(filter(x IN nodes(p)
                          WHERE n = x))> 1)
WITH    p,
        reduce(costAccum = 0, n in filter(n in nodes(p) where has(n.cost)) | costAccum+n.cost) AS costAccum
        order by costAccum
RETURN p, costAccum

但是,由于存在多个到相同位置的路径,因此在查询时会多次复制相同的节点。是否有可能收集节点及其成本,然后只返回一个不同的子集(例如,从Feed 4555给我最好的结果?

我如何返回一组不同的路径,按费用排名,并按Feed标识符唯一?我构建这种类型的问题是错误的吗?

请帮忙!

1 个答案:

答案 0 :(得分:0)

您可以收集每个地点的所有路径d,然后在每个集合中选择最佳路径(因为它们将被分类然后收集)

MATCH p=(a:Place {placeId:789, feedId:55})-[*..4]-(d:Place)
WITH d, collect(p) as paths,
        reduce(costAccum = 0, n in filter(n in nodes(p) where has(n.cost)) | costAccum+n.cost) AS costAccum
        order by costAccum
RETURN head(paths) as p, costAccum