use*_*332 1 neo4j cypher spring-data-neo4j-4 neo4j-ogm spring-data-neo4j-5
版本:
<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>
Run Code Online (Sandbox Code Playgroud)
这是我的实体:
@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;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取 A 和 D 之间的路线,我需要按照正确的顺序将结果作为 A、B、C 和 D,然后我将获取每个对象中的公共汽车。
这是我的密码:
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());
}
Run Code Online (Sandbox Code Playgroud)
但结果的顺序不正确。我将结果视为 D、C、A、B(或某种随机顺序),而不是 A、B、C、D。
我能想到的一种方法是向 OperatedByBus 添加一个属性,比如 intlegId,然后在查询中按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)
Run Code Online (Sandbox Code Playgroud)
请注意,当 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");
Run Code Online (Sandbox Code Playgroud)