Spring数据| Neo4J |按正确顺序查询路径

Spring数据| Neo4J |按正确顺序查询路径,neo4j,cypher,spring-data-neo4j-4,neo4j-ogm,spring-data-neo4j-5,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> <!

版本:

<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>

org.neo4j
neo4j ogm核
2.1.1
org.neo4j
neo4j ogm http驱动程序
2.1.1
org.springframework.data
spring-data-neo4j
4.2.0.1发布
以下是我的实体:

@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;
}
@数据
@节点性
@EqualsAndHashCode(排除={“OperatedByBus”})
@ToString(of={“name”})
公务舱巴士站{
@格拉希德
私人长格拉皮德;
@索引(unique=true,primary=true)
私有字符串名称;
私有字符串pincode;
私人地标;
私有字符串[]latlong;
@关系(type=“操作方”)
private Set operatedbybus=new HashSet();
}
@资料
@RelationshipEntity(type=“运营方”)
@ToString(of=“displayName”)
公车{
@格拉希德
私人长id;
@StartNode
私家巴士站起点;
@端节点
私人巴士站目的地;
}
我试图得到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());
        }
String findBuses=“MATCH p=shortestPath((o:BusStop)-[bus*1..40]->(d:BusStop))\n”+
“其中o.name=~”(?i)。*ABC公共汽车站。*'\n”+
“和d.name=~”(?i)。*XYZ公共汽车站。*'\n”+
“返回p”;
Iterable busstops=session.query(BusStop.class、findBuses、Collections.emptyMap());
系统输出打印LN(总线停止);
用于(公共汽车站公共汽车站:公共汽车站){
System.out.println(bustop.getName());
System.out.println(“\t”+总线站.getOperatedByBus());
}
但结果的顺序并不正确。我看到的结果是D,C,A,B(或一些随机顺序),而不是A,B,C,D

我可以想到的一种方法是,在查询中向OperatedByBus添加一个属性,比如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)
请注意,当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");
Iterable result.queryResults();
.. 迭代结果
//获取单个路径作为集合的“busStops”,该集合应按顺序排列
.. (收集)项目。获取(“公共汽车站”);

按如下方式修改查询:

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");
Iterable result.queryResults();
.. 迭代结果
//获取单个路径作为集合的“busStops”,该集合应按顺序排列
.. (收集)项目。获取(“公共汽车站”);

您希望返回的具体内容是什么<代码>session.query(BusStop.class…将从结果中返回BusStop的所有实例,包括路径上的中间节点-这是您所期望的吗?好的,我期望BusStop实例按照路由的顺序。例如,BusStop1->BusStop2->BusStop3->BusStop4。我是否应该使用其他查询方法来导航总线按正确的顺序停止实例?您希望返回什么?
session.query(BusStop.class…
将从结果中返回BusStop的所有实例,包括路径上的中间节点-这是您所期望的吗?好的,我期望BusStop实例按照路由的顺序。例如,BusStop1->BusStop2->BusStop3->BusStop4。我是否应该使用其他查询方法来导航总线按正确的顺序停止实例?