Spring,Neo4j,用正则表达式搜索

时间:2014-03-20 20:29:23

标签: spring spring-mvc neo4j cypher

我试图在我的Spring网络应用程序中找到neo4j的图形数据库中的结果。这是我的工作:

@Query("START user=node:__types__(className='User') WHERE user.userName=~{0} OR user.firstName=~{0} OR user.lastName=~{0} RETURN user ORDER BY user.firstName, user.lastName")
Iterable<User> searchUsers(final String query);

我这样称呼它:

Iterable<User> users = usersService.searchUsers(String.format("(?i)%s.*", "All"));

查询正在执行,但没有返回任何结果。它实际上是一个测试,在用户搜索之前,我将几个结果保存到数据库中:

    User userA = new User("Tinker", "password", "Percy", "Alleline", 25, "1234567890", "my_email@email.com", User.Gender.MALE, new Date());
    User userD = new User("Taylor", "password", "Bill", "Haydon", 24, "1234567890", "my_email@email.com", User.Gender.FEMALE, new Date());
    User userC = new User("Soldier", "password", "Roy", "Bland", 25, "1234567890", "my_email@email.com", User.Gender.MALE, new Date());
    User userB = new User("Spy", "password", "Toby", "Esterhase", 24, "1234567890", "my_email@email.com", User.Gender.FEMALE, new Date());

    userA.persist();
    userB.persist();
    userC.persist();
    userD.persist();

更重要的是,如果我直接在数据库上执行查询,它会正确地找到结果。

START user=node:__types__(className = 'User') WHERE user.lastName=~ '(?i)to.*' OR user.firstName =~ '(?i)to.*' RETURN user ORDER BY user.firstName, user.lastName;

任何?

1 个答案:

答案 0 :(得分:0)

好的,其中一个解决方案是确保节点 HAS(node.property)作为搜索条件的属性。

下面的工作示例:

usersService.searchUsers(String.format("(?i).*%s.*", "All")); 

@Query("START user=node(*) WHERE HAS(user.firstName) AND (user.firstName=~{0} OR user.lastName=~{0}) RETURN user ORDER BY user.firstName, user.lastName")
Iterable<User> searchUsers(final String query);

如果你想出新的更好的,请提供。

谢谢。