Sitecore 6 Lucene Search: The most basic setup

07 November 2013
Marek Musielak
Frink_Cognifide_2016_HeaderImages_0117

sitecore-lucene-search-and-indexing

This is the first blog post from the series of articles about using Lucene in Sitecore 6 solutions. It assumes that you know some basics of Sitecore but no Lucene knowledge is required. In this blog post I'm going to show you how to setup the most basic custom Lucene index in Sitecore and how to use it. Other topics related with Sitecore and Lucene will be covered in further posts.

The web application I'm working on is an empty Sitecore 6.6 rev.130529 (Service Pack-1) solution without any modifications.

First step of introducing a custom Lucene index to our application is to create a definition of an index in Sitecore configuration. I've created a Search.config file in the App_Config/Include directory with the content:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
  <search>
   <configuration>
    <indexes>
     <index id="custom-index" type="Sitecore.Search.Index, Sitecore.Kernel">
      <!-- name of the index displayed in the Sitecore Control Panel -->
      <param desc="name">Custom Index</param>
      <!-- folder in which index file will be stored -->
      <param desc="folder">__$(id)</param>
      <!-- reference to the analyzer defined in Sitecore config -->
      <Analyzer ref="search/analyzer" />
      <!-- list of locations which will be index by our index -->
      <locations hint="list:AddCrawler">
       <!-- our first and only location crawled by standard Sitecore crawler -->
       <custom-loc-1 type="Sitecore.Search.Crawlers.DatabaseCrawler,Sitecore.Kernel">
        <!-- location root is Home item in master database -->
        <Database>master</Database>
        <Root>/sitecore/content/Home</Root>
       </custom-loc-1>
      </locations>  
     </index>
    </indexes>
   </configuration>
  </search>
 </sitecore>
</configuration>

After this configuration is added, it should be included in the merged Sitecore config which can be displayed when you navigate to http://localhost/sitecore/admin/showconfig.aspx. If you cannot find it there, double check if you placed the new config file in the proper folder, if its name ends with '.config' and check for any typos within it. Also if the configuration is valid and the application is started, you should see a new folder in the Data/Indexes directory of your application.

Next step is to rebuild the index. When you login to Sitecore Desktop, start Control Panel application and run Rebuild the Search Index. Now select Custom Index checkbox and click Rebuild >. You should get the information with details of the rebuilding process. Note that this is required only once after the index is defined - all updates to the Sitecore items will be reindexed automatically.

If your using older version of Sitecore and your custom index is not displayed in the Rebuild the Search Index application, you will need to rebuild your index manually by executing the following line of code:

Sitecore.Search.SearchManager.GetIndex("custom-index").Rebuild();

Now the new index is ready and working. You can search for all the items included in the new index be executing this method:

public IEnumerable<Item> GetAllIndexedItems()
{
 using (IndexSearchContext sc = 
           SearchManager.GetIndex("Custom Index").CreateSearchContext())
 {
  // search using 'match all' query and checking maximum number of items
  TopDocs topDocs = sc.Searcher.Search(new MatchAllDocsQuery(), int.MaxValue);

  // read search hits from the found Lucene docs
  SearchHits searchHits = new SearchHits(topDocs, sc.Searcher.GetIndexReader());

  return searchHits
   // fetch all hits starting from the first one
   .FetchResults(0, int.MaxValue)
   // and retrieve Sitecore item for all of them
   .Select(r => r.GetObject<Item>()).ToList();
 }
}

In my case, as I'm using an empty Sitecore application, the method above returns only the Home item. If you're working in your solution, you should get all the items below the 'Home' node.

And that's it. The simplest custom Lucene index in Sitecore definition and search in 5 minutes. If you have any questions or comments or want me to cover any other topic concerning Sitecore, do not hesitate to leave your feedback below and if you're interested in more information about Lucene and Sitecore, check out my other Sitecore blog posts.