[Cypher] Search queries based on indexes

30 views
Skip to first unread message

Samwillie

unread,
May 9, 2012, 6:13:34 AM5/9/12
to Neo4j
Hi experts,

Am using Neo4j 1.7.M01 and the cypher is the same version, included
with neo4j.

I am having trouble with the following queries and need your
assistance. I have indexed a group of nodes as "abcNodes" by the node
property "name". Now I would like to do a search (with or without
wildcard) *, with the following query. I pass in the search string as
a parameter.

1. "start n = node:abcNodes('name:{searchString}*') return n

But this throws an exception: Cannot parse 'name:{searchString}':
Encountered " "}" "} "" at line 1, column 31. How can a parameter be
passed in to search in cypher?

2. To understand cypher better, I have adapted the following example
from the Neo4j Documentation.

Movies <- "ACTS_IN" - Actors. Here the relationship "ACTS_IN" has a
property "role" which has the character name. I have indexed the
movies by the name property, actors by the name property and the
relationships by the "role" property.

Now if we want to find out "Give me the actor's name, who acted as
"Neo" in the movie "Matrix" - should we first search the movies index
(based on 1) to find Matrix, then search on relationship index to find
"Neo" and then get the start node?

Many thanks,

Peter Neubauer

unread,
May 9, 2012, 6:20:42 AM5/9/12
to ne...@googlegroups.com
Hi there,
http://docs.neo4j.org/chunked/snapshot/cypher-parameters.html covers
this, it should probably be

searchString = "name:abs*"

start n = node:abcNodes({searchString}) return n

Since the whole query is one string passed in as a parameter to the
underlying (Lucene) index per default.

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

Samwillie

unread,
May 9, 2012, 11:10:09 AM5/9/12
to Neo4j
Hi Peter,

Thanks much! This works!

As for my second query, I have the following, which works in Neo4j
console window but not in my java application - I am trying to get
this work and would appreciate your help greatly...

roles is the relationship index name, and role is the field name.
nodeId is the movie id, say of 'Matrix'

Here I expect the result - the corresponding actor node. say, "Keanu
Reaves".

String searchString = "role:Neo*";
executionEngine.query("start r=relationship:roles({searchString})
match path = (x-[r]-node) where node.id = ({nodeId}) return x",
params);

The following however works in my console:

start r=relationship:roles('role:Neo*') match path = (x-[r]-node)
where node.id = 8791 return x

Many thanks for your time Peter,

Greets,

Michael Hunger

unread,
May 9, 2012, 11:21:28 AM5/9/12
to ne...@googlegroups.com
If you're working with the cineasts dataset node.id is a string,

do you pass in "8791" as a string parameter?

Michael

Samwillie

unread,
May 10, 2012, 3:36:39 AM5/10/12
to Neo4j
Hi Michael,

No, I am not working with the cineasts dataset. I wanted to refer to
the cineasts example, for this query so I can explain it better here.

I tried passing the node Id parameter as string, but it does not give
me any results, whereas I see the results in the console. (please see
query below)

// parameter in map:
nodeId = node.getId();
String searchString = "role:Neo*";
// query
executionEngine.query("start r=relationship:roles({searchString})
match path = (x-[r]-node) where node.id = '{nodeId}' return x",
params);

I don't know what needs to be changed in the query to make it work in
my java application, not only the console.

Many thanks for your time...

Greets



On May 9, 5:21 pm, Michael Hunger <michael.hun...@neotechnology.com>
wrote:

Michael Hunger

unread,
May 10, 2012, 3:37:37 AM5/10/12
to ne...@googlegroups.com
Oh,

you need to use ID(node) to get the id.

And the node-id is a long value.

Michael

Samwillie

unread,
May 10, 2012, 4:04:34 AM5/10/12
to Neo4j
Hi Michael,

Did you mean the following?

String searchString = "role:Neo*";
Map<String, Object> params = new HashMap<String, Object>();
params.put("node", movieNode);
params.put("searchString", searchString);
QueryResult<Map<String, Object>> result =
executionEngine.query("start r=relationship:roles({searchString})
match path = (x-[r]-node) where node.id = ID({node}) return x",
params);

But the execution just hangs without going to the next step. Did I
understand you correctly?

Thanks,,


On May 10, 9:37 am, Michael Hunger <michael.hun...@neotechnology.com>
wrote:

Michael Hunger

unread,
May 10, 2012, 4:15:44 AM5/10/12
to ne...@googlegroups.com
> String searchString = "role:Neo*";
> Map<String, Object> params = new HashMap<String, Object>();
> params.put("nodeId", movieNode.getId());
> params.put("searchString", searchString);
> QueryResult<Map<String, Object>> result =
> executionEngine.query("start r=relationship:roles({searchString})
> match path = (x-[r]-node) where ID(node) = {nodeId} return x",
> params);

you should probably add a direction to your match to make it faster.

but rather use:

> String searchString = "role:Neo*";
> Map<String, Object> params = new HashMap<String, Object>();
> params.put("nodeId", movieNode.getId());
> params.put("searchString", searchString);
> QueryResult<Map<String, Object>> result =
> executionEngine.query("start node=node({nodeId}), r=relationship:roles({searchString})
> match x-[r]-node return x",
> params);

Michael

Samwillie

unread,
May 10, 2012, 4:16:16 AM5/10/12
to Neo4j
Michael! Please ignore my previous message, thanks for your help!

we just need to pass in this clause:

...... where node.id = {nodeId} return x

where nodeId is the parameter, say, node.getId();

Many thanks again,

Greets

Michael Hunger

unread,
May 10, 2012, 4:24:24 AM5/10/12
to ne...@googlegroups.com
yes, but node.id is not the same as ID(node) which is the neo-id
node.id is a property called "id"

Michael

Samwillie

unread,
May 10, 2012, 4:28:23 AM5/10/12
to Neo4j
Thanks Michael! This is much faster than the earlier query!
Reply all
Reply to author
Forward
0 new messages