Session: Querying: How to use Lucene in queries?

Lucene can be used directly by using DocumentQuery method from advanced session operations. It allows us to acquire low-level access to query structure which gives more flexibility.

Syntax

IDocumentQuery<T> DocumentQuery<T, TIndexCreator>() 
	where TIndexCreator : AbstractIndexCreationTask, new();

IDocumentQuery<T> DocumentQuery<T>(
	string indexName,
	bool isMapReduce = false);

IDocumentQuery<T> DocumentQuery<T>();
Parameters
indexName string Name of an index to perform query on
isMapReduce bool Indicates if queried index is a map/reduce index (modifies how we treat identifier properties).
Return Value
IDocumentQuery Instance implementing IDocumentQuery interface containing additional query methods and extensions.

Example I - Basic

// load up to 128 entities from 'Employees' collection
List<Employee> employees = session
	.Advanced
	.DocumentQuery<Employee>()
	.ToList();

// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
List<Employee> employees = session
	.Advanced
	.DocumentQuery<Employee>()
	.WhereEquals(x => x.FirstName, "Robert")
	.ToList();

Example II - Querying specified index

// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
List<Employee> employees = session
	.Advanced
	.DocumentQuery<Employee>("My/Custom/Index")
	.WhereEquals(x => x.FirstName, "Robert")
	.ToList();

or

// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
List<Employee> employees = session
	.Advanced
	.DocumentQuery<Employee, MyCustomIndex>()
	.WhereEquals("FirstName", "Robert")
	.ToList();

Custom methods and extensions

Note

Functionality of most of the methods matches functionality of their Query counterparts and therefore will not be described again. Please refer to appropriate counterpart documentation articles. Links starting with [Query] are marking those articles.

Available custom methods and extensions:

Remarks

By default, if page size is not specified, the value will be set to 128. This is part of Safe-by-Default approach.