내가 특정 사람의 특정 "거리"내 친척을 찾기 위해 다음과 같은 쿼리를 실행 한 설정 한 사람과 한 파트너쉽이라는 두 노드로 표현되는 사람들 사이의 개념적 "홉"봄 데이터/Neo4j 경로 길이는
테스트 집단에서는 지금까지 괜찮 았습니다. 이제 실제 데이터로 이동합니다. 실제 데이터는 1 ~ 1,000 만 개 크기로 구성되며, 이는 웹 인터페이스의 데이터 브라우저에서도 영원히 계속됩니다. 비용이 ancestors
에 모든 것을로드에서였다 가정
, 나는 데이터 브라우저에서 테스트로 쿼리를 재 작성 :
start person=node(385716) match p=person-[:PARTNER|CHILD*1..10]-relatives where relatives.__type__! = 'Person' return distinct relatives
그리고 그 같은 데이터 저장소에 두 번째의 분수, 잘 작동합니다. 나는 자바에 다시 넣을 때 :
@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;
}
매우 빠르게 실행됩니다!
id를 직접 사용하지 말고 클래스 이름과 동일한 매개 변수를 사용하십시오. 그렇지 않으면 모든 쿼리를 다시 구문 분석해야하므로 런타임에 쿼리 캐싱이 느슨해집니다 !! –