cypher - Neo4J: How to find unique nodes from a collection of paths -
i using neo4j solve realtime normalization problem. lets have 3 places 2 different sources. 1 source 45
gives me 2 places in-fact duplicates of each other, , 1 source 55
gives me 1 correct identifier. however, place identifier (duplicate or not), want find closest set of places unique feed identifier. data looks so:
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"})
i have connected these nodes matching nodes can normalization on data. instance:
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)
when want find closest matches start place id, can run match on paths start node, ranked cost, ie:
match p=(a:place {placeid:789, feedid:55})-[*..4]-(d:place) none (n in nodes(p) size(filter(x in nodes(p) n = x))> 1) p, reduce(costaccum = 0, n in filter(n in nodes(p) has(n.cost)) | costaccum+n.cost) costaccum order costaccum return p, costaccum
however, there multiple paths same places, same node replicated multiple times when querying this. possible collect nodes , costs, , return distinct subset (for e.g., give me best result feed 45
, 55
?
how return distinct set of paths, ranked cost, , unique feed identifier? structuring type of problem wrong?
please help!
you can collect paths each place d, , take best path in each collection (since sorted collected)
match p=(a:place {placeid:789, feedid:55})-[*..4]-(d:place) d, collect(p) paths, reduce(costaccum = 0, n in filter(n in nodes(p) has(n.cost)) | costaccum+n.cost) costaccum order costaccum return head(paths) p, costaccum
Comments
Post a Comment