Neo4j limits the value of a property to only x changes

Is there any way Neo4J say for a query like this

MATCH (p1:BusStop {idStop: "1293"}), (p2:BusStop {idStop: "1052"}),
  path = shortestpath((p1)-[:PATH*]->(p2))
RETURN path

I want it to keep the same property value for the whole path.

Sort of:

MATCH (p1:BusStop {idStop: "1293"}), (p2:BusStop {idStop: "1052"}),
  path = shortestpath((p1)-[:PATH*{busLineID: AlwaysTheSame}]->(p2))
RETURN path

But I do not want to write busLineID myself, because I cannot know this in advance.

If possible, one can also say neo4j: change the value of this property no more than x times during the path?

thank

+4
source share
1 answer

Yes, you can add a predicate to ensure that all relationships in the path have the same property value.

This helps to find out all the possible busLineID values ​​by first discovering the common values ​​in the relationships extending from both nodes.

MATCH (p1:BusStop {idStop: "1293"})-[r:PATH]-()
WITH p1, COLLECT(DISTINCT r.busLineID) as firstSet
MATCH (p2:BusStop {idStop: "1052"})-[r:PATH]-()
WITH p1, p2, firstSet, COLLECT(DISTINCT r.busLineID) as secondSet
WITH p1, p2, FILTER(id IN firstSet WHERE id IN secondSet) as commonIDs
UNWIND commonIDs as busLineID
MATCH path = shortestpath((p1)-[:PATH*]-(p2))
WHERE ALL(rel in RELATIONSHIPS(path) | rel.busLineID = busLineID)
RETURN path

, WHERE ALL shortestPath() , .

, , , , .

- . , , . ( , ):

MATCH (p1:BusStop {idStop: "1293"}), (p2:BusStop {idStop: "1052"})
MATCH p = allShortestPaths((p1)-[:PATH*]-(p2))
RETURN p, SIZE(FILTER(x in RANGE(0, SIZE(RELATIONSHIPS(p))) 
       WHERE (RELATIONSHIPS(p)[x]).busLineID <> (RELATIONSHIPS(p)[x-1]).busLineID)) as busLineChanges
ORDER BY busLineChanges ASC

, , , shortestPath() , , , , busLineID MATCH, WHERE, WITH RETURN, shortestPath() (shortestPath() MATCH WHERE , , , shortestPath()).

, .

, , , : PATH ( , , , ):

MATCH (p1:BusStop {idStop: "1293"}), (p2:BusStop {idStop: "1052"})
MATCH path = (p1)-[rels:PATH*1..10]-(p2)
WITH path, SIZE(FILTER(x in RANGE(0, SIZE(rels)) 
           WHERE (rels[x]).busLineID <> (rels[x-1]).busLineID)) as busLineChanges
WHERE busLineChanges <= 3
RETURN path, busLineChanges
ORDER BY SIZE(path) ASC
LIMIT 1

: PATH, , .

+1

Source: https://habr.com/ru/post/1660292/


All Articles