}
// END SNIPPET: queryWithScore
assertEquals( 2, hits.size() );
// START SNIPPET: queryWithRelevance
hits = movies.query( "title", new QueryContext( "The*" ).sortByScore() );
// END SNIPPET: queryWithRelevance
float previous = Float.MAX_VALUE;
// START SNIPPET: queryWithRelevance
for ( Node movie : hits )
{
// hits sorted by relevance (score)
// END SNIPPET: queryWithRelevance
assertTrue( hits.currentScore() <= previous );
previous = hits.currentScore();
// START SNIPPET: queryWithRelevance
}
// END SNIPPET: queryWithRelevance
assertEquals( 2, hits.size() );
// START SNIPPET: termQuery
// a TermQuery will give exact matches
Node actor = actors.query( new TermQuery( new Term( "name", "Keanu Reeves" ) ) ).getSingle();
// END SNIPPET: termQuery
assertEquals( "Keanu Reeves", actor.getProperty( "name" ) );
Node theMatrix = movies.get( "title", "The Matrix" ).getSingle();
Node theMatrixReloaded = movies.get( "title", "The Matrix Reloaded" ).getSingle();
// START SNIPPET: wildcardTermQuery
hits = movies.query( new WildcardQuery( new Term( "title", "The Matrix*" ) ) );
for ( Node movie : hits )
{
System.out.println( movie.getProperty( "title" ) );
// END SNIPPET: wildcardTermQuery
assertTrue( ( (String) movie.getProperty( "title" ) ).startsWith( "The Matrix" ) );
// START SNIPPET: wildcardTermQuery
}
// END SNIPPET: wildcardTermQuery
assertEquals( 2, hits.size() );
// START SNIPPET: numericRange
movies.add( theMatrix, "year-numeric", new ValueContext( 1999L ).indexNumeric() );
movies.add( theMatrixReloaded, "year-numeric", new ValueContext( 2003L ).indexNumeric() );
// Query for range
long startYear = 1997;
long endYear = 2001;
hits = movies.query( NumericRangeQuery.newLongRange( "year-numeric", startYear, endYear, true, true ) );
// END SNIPPET: numericRange
assertEquals( theMatrix, hits.getSingle() );
// START SNIPPET: compoundQueries
hits = movies.query( "title:*Matrix* AND year:1999" );
// END SNIPPET: compoundQueries
assertEquals( theMatrix, hits.getSingle() );
// START SNIPPET: defaultOperator
QueryContext query = new QueryContext( "title:*Matrix* year:1999" ).defaultOperator( Operator.AND );
hits = movies.query( query );
// END SNIPPET: defaultOperator
// with OR the result would be 2 hits
assertEquals( 1, hits.size() );
// START SNIPPET: sortedResult
hits = movies.query( "title", new QueryContext( "*" ).sort( "title" ) );
for ( Node hit : hits )
{
// all movies with a title in the index, ordered by title
}
// END SNIPPET: sortedResult
assertEquals( 3, hits.size() );
// START SNIPPET: sortedResult
// or
hits = movies.query( new QueryContext( "title:*" ).sort( "year", "title" ) );
for ( Node hit : hits )
{
// all movies with a title in the index, ordered by year, then title
}
// END SNIPPET: sortedResult