版本:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId>
<version>2.1.1</version>
</dependency>
<dependency> <!-- If you're using the HTTP driver -->
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-neo4j -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
这是我的实体:
@Data
@NodeEntity
@EqualsAndHashCode(exclude = {"operatedByBuses"})
@ToString(of = {"name"})
public class BusStop {
@GraphId
private Long graphId;
@Index(unique = true, primary = true)
private String name;
private String pincode;
private String landmark;
private String[] latlong;
@Relationship(type = "OPERATED_BY")
private Set<OperatedByBus> operatedByBuses = new HashSet<>();
}
@Data
@RelationshipEntity(type = "OPERATED_BY")
@ToString(of = "displayName")
public class OperatedByBus {
@GraphId
private Long id;
@StartNode
private BusStop origin;
@EndNode
private BusStop destination;
}
我正在尝试以正确的顺序获得A,B,C和D的A和D之间的路线,然后我将公共汽车在每个对象中获取。
这是我的Cypher:
String findBuses = "MATCH p=shortestPath((o:BusStop)-[buses*1..40]->(d:BusStop))n" +
"WHERE o.name =~ '(?i).*ABC Bus Stand.*'n" +
" AND d.name =~ '(?i).*XYZ Bus Stand.*'n" +
"RETURN p";
Iterable<BusStop> busstops = session.query(BusStop.class, findBuses, Collections.emptyMap());
System.out.println(busstops);
for (BusStop busStop : busstops) {
System.out.println(busStop.getName());
System.out.println("t " + busStop.getOperatedByBuses());
}
但结果不正确。我认为结果为d,c,a,b(或某些随机顺序),而不是a,b,c,d。
我能想到的一种方法是向操作的bbybus say int legid添加一个属性,然后在我的查询中按LEGID订购。不确定这是否是最好的方法。
我缺少什么?
修改查询如下:
MATCH p=shortestPath((o:BusStop)-[buses*1..40]->(d:BusStop))
WHERE
o.name =~ '(?i).*ABC Bus Stand.*' AND
d.name =~ '(?i).*XYZ Bus Stand.*'
RETURN nodes(p) as busStops,relationships(p)
请注意,当您的查询可能返回多个行,当时子句匹配多个节点。
然后使用session.query(findBuses, Collections.emptyMap());
。结果类型是org.neo4j.ogm.model.Result
,使用以下来获得单个结果:
Iterable<Map<String, Object>> result.queryResults();
.. iterate over results
// to get a `busStops` for single path as collection, which should be in order
.. (Collection<BusStop>) item.get("busStops");