An index can be searched in two ways, get and query.
The get
method will return exact matches to the given key-value pair, whereas query
exposes querying capabilities directly from the backend used by the index.
For example the Lucene query syntax can be used directly with the default indexing backend.
This is how to search for a single exact match:
IndexHits<Node> hits = actors.get( "name", "Keanu Reeves" ); Node reeves = hits.getSingle();
IndexHits is an Iterable
with some additional useful methods. For example getSingle() returns the first and only item from the result iterator, or null
if there isn’t any hit.
Here’s how to get a single relationship by exact matching and retrieve its start and end nodes:
Relationship persephone = roles.get( "name", "Persephone" ).getSingle(); Node actor = persephone.getStartNode(); Node movie = persephone.getEndNode();
Finally, we can iterate over all exact matches from a relationship index:
for ( Relationship role : roles.get( "name", "Neo" ) ) { // this will give us Reeves twice Node reeves = role.getStartNode(); }
Important | |
---|---|
In you don’t iterate through all the hits, IndexHits.close() must be called explicitly. |
There are two query methods, one which uses a key-value signature where the value represents a query for values with the given key only. The other method is more generic and supports querying for more than one key-value pair in the same query.
Here’s an example using the key-query option:
for ( Node actor : actors.query( "name", "*e*" ) ) { // This will return Reeves and Bellucci }
In the following example the query uses multiple keys:
for ( Node movie : movies.query( "title:*Matrix* AND year:1999" ) ) { // This will return "The Matrix" from 1999 only. }
Note | |
---|---|
Beginning a wildcard search with "*" or "?" is discouraged by Lucene, but will nevertheless work. |
Caution | |
---|---|
You can’t have any whitespace in the search term with this syntax. See Section 14.11.3, “Querying with Lucene Query objects” for how to do that. |
Copyright © 2012 Neo Technology