首页 > 解决方案 > Spring boot Neo4j - 查询深度无法正常工作

问题描述

TL;DR:@Depth(value = -1) 抛出空指针,其他大于 1 的值被忽略

在我的带有 Neo4j 的 Spring Boot 项目中,我有 3 个具有关系的简单实体:

@NodeEntity
data class Metric(
        @Id @GeneratedValue val id: Long = -1,
        val name: String = "",
        val description: String = "",
        @Relationship(type = "CALCULATES")
        val calculates: MutableSet<Calculable> = mutableSetOf()
) {
    fun calculates(calculable: Calculus) = calculates.add(calculable)
    fun calculate() = calculates.map { c -> c.calculate() }.sum()
}

interface Calculable {
    fun calculate(): Double
}

@NodeEntity
data class Calculus(
        @Id @GeneratedValue val id: Long = -1,
        val name: String = "",
        @Relationship(type = "LEFT")
        var left: Calculable? = null,
        @Relationship(type = "RIGHT")
        var right: Calculable? = null,
        var operator: Operator? = null
) : Calculable {
    override fun calculate(): Double =
            operator!!.apply(left!!.calculate(), right!!.calculate())
}

@NodeEntity
data class Value(
        @Id @GeneratedValue val id: Long = -1,
        val name: String = "",
        var value: Double = 0.0
) : Calculable {
    override fun calculate(): Double = value
}

enum class Operator : BinaryOperator<Double>, DoubleBinaryOperator {//not relevant}

我创建了一个像这样的简单图表:

Neo4j 图

使用以下存储库:

@Repository
interface MetricRepository : Neo4jRepository<Metric, Long>{
    @Depth(value = 2)
    fun findByName(name: String): Metric?
}

@Repository
interface CalculusRepository : Neo4jRepository<Calculus, Long>{
    fun findByName(name: String): Calculus?
}

@Repository
interface ValueRepository : Neo4jRepository<Value, Long>{
    fun findByName(name: String): Value?
}

以及以下代码:

// calculus
val five = valueRepository.save(Value(
        name = "5",
        value = 5.0
))

val two = valueRepository.save(Value(
        name = "2",
        value = 2.0
))

val fiveTimesTwo = calculusRepository.save(Calculus(
        name = "5 * 2",
        operator = Operator.TIMES,
        left = five,
        right = two
))

println("---")
println(fiveTimesTwo)
val fromRepository = calculusRepository.findByName("5 * 2")!!
println(fromRepository) // sometimes has different id than fiveTimesTwo
println("5 * 2 = ${fromRepository.calculate()}")
println("--- \n")


// metric
val metric = metricRepository.save(Metric(
        name = "Metric 1",
        description = "Measures a calculus",
        calculates = mutableSetOf(fromRepository)
))
metricRepository.save(metric)

println("---")
println(metric)
val metricFromRepository = metricRepository.findByName("Metric 1")!!
println(metricFromRepository) // calculates node is partially empty
println("--- \n")

要检索如上图所示的相同图表(取自实际的 neo4j 仪表板),我这样做metricRepository.findByName("Metric 1")@Depth(value = 2),然后打印保存的指标和检索到的指标:

Metric(id=9, name=Metric 1, description=Measures a calculus, calculates=[Calculus(id=2, name=5 * 2, left=Value(id=18, name=5, value=5.0), right=Value(id=1, name=2, value=2.0), operator=TIMES)])

Metric(id=9, name=Metric 1, description=Measures a calculus, calculates=[Calculus(id=2, name=5 * 2, left=null, right=null, operator=TIMES)])

无论深度值如何,我都无法获得包含所有子节点的 Metric 节点,它检索一级深度最大值并在叶节点上返回 null。

我在文档中读到 depth=-1 检索完全解析的节点,但这样做会导致该findByName()方法以空指针失败:kotlin.KotlinNullPointerException: null

以下是我咨询过的资源列表和一个包含完整代码的工作 GitHub 存储库:

最后注意事项:

版本:

感谢您的帮助,欢迎所有反馈!

标签: spring-bootkotlinneo4jspring-data-neo4jneo4j-ogm

解决方案


问题不在于查询深度,而在于模型。实体与Metric有关系Calculable,但Calculable它本身没有定义关系。Spring Data 无法扫描Calculable接口的所有可能实现以了解它们之间的关系。如果您将Metrics.calculatestype 更改为MutableSet<Calculus>,它将按预期工作。

要查看发送到服务器的 Cypher 请求,您可以添加logging.level.org.neo4j.ogm.drivers.bolt=DEBUGapplication.properties

变更前的要求:

MATCH (n:`Metric`) WHERE n.`name` = $`name_0` WITH n RETURN n,[ [ (n)->[r_c1:`CALCULATES`]->(x1) | [ r_c1, x1 ] ] ], ID(n) with params {name_0=Metric 1}

变更后的要求:

MATCH (n:`Metric`) WHERE n.`name` = $`name_0` WITH n RETURN n,[ [ (n)->[r_c1:`CALCULATES`]->(c1:`Calculus`) | [ r_c1, c1, [ [ (c1)-[r_l2:`LEFT`]-(v2:`Value`) | [ r_l2, v2 ] ], [ (c1)-[r_r2:`RIGHT`]-(v2:`Value`) | [ r_r2, v2 ] ] ] ] ] ], ID(n) with params {name_0=Metric 1}

推荐阅读