cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Path node list query

Env - Neo4j 4.0 enterprise
OS - Linux
Plugins - GDS and apoc

I have a data in neo4j like below -

CREATE (a:hotel {hotel_id:'A'}),
  (b:hotel {hotel_id:'B'}),
  (c:hotel {hotel_id:'C'}),
  (d:hotel {hotel_id:'D'}),
  (e:hotel {hotel_id:'E'}),
  (f:hotel {hotel_id:'F'}),
  (g:hotel {hotel_id:'G'}),
  (h:hotel {hotel_id:'H'}),
  (i:hotel {hotel_id:'I'}),
  (a)-[:HAS]->(b),
  (b)-[:HAS]->(c),
  (c)-[:HAS]->(d),
  (d)-[:HAS]->(e),
  (e)-[:HAS]->(f),
  (f)-[:HAS]->(g),
  (g)-[:HAS]->(h),
  (h)-[:HAS]->(i),
  (ap:hotel_loc {loc_id:'pp1A'}),
  (bp:hotel_loc {loc_id:'pp1B'}),
  (cp:hotel_loc {loc_id:'pp1C'}),
  (dp:hotel_loc {loc_id:'pp1D'}),
  (ep:hotel_loc {loc_id:'pp1E'}),
  (fp:hotel_loc {loc_id:'pp1F'}),
  (gp:hotel_loc {loc_id:'pp1G'}),
  (hp:hotel_loc {loc_id:'pp1H'}),
  (ip:hotel_loc {loc_id:'pp1I'}),
  (a)-[:HAS_JUNC]->(ap),
  (b)-[:HAS_JUNC]->(bp),
  (c)-[:HAS_JUNC]->(cp),
  (d)-[:HAS_JUNC]->(dp),
  (e)-[:HAS_JUNC]->(ep),
  (f)-[:HAS_JUNC]->(fp),
  (g)-[:HAS_JUNC]->(gp),
  (h)-[:HAS_JUNC]->(hp),
  (i)-[:HAS_JUNC]->(ip);

2 scenerios 1. List of the all node starting from given a loc_id (example pp1F ), i need to get all the nodes(hotel) and their associated :HAS_JUNC nodes.

f,pp1F
g,pp1G
h,pp1H
i,pp1I
  1. List of all until the end. given a loc_id (example pp1C ), i need to all the nodes until pp1C
a,pp1A
b,pp1B
c,pp1C

Any help is highly appreciated.
@William_Lyon

1 REPLY 1

Hi,

First, find a hotel by location.
Then get the longest path.
Finally, hotel is displayed in order.

I don't think it's good code, but it works.

scenerios 1

MATCH (l:hotel_loc)<-[:HAS_JUNC]-(h:hotel)
  WHERE l.loc_id = 'pp1F'
WITH h
MATCH path = (h)-[:HAS*0..10]->(:hotel)
WITH nodes(path) AS hotels
  ORDER BY length(path) DESC
  LIMIT 1
UNWIND hotels AS hotel
MATCH (hotel)--(loc:hotel_loc)
RETURN hotel.hotel_id + ',' + loc.loc_id;

scenerios 2

MATCH (l:hotel_loc)<-[:HAS_JUNC]-(h:hotel)
  WHERE l.loc_id = 'pp1C'
WITH h
MATCH path = (:hotel)-[:HAS*0..10]->(h)
WITH nodes(path) AS hotels
  ORDER BY length(path) DESC
  LIMIT 1
UNWIND hotels AS hotel
MATCH (hotel)--(loc:hotel_loc)
RETURN hotel.hotel_id + ',' + loc.loc_id;