Over a million developers have joined DZone.

Neo4j 2.0.0 Cypher: Index Hints and Neo.ClientError.Schema.NoSuchIndex

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

One of the features added into the more recent versions of Neo4j’s Cypher query language is the ability to tell Cypher which index you’d like to use in your queries.

We’ll use the football dataset, so let’s start by creating an index on the ‘name’ property of nodes labelled ‘Player’:

CREATE INDEX ON :Player(name)

Let’s say we want to write a query to find ‘Wayne Rooney’ while explicitly using this index. We might start with the following query:

MATCH p
USING INDEX p:Player(name)
WHERE p.name = "Wayne Rooney"
RETURN p

If we run that we’ll see this error:

Cannot use index hint in this context. The label and property comparison must be specified on a non-optional node
Label: `Player`
Property name: `name`
Neo.ClientError.Schema.NoSuchIndex

We need to specify the label on the node in the ‘MATCH’ part of the query for our hint to work e.g.

MATCH (p:Player)
USING INDEX p:Player(name)
WHERE p.name = "Wayne Rooney"
RETURN p

Now we might decide that we want to find ‘Wayne Rooney’ or ‘Robin Van Persie’ so we change our query slightly:

MATCH (p:Player)
USING INDEX p:Player(name)
WHERE p.name = "Wayne Rooney" OR p.name = "Robin Van Persie"
RETURN p

But this one fails too!

Cannot use index hint in this context. The label and property comparison must be specified on a non-optional node
Label: `Player`
Property name: `name`
Neo.ClientError.Schema.NoSuchIndex

The problem here is that when you use ‘OR’ it’s currently not using an index but rather a label scan so the hint to use the index doesn’t make sense to Cypher.

The final way I’ve seen people get confused using index hints is when matching node properties inline e.g.

MATCH (p:Player {name: "Wayne Rooney"})
USING INDEX p:Player(name)
RETURN p

If we run that we’ll be back in the land of exceptions:

Cannot use index hint in this context. The label and property comparison must be specified on a non-optional node
Label: `Player`
Property name: `name`
Neo.ClientError.Schema.NoSuchIndex

We can work around that by pulling out the inline matching of the ‘name’ property”:

MATCH (p:Player)
USING INDEX p:Player(name)
WHERE p.name = "Wayne Rooney"
RETURN p


Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}