问题
I have users
and groups
they belong to.
I have an index n:Group(name)
I want to search for all users belong to a set of groups : ["gr1","gr2","gr3"]
which other groups they belong to.
so I have the following query: Query :
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name in ["gr1","gr2"]
return distinct gr2
The thing here, is that I think that cypher don't use the index because the query is to slow.
I have 2k nodes, 50k relationships. The following query takes between 200 to 700 ms (depending on cache):
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name = "gr1"
return distinct gr2
However, the following query, takes between 2 to 6 seconds!
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name in ["gr1"]
return distinct gr2
When I'm trying to do :
MATCH (gr:Group) <--(us:User)--(gr2:Group)
using index gr:Group(name)
WHERE gr.name in ["gr1"]
return distinct gr2
I get the following error:
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`
(with "=" instead of "in" I get no error)
I'm using neo4j enterprise 2.0.1 . The test results are from running on the neo4j browser .
回答1:
The index is referred either when you specify the attribs in the pattern as in (gr:Group {name: "gr1"})
or you call WHERE gr.name = "gr1"
. In your case where multiple attrib values are possible you can use OR
clause.
MATCH (gr:Group) <--(us:User)--(gr2:Group)
WHERE gr.name="gr1" OR gr.name="gr2"
return distinct gr2
来源:https://stackoverflow.com/questions/21617625/cypher-2-0-using-label-based-index-to-search-a-set-of-nodes