Searching Using Lucinq with Sitecore 7 default indexes

In this post I will be looking at using Lucinq to drive search using the default sitecore 7 indexing.

Lucinq provides the ability to use the generic forms of the standard Term, Phrase, Wildcard and other methods. This allows the assignment of an expression to denote which of the fields is the target of the query.

In the line below, you can see an example of this in action

queryBuilder.Term(t => t.Content, "value")

This tells Lucinq to read the IndexField attribute from the Content field of the SearchResultItem class and use that as the field for the Lucene query that is to be sent down.

* Note, the .HasBaseTemplate() method of Lucinq’s query extensions requires a change to the default Sitecore indexing settings. *

Here is an example in situe – this can be can in unit tests WITHOUT the need to run the sitecore initialise pipeline (unlike SC 7 search).

// the index lives in the appdata folder, so using a Server.MapPath to get the real path for Lucene.
string searchIndexPath = Server.MapPath(Settings.IndexFolder + @"/lucinqwebindex");
using (SitecoreSearch search = new SitecoreSearch(searchIndexPath))
{
     ISitecoreQueryBuilder queryBuilder = new SitecoreQueryBuilder(SitecoreMode.Sitecore7);
     queryBuilder.TemplateId(ContentIds.HomeTemplateId); // add template id to the query
     
     // below users the sitecore index field attribute, its the same as 
     // queryBuilder.Term("content", "value");
     queryBuilder.Term(t => t.Content, "value");// set up the term based on the in built sitecore 7 class.
     ISitecoreSearchResult searchResult = search.Execute(qb);
     int totalResults = searchResult.TotalHits; // gets the total number of results
     List items = searchResult.GetPagedItems(1, 10);
}

More information can be found on Lucinq on github here

https://github.com/cardinal252/Lucinq/wiki/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s