0

Следующий запрос должен вернуть учетную запись, отсортированную по числу «последователей» в порядке desc, с лимитом.Возможная ошибка данных Spring с SDN Neo4J Cypher Query

В качестве параметров (maxCount и limit) требуется 2 переменных. MaxCount относится к числу последователей. поэтому, если maxCount равен 100, то возвратите только те учетные записи, у которых есть последователи менее 100.

Если я запустил этот запрос в приложении Spring Data Neo4j. Кажется, что игнорирует ограничение maxCount, например. Если я установил maxCount равным 2 и у вас есть учетная запись 1 с 3 последователями и учетной записью 2 с 1 последователем. Кажется, неправильно возвращать обе учетные записи, когда он должен возвращать только счет 2, который имеет только 1 последователя.

Запрос

@Query("MATCH (a:Account)<-[:follows]-(b:Account) WITH a, COLLECT(b) 
AS bs WHERE SIZE(bs) < {0} RETURN a ORDER BY SIZE(bs) DESC LIMIT {1}") 
List<Account> findCappedSortedAccountByFollowers(int maxCount, int resultSize); 

Это работает, если ввести его непосредственно в Neo4j консоли

Возможно, это ошибка Spring Data?

SDN Версия: 4.1.3.RELEASE

OGM Embedded Driver Версия: 2.0.5

Small App продемонстрировать проблемы доступной here

+0

Можете ли вы попробовать его с SDN 4.2.0.BUILD-SNAPSHOT и OGM 2.1.0-SNAPSHOT. Возможно, вам понадобится следующее: http://graphaware.com/neo4j/2016/09/30/upgrading-to-sdn-42.html, чтобы настроить. – digx1

+0

Вы запустили образец приложения? –

+0

Да. Он работает с этими версиями. – digx1

ответ

2

Вот ваш код обновлен для работы с новыми версиями , Оба теста проходят. Я переписал ваш тест и модель, чтобы больше соответствовать нашей документации и примерам. Помните, что OGM может сохраняться по достижимости. Надеюсь, это поможет вам преодолеть проблему. Я объяснил, как писать тесты типа Transactional с помощью Spring Data здесь: https://stackoverflow.com/a/39887536/2271422.

build.gradle:

dependencies { 
    compile "ch.qos.logback:logback-classic:1.1.7" 
    compile "org.springframework.data:spring-data-neo4j:4.2.0.BUILD-SNAPSHOT" 
    compile "org.springframework:spring-test:4.3.3.RELEASE" 
    compile "org.neo4j:neo4j-ogm-embedded-driver:2.1.0-SNAPSHOT" 
    testCompile "junit:junit:4.12" 
} 

src/main/javacom.neo4j.relation.config:

@Configuration 
@EnableNeo4jRepositories("com.neo4j.relation.repository") 
@EnableTransactionManagement 
public class EmbeddedNeo4JConfig { 

    @Bean 
    public SessionFactory sessionFactory() { 
     return new SessionFactory("com.neo4j.relation.model"); 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager() { 
     return new Neo4jTransactionManager(sessionFactory()); 
    } 
} 

src/main/javacom.neo4j.relation.model:

@NodeEntity 
public class Account { 

    @GraphId 
    private Long id; 

    private String name; 

    @Relationship(type = "FOLLOWS", 
      direction = Relationship.OUTGOING) 
    private Set<Account> following = new HashSet<Account>(); 

    @Relationship(type = "FOLLOWS", 
      direction = Relationship.INCOMING) 
    private Set<Account> followers = new HashSet<Account>(); 

    private Account() { 
    } 

    private Account(String name) { 
     this.name = name; 
    } 

    public static Account newAccountInstance(String name) { 
     return new Account(name); 
    } 

    //Accessors 
    public Long getId() { 
     return id; 
    } 

    public Set<Account> getFollowers() { 
     return followers; 
    } 

    public String getName() { 
     return name; 
    } 

    public String toString() { 
     StringBuilder sb = new StringBuilder(); 
     sb.append("Id: ").append(getId()).append(", "); 
     sb.append("Name: ").append(getName()).append(", "); 
     return sb.toString(); 
    } 
} 

src/main/javacom.neo4j.relation.repository:

public interface AccountRepository extends GraphRepository<Account> { 
    @Query("MATCH (a:Account)<-[:FOLLOWS]-(b:Account) WITH a, COLLECT(b) AS bs ORDER BY SIZE(bs) DESC RETURN a LIMIT {0}") 
    List<Account> findSortedAccountByFollowers(int maxSize); 

    @Query("MATCH (a:Account)<-[:FOLLOWS]-(b:Account) WITH a, COLLECT(b) AS bs WHERE SIZE(bs) <= {0} RETURN a ORDER BY SIZE(bs) DESC LIMIT {1}") 
    List<Account> findCappedSortedAccountByFollowers(int maxCount, int resultSize); 
} 

src/test/resourceslogback.xml:

<configuration> 

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> 
     <encoder> 
      <pattern>%d %5p %40.40c:%4L - %m%n</pattern> 
     </encoder> 
    </appender> 

    <logger name="org.neo4j.ogm" level="debug" /> 
    <logger name="org.springframework" level="warn" /> 
    <logger name="org.springframework.data.neo4j" level="debug" /> 
    <root level="debug"> 
     <appender-ref ref="console" /> 
    </root> 

</configuration> 

src/test/resourcescom.neo4j.test:

@ContextConfiguration(classes = {EmbeddedNeo4JConfig.class}) 
@RunWith(SpringJUnit4ClassRunner.class) 
@Transactional 
public class AccountTester { 

    @Autowired 
    AccountRepository accountRepository; 

    @Test 
    public void testFindSortedAccountByFollowers() { 

     Account account = Account.newAccountInstance("James Martin"); 
     Account account2 = Account.newAccountInstance("Steve Owen"); 
     Account account3 = Account.newAccountInstance("Bill Gates"); 
     Account account4 = Account.newAccountInstance("Steve Jobs"); 
     account.getFollowers().add(account2); 

     accountRepository.save(account); 
     accountRepository.save(account3); 
     accountRepository.save(account4); 

     assertNotNull(account.getId()); 

     final Iterable<Account> all = accountRepository.findAll(); 

     assertEquals(4, IteratorUtils.size(all.iterator())); 

     List<Account> accounts = accountRepository.findSortedAccountByFollowers(10); 
     int size = accounts.size(); 
     assertEquals(1, size); 
     assertEquals(account, accounts.get(0)); 
    } 

    @Test 
    public void testCappedSortAccountByFollowerCount() { 
     Account account = Account.newAccountInstance("Steve Martin"); 
     Account account2 = Account.newAccountInstance("Steve Owen"); 
     Account account3 = Account.newAccountInstance("Bill Gates"); 
     Account account4 = Account.newAccountInstance("Steve Jobs"); 

     account.getFollowers().add(account2); 
     account.getFollowers().add(account3); 
     account.getFollowers().add(account4); 

     account4.getFollowers().add(account3); 

     accountRepository.save(account); 

     List<Account> accounts = accountRepository.findCappedSortedAccountByFollowers(2, 10); 
     int size = accounts.size(); 
     assertEquals(1, size); 
     assertEquals(account4, accounts.get(0)); 
    } 
} 
+0

. Оно добавляется через доступность. Тест проходит. – digx1

 Смежные вопросы

  • Нет связанных вопросов^_^