Wednesday, May 4, 2016

Implementing simple search functionality in Sitecore using Solr

Ø  Refer the link to configure solr Sitecore Search and indexing --https://sdn.sitecore.net/upload/sitecore6/65/sitecore_search_and_indexing_sc60-65-a4.pdf


This is continuous of my previous blog post “step-by-step-guide-for-setting-up-solr” For implementing simple search page create a search controller, sear result page and search helper classes (models). See the below image for reference.



Index Definition
An index is a physical file populated by a crawler and stored in the data folder of the Website. In Sitecore, an ‘index’ can serve a number of different purposes:
·   From a configuration perspective, an index is a searchable database with a collection of crawlers that populate a database. An Index is identified by its name and stores data in a physical location in the data folder.
     ·   From end-user perspective, an index is a searchable database of documents which links to        actual Sitecore items.

Creating a custom index in Solr
   You can create your own custom search index or you can use the indexes that ships with Sitecore by default such as “sitecore_web_index”.
 To create custom search index in solr you need to go to following node
 <contentSearch> <configuration> <indexes>  and duplicate the exsting index and change the index id ( provide name of your custom index). Please refer the below screen shot for the same.



Configure root node for custom index i.e location of sitecore content tree which u need to index. Please refer the below screen shot for the same.



Understanding parameters of custom index
·         index id : This id we will use in our code to query the search results.
·         <param desc="core">$(id)</param> : name of the core where ur search indexes   are stored.
·         Configuration: By default it will point to the defaultSolrIndexConfiguration node, you can create your own SolrIndexConfiguration node, this is the node which has most of the solr configuration settings.
·         Strategy: This node has list of the different indexing strategies that you can use. You can also combine these strategies to achieve what you want.
·         Crawler: defines which database it should crawl, and where to start.
Refer the below screen shot



Default Solr Index configuration settings
When we look at the config file namedSitecore.ContentSearch.Solr.DefaultIndexConfiguration”, we see lot of configuration setting. One should be aware of some of the common settings such as
·         indexAllFields: Basically this is the master setting, index every field by default or not. The default setting is true
·         fieldNames hint=”raw:AddfieldByFieldName”: This allow you to specifiy how fields will be indexed based on fieldname.
·         fieldTypes hint=”raw:AddFieldByFieldTypeName”: This allow you to specifly how fields will be indexed based on fieldtype
·         Exclude hint=“list:ExcludeTemplate”: this allow you to exclude a specific template to be indexed, use this if you used true as a value for the indexAllfields setting.
·         Exclude hint=”list:ExcludeField”: this allow you to exclude a specific field from your index, use this if you used true as a value for the indexAllfields setting.
·         Include hint:”list:IncludeTempalte”: this allow you to include a specific template to be indexed, use this if you used false as a value for the indexAllfields setting.
·         Include hin:”list:IncludeField”: this allow you to include a specific field to be indexed, use this if you used false as a value for the indexAllfields setting.

Creating custom class for solr search
Before utilizing the created custom index create a custom class that inherits from SearchResultItem, and then implements the field we added to the index.

    public class CustomSearchResultItem : SearchResultItem
    {
        [IndexField("Title")]
        public string Title { get; set; }

          [IndexField("Name")]
        public string Name { get; set; }

          [IndexField("Categories")]
        public string Categories { get; set; }
     }
This will enable you to utilize LINQ to Sitecore like this
var query = context.GetQueryable< SearchHelper >().Where(i => i.Categories.Contains(categoryID));

Writing code for query the search results


Predicate builder
Allows you to create more complex queries using AND and OR and it generates complex Expression trees without having to know about their inner workings.

Switch Solr indexes
·         You can set up Solr to rebuild an index in a separate core so that the rebuilding does not affect the search index that is currently used. Once the rebuilding and the optimization of the index completes, Sitecore switches the two cores, and the rebuilt and optimized index is used.
·         The SwitchOnRebuildSolrSearchIndex class inherits from the SolrSearchIndex class and adds the capability of maintaining two cores for a particular index. Because this is only important for production environments, you can reconfigure your custom index with the SwitchOnRebuildSolrSearchIndex implementation during testing and before moving to a production environment.
     For more detail please see the blog post: https://doc.sitecore.net/sitecore_experience_platform/setting_up__maintaining/search_and_indexing/indexing/switch_solr_indexes


 Sitecore computed index fields
·         A computed index field allows you to perform additional processing before adding data to an index. For example, you may want to store the contents of a droplink field (the raw value being a GUID) as the target item's name, or a particular field value from the target item.
            For building a computed field and debugging please refer below blog post:

No comments:

Post a Comment