Lucene Sitecore Search Base Components

Jun 25, 2012
Pablo Bustamante

There are many things that can be done with Sitecore and Lucene, from simple search to complex information retrieval. The following elements are the basic building blocks that will allow you to create them:

Basic Index Configuration

The following configuration will create an index for all items of a specific template and location:

<index id="{indexName}" type="Sitecore.Search.Index, Sitecore.Kernel">
 <param desc="name">$(id)
 </param><param desc="folder">__{indexName}
 </param><Analyzer ref="search/analyzer" />
 <locations hint="list:AddCrawler">
 <customindex type="Sitecore.Search.Crawlers.DatabaseCrawler, 
 Sitecore.Kernel">
 <Database>
 web
 </Database>
 <Root>
 {fullItemPath}
 </Root>
 <Tags>
 {contentTag}
 </Tags>
 <include hint="list:IncludeTemplate">
 <template>
 {templateId}
 </template>
 </include>
 </customindex>
 </locations>
</index>



Replace the following variables as follows:
1.- {indexName} : the descriptive name of your index, something like: "Events", "GlobeNews", etc
2.- {fullItemPath} : The item sitecore path, for example : /sitecore/content/home/news
3.- {templateId} : The template id of the content to be indexed, if not added, then all contnet below the <root> will be indexed.
4.- {contentTag} : the tag that will be applied to all the content of the index.

The configuration must be placed on <configuration><sitecore><indexes> in the web.config file.

History Engine Storage

The following lines need to be added on <configuration><sitecore><databases><database id="web" ...> to enable History Engine so that any change that is published is automatically added to the index we created for the web database, this is something that is not present in the standard Sitecore install, at least till version 6.3.

<Engines.HistoryEngine.Storage>
    <obj type="Sitecore.Data.$(database).$(database)HistoryStorage, Sitecore.Kernel">
        <param connectionStringName="$(id)">
        </param>
    </obj>
</Engines.HistoryEngine.Storage>


C# Code For Simple Search (Query, Sort Field, Number of Elements)

This is a simple code that will take a Query instance (Lucene.Net.Search), with a the sort string and return a maximum (limit) list of items that can be following the custom Item pattern for Sitecore.

        private static List<CustomItem> PerformSearchLuceneSortedQuery
(Query query, string sortField, int limit)
        {
            List<CustomItem> items = new List<CustomItem>(); //define list to return

            Sitecore.Search.SearchResultCollection results;

            using (var context = new Sitecore.Search.IndexSearchContext ("INDEX")) //change "INDEX" with the index id
            {
                Sort sorting = new Sort(sortField, true); //Lucene.Net. Search.Sort, the second parameter tells ascending or descending option
                var hits = new SearchHits(context.Searcher.Search(query, sorting));
                if (limit > 0 && hits.Length > limit)
                    results = hits.FetchResults(0, limit);
                else
                    results = hits.FetchResults(0, hits.Length);
            }
            
            //Take the search results and retrieve the items, if using custom item pattern the implicit operator will cast the item into the customItem.
            foreach (Sitecore.Search.SearchResult result in results)
            {
                Lucene.Net.Documents.Field url = result.Document. GetField("_url");
                Sitecore.Data.ItemUri itemUri = new Sitecore.Data. ItemUri(url.StringValue());
                Sitecore.Data.Items.Item item = Sitecore.Context. Database.GetItem(itemUri.ToDataUri());

                items.Add(item);
            }

            return items;
        }