Query vs DocumentQuery

You might be wondering why does the RavenDB client offer two ways of querying by exposing query as well as documentQuery methods and what are differences between them.

documentQuery is the lower level API that we use to query RavenDB. Therefore we have created query.

query is just a wrapper of documentQuery, so when you use query it always is translated to documentQuery object, which then builds a Lucene-syntax query that is sent to the server. However we still expose documentQuery in advanced options to allow the users to have the full power of Lucene available to them.

Immutability

documentQuery is mutable while query is immutable. It means that you might get different results if you try to reuse a query. The usage of query method like in the following example:

QQueryAndLuceneQuery_User u = QQueryAndLuceneQuery_User.user;
IRavenQueryable<User> query = session
  .query(User.class)
  .where(u.name.startsWith("A"));

IRavenQueryable<User> ageQuery = query.where(u.age.gt(21));

IRavenQueryable<User> eyeColor = query.where(u.eyeColor.eq("blue"));

will cause that the queries will be translated into following Lucene-syntax queries:

query - Name:A*

ageQuery - (Name:A*) AND (Age_Range:{Ix21 TO NULL})

eyeQuery - (Name:A*) AND (EyeColor:blue)

The similar usage of documentQuery:

IDocumentQuery<User> documentQuery = session
  .advanced()
  .documentQuery(User.class)
  .whereStartsWith(u.name, "A");

IDocumentQuery<User> ageDocumentQuery = documentQuery.whereGreaterThan(u.age, (byte)21);

IDocumentQuery<User> eyeDocumentQuery = documentQuery.whereEquals(u.eyeColor, "blue");

// here all of the DocumentQuery variables are the same references

documentQuery - Name:A* (before creating ageQuery)

ageLuceneQuery - Name:A* Age_Range:{Ix21 TO NULL} (before creating eyeDocumentQuery)

eyeLuceneQuery - Name:A* Age_Range:{Ix21 TO NULL} EyeColor:blue

In result all created Lucene queries are the same query (actually the same instance). This is important hint that you should be aware if you are going to reuse documentQuery.

Default query operator

The example above shows an another difference between querying methods. Note that the usage of where statement resulted in AND operator in the final Lucene query when using query method. In case of documentQuery usage the Lucene query has no operator between query conditions what means that OR will be used. This is the default operator of Lucene engine. You are able to change that by using usingDefaultOperator:

session
  .advanced()
  .documentQuery(User.class)
  .usingDefaultOperator(QueryOperator.AND);