gpt4 book ai didi

java - 大数据集的 Spring data/Neo4j 路径长度

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:49 25 4
gpt4 key购买 nike

我一直在运行以下查询来查找给定人的特定“距离”内的亲戚:

@Query("start person=node({0}), relatives=node:__types__(className='Person') match p=person-[:PARTNER|CHILD*]-relatives where LENGTH(p) <= 2*{1} return distinct relatives")
Set<Person> getRelatives(Person person, int distance);

2*{1} 来自表示为两个节点(一个人和一个合作伙伴)的人之间的一个概念“跳跃”。

到目前为止,在测试人群中,这一切都很好。现在我将继续讨论实际数据,其大小从 1 到 1000 万不等,而且这将永远持续下去(也来自 Web 界面中的数据浏览器)。

假设成本是将所有内容加载到ancestors中,我将查询重写为数据浏览器中的测试:

start person=node(385716) match p=person-[:PARTNER|CHILD*1..10]-relatives where relatives.__type__! = 'Person' return distinct relatives

在同一个数据存储上,这工作得很好,只需不到一秒的时间。但是当我想把它放回 Java 时:

@Query("start person=node({0}) match p=person-[:PARTNER|CHILD*1..{1}]-relatives where relatives.__type__! = 'Person' return relatives")
Set<Person> getRelatives(Person person, int distance);

这行不通:

[...]
Nested exception is Properties on pattern elements are not allowed in MATCH.
"start person=node({0}) match p=person-[:PARTNER|CHILD*1..{1}]-relatives where relatives.__type__! = 'Neo4jPerson' return relatives"
^

是否有更好的方法来限制路径长度?我不想使用 where,因为这会涉及加载所有路径,可能会加载数百万个节点,而我只需要达到 10 的深度。这可能不会给我带来更好的结果。

任何想法将不胜感激!

<小时/>

迈克尔来救援!

我的解决方案:

public Set<Person> getRelatives(final Person person, final int distance) {

final String query = "start person=node(" + person.getId() + ") "
+ "match p=person-[:PARTNER|CHILD*1.." + 2 * distance + "]-relatives "
+ "where relatives.__type__! = '" + Person.class.getSimpleName() + "' "
+ "return distinct relatives";

return this.query(query);

// Where I would previously instead have called
// return personRepository.getRelatives(person, distance);
}

public Set<Person> query(final String q) {

final EndResult<Person> result = this.template.query(q, MapUtil.map()).to(Neo4jPerson.class);
final Set<Person> people = new HashSet<Person>();

for (final Person p : result) {
people.add(p);
}

return people;
}

运行速度非常快!

最佳答案

你就快到了:)

您的第一个查询是完整的图形扫描,它有效地将整个数据库加载到内存中,并通过此模式匹配多次拉取所有节点。

所以它不会很快,而且它会返回巨大的数据集,不知道这是否是你想要的。

第二个查询看起来不错,唯一的问题是您无法参数化可变长度关系的最小-最大值。由于查询优化/缓存的影响。

因此,现在您必须在存储库中使用 template.query() 或不同的查询方法来获取不同的最大值。

关于java - 大数据集的 Spring data/Neo4j 路径长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18087877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com