Skip to content

Sitecore querying with Lucinq index

cardinal252 edited this page Oct 24, 2013 · 4 revisions

Querying sitecore with the Lucinq index is incredibly straight forward, take a look at the sample code below.

SitecoreSearch search = new SitecoreSearch(Constants.IndexPath, new TestDatabaseHelper()))
// get out all items with the given template
ISitecoreQueryBuilder queryBuilder = new SitecoreQueryBuilder(x => x.TemplateId(templateId));
var sitecoreSearchResult = search.Execute(queryBuilder, 20);
	
var sitecoreItemResult = sitecoreSearchResult.GetPagedItems(0, 10);

// the original lucinq search result is available here
// sitecoreSearchResult.LuceneSearchResult
// therefore - for the total number of hits you can use
int totalHits = sitecoreSearchResult.TotalHits;

// for results as a list for convenience you can use
List<Item> results = sitecoreItemResult.Items;

// as with the regular lucene search result - the sitecoreItemResult is IEnumerable<Item>
foreach (Item item in sitecoreItemResult)
{
	// do something with the items        
}

As an added bonus - the syntax is the same for Sitecore versions 6.6 & 7