我有用户
和他们所属的组
。
我有一个索引 n:Group(name)
我想搜索属于一组组的所有用户:["gr1","gr2","gr3"]
他们属于哪些其他组。
所以我有以下查询: 查询:
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name in ["gr1","gr2"]
return distinct gr2
这里的事情是,我认为 cypher 不使用索引,因为查询速度很慢。
我有 2k 个节点,50k 个关系。 以下查询需要 200 到 700 毫秒(取决于缓存):
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name = "gr1"
return distinct gr2
但是,以下查询需要 2 到 6 秒!
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name in ["gr1"]
return distinct gr2
当我尝试这样做时:
MATCH (gr:Group) <--(us:User)--(gr2:Group)
using index gr:Group(name)
WHERE gr.name in ["gr1"]
return distinct gr2
我收到以下错误:
Cannot use index hint in this context. The label and property comparison must be specified on a non-optional node
Label: `Group`
Property name: `name`
(用“=”而不是“in”我没有得到错误)
我正在使用 neo4j enterprise 2.0.1 。测试结果是在neo4j浏览器上运行的。
最佳答案
当您在模式中指定属性(如 (gr:Group {name: "gr1"})
或调用 WHERE gr.name = "gr1 “
。在可能有多个属性值的情况下,您可以使用 OR
子句。
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name="gr1" OR gr.name="gr2"
return distinct gr2
关于neo4j - 密码 2.0 : using label based index to search a set of nodes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617625/