版本:
<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和D之间的路线,我需要以正确的顺序将结果作为A,B,C和D,然后我将在每个对象中获得总线。 / p>
这是我的密码:
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。
我能想到的一种方法是向OperatedByBus添加一个属性,例如int legId,然后在我的查询中按legId排序。不确定这是否是最佳方式。
我遗失了什么?
答案 0 :(得分:4)
按如下方式修改您的查询:
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)
请注意,当where子句匹配多个节点时,您的查询可能会返回多行。
然后使用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");