春季数据| Neo4J |以正确的顺序查询路径 [英] Spring Data | Neo4J | Querying for the path in the correct order

查看:286
本文介绍了春季数据| Neo4J |以正确的顺序查询路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

版本:

<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,然后将每个对象中的总线获取.

I'm trying to get the routes between A and D for which I need the result as A,B,C and D in the correct order and then I'll get the buses in each object.

这是我的密码:

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.

But the results are not in the right order. I see results as D,C,A,B (or some random order) as opposed to A,B,C,D.

我能想到的一种方法是在OperatedByBus中添加一个属性,例如int legId,然后在我的查询中按legId排序.不知道这是否是最好的方法.

One way I can think of is add an attribute to the OperatedByBus say int legId and then order by legId in my query. Not sure if this is the best way.

我想念什么吗?

推荐答案

按如下所示修改您的查询:

Modify your query as follows:

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子句与多个节点匹配时,您的查询可能会返回多行.

note that your query may return multiple rows, when the where clause matches multiple nodes.

然后使用session.query(findBuses, Collections.emptyMap());代替.结果类型为org.neo4j.ogm.model.Result,使用以下命令获取单个结果:

Then use session.query(findBuses, Collections.emptyMap()); instead. The result type is org.neo4j.ogm.model.Result, use following to get individual results:

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");

这篇关于春季数据| Neo4J |以正确的顺序查询路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆