Searching with Lucinq and Glass Mapper

In this post I will cover using Lucinq with sitecore – in particular returning the results through the Glass Sitecore Mapper.

Returning results from lucene is something we as Sitecore developers have been used to for some time. Lucene provides hyper fast searching and its flexible storage makes it great for indexing content. However, in use, the API is somewhat verbose and not necessarily the easiest to understand without reading up on it first (I still would recommend the book Lucene In Action to get a grounding in the product).

I have a few issues with the search layers that Sitecore have implemented over the years which I won’t go into in this post.

When I set about a large project in Sitecore for a firm here in the UK, search was one of the key complexities in the build, so I opted to write a wrapping layer to make my life simpler when using the Lucene. This was the foundation for Lucinq. I then started to think about how to return the results in a nicer format given that the Lucene Document class is just a little clumsier than I would like for what I was trying to achieve. So to this end I wrote a wrapper to allow me to return objects as classes.

So – enough rambling – this in short allowed me to specify how to get objects out of Sitecore using an ORM – and my ORM of choice is currently still Glass Sitecore Mapper.

Lucinq has several methods of querying glass – at present, it will only read the automapped and attribute based configuration’s, support is in development for fluent based configuration. You can attempt to mix and match the IndexField and glass field attributes if you wish, though you do so at your own risk.

Lucinq provides generic implementations of most of the standard Sitecore query building functions. These will read the configuration to attempt to ascertain which index field you are to be querying. They will also define the type of data that you are getting back. This (unlike Sitecore 7 search) is done using the glass sitecore mapper and instantiating the objects from the Sitecore database meaning that the indexes only need contain the values that are being searched upon, not storing excessive amounts of data in the index for the sole purpose of retrieval.

Below we can see an example of how you can set up a query with return from the Glass Mapper Sitecore Service

// assuming glass is already set up
ISitecoreService service = new SitecoreService("web");
LuceneSearch search = new LuceneSearch("path to my index");

// get a normal sitecore query builder
ISitecoreQueryBuilder queryBuilder = new SitecoreQueryBuilder();

// use one of the glass mapper extensions to setup the query
queryBuilder.Field(t => t.PageTitle, "new");

// get the search result
var sitecoreSearchResult = new GlassSearchResult(service, search.Execute(queryBuilder));

One thought on “Searching with Lucinq and Glass Mapper

  1. Pingback: Glass Mapper – Introducing Glass Maps | CardinalCore

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s