Getting Started with Sitecore Search
If this is your first time with Sitecore and with Sitecore's search, you may be a little confused with the Sitecore documentation. It's actually pretty easy if you know what you are doing and know LINQ statements from standard .NET code. This tutorial works whether you are using the built-in Lucene index or have an external SOLR server.
How does the search work on Sitecore?
When looking for items the API doesn't connect directly to any database (master, core, web); rather it connects to an external index. This means that Sitecore will use a configuration file to access the corresponding Lucene or Solr engine to get the information.
This configuration file will allow the user to choose what and how the engine will save the indexes. This will help to improve the performance and the quality of the results that a search query will get.
The index configuration file is located inside the web application on the /App_Config/Include folder. There are specific configurations for each of Sitecore databases.
It doesn't matter which search engine you use. Any LINQ statement is translated into something that Lucene or Solr will understand, and the user will get an object as a result.
The main difference between Lucene and Solr is that Solr is an enterprise-level platform that contains most of Lucene's functionality, making it a great option when doing a large-scale project. However Sitecore provides configurations for l=Lucene out of the box
Which are the main steps to performing a simple search?
1) We need to obtain the index that we are going to use. This is done by using the ContentSearchManager class that comes on the Sitecore.ContentSearch.dll, and by looking for it by its name. Example:
var selectedIndex = ContentSearchManager.GetIndex(
For the indexes names we can refer to the following list:
2) Once we have the index object we need to create a search context by calling the CreateSearchContext() method of the object.
var searchContext = selectedIndex.CreateSearchContext();
3) By using the context method GetQueryable<T>() we can start defining our LINQ statements for filtering and obtaining items.
var result = searchContext.GetQuaeryable<SearchResultItem>().Where(x => x[“Title”].Contains(“cars”)).Take(20).ToList();
For testing your queries, Sitecore provides a friendly interface called the LINQscratchPad, we can use this by navigating through
website/sitecore/admin/linqscratchpad.aspx. On this screen we can put our code ( please be aware that if you are using custom classes, you need to implement them on this screen as well) and then we will get our result as a table.
By default the API will map to the SearchResultItem class for the result, but we can create any custom class for getting the results as we may need.