Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Content search api in sitecore 8.1

1,652 views

Published on

Session on Sitecore Content Search API by Nidhi Sinha from Sapient. (Sitecore User Group Bangalore)

Published in: Technology
  • Be the first to comment

Content search api in sitecore 8.1

  1. 1. Sitecore Content Search API USING SOLR SITECORE USER GROUP BANGALORE BY NIDHI SINHA
  2. 2. Solr Solr is built around Lucene Lucene allows us to add search capability to our applications, and exposed an easy-to-use API, while hiding all the search-related complex operations Solr is a web application, that offers an entire infrastructure and a lot more features in addition to what Lucene offers, making it more manageable to work with powers provided by Lucene. SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  3. 3. Sitecore is SMART  Sitecore, to make our life easier actually provides an abstraction over the low level details of working with native search technologies like Lucene and Solr  Use one API from Sitecore, to work with either Lucene or Solr  Sitecore were to support a new search index technology in addition to Lucene and Solr, like Elasticsearch (which is also built on top of Lucene), they could fit this in the same way, and we won't need to change any of our query specific code, only configuration SITECORE USER GROUP BANGALORE BY NIDHI SINHA
  4. 4. Solr SetUp Java is prerequisite to setup Solr. So, download Java from http://www.java.com/en/download/ and install it if it’s not already installed.  Download solr-5.4.1.zip folder from htttp://www.us.apache.org/dist/lucene/solr/5.4.1  Extract zip file into folder for eg: D:MyProjectSolrsolr-5.4.1 SITECORE USER GROUP BANGALORE BY NIDHI SINHA
  5. 5. Running Solr as Windows service using NSSM NSSM is a service helper which doesn’t suck. Other service helper programs suck becausethey don’t handle failure of the applications running as a service. NSSM also features a graphical service installation and removal facility. SITECORE USER GROUP BANGALORE BY NIDHI SINHA
  6. 6. NSSM Setup Download NSSM Download nssm 2.24 from https://nssm.cc/release/nssm-2.24.zip Extract NSSM zip into folder for eg: D:MyProjectnssm-2.24nssm- 2.24 Run the below command in command prompt  D:MyProjectnssm-2.24nssm- 2.24win64nssm install solr5.4.1 SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  7. 7. Installing SOLR…. This command will open NSSM service installer window  Enter D:MyProjectSolrsolr-5.4.1binsolr.cmd in Path Enter D:MyProjectSolrsolr-5.4.1bin in Startup Directory Enter start –f –p 8983 in Arguments SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  8. 8. Check for successful installation If service is installed successfully, it will show message below message Go to windows services(sevices.msc) and check Solr 5.4.1 service is available or not. SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  9. 9. Check if service is running or not Right click on this service and click on Start Now go to browser and browse http://localhost:8983/solr It will show Solr interface. SITECORE USER GROUP BANGALORE
  10. 10. Integrating Sitecore with Solr Disable / Delete Lucene configuration files Take the back up of your Website Go to WebsiteApp_ConfigInclude folder in website and search for“Lucene” Select all the files and delete them (disable them by adding .example at the end) SITECORE USER GROUP BANGALORE BY NIDHI SINHA
  11. 11. Enable Solr configuration files Go to WebsiteApp_ConfigInclude folder in website and search for “Solr” Enable all the Solr config files by removing .disabled/.example from the file names SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  12. 12. Sitecore Indexing Login to Sitecore and go to Control Panel  Click on Indexing Manager  Select index name to rebuild  Click on Rebuild and wait till the indexing is completed.  After indexing completed, browse below url and check records are there are not http://localhost:8983/solr/sitecore_master_index/select?q=* Similarly repeat above steps to rebuild all the indexes. SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  13. 13. Group Query on Solr http://localhost:8983/solr/sitecore_web_index/select?q=_group:02853efdd2864a7eaef955676 dc5a735 SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  14. 14. What is SOLRNet and how its used SolrNet is an Apache Solr client for .NET Using these SolNet dlls we can build queries to get results from Solr. Step 1: Have a model to Fetch your results into Example of a Model class. SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  15. 15. Model class using SolrNet using SolrNet.Attributes; namespace MyProject.Model { public class MyResult { #region Generic [SolrField("title_t")] public string Title { get; set; } [SolrField("description_t")] public string Description { get; set; } } } SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  16. 16. Example of a SolrNet Query QueryOptions options; if (!string.IsNullOrWhiteSpace(langauge)) { options = new QueryOptions { FilterQueries = new ISolrQuery[] { new SolrQueryByField("_language", langauge), new SolrQueryByField("title_t", "My Title"), new SolrQueryInList("country_sm",countries) } }; return options; SITECORE USER GROUP BANGALORE
  17. 17. Create a Generic Method to establish connection to Solr Public class GetSolrConnection<T>() { ISolrOperations<T> solr; solr = SolrOperations.ConnectToIndex<T>(_solrUrl); return solr; } return GetSolrConnection<MyClass>().Query(query,options); SITECORE USER GROUP BANGALORE
  18. 18. Example of Model using Content Search API for SOLR using Sitecore.ContentSearch; using Sitecore.ContentSearch.SearchTypes; using System; using System.Collections; using System.Collections.Generic; namespace MyProject.Models { public class TestClass : SearchResultItem { [IndexField("title_t")] public string Title { get; set; } [IndexField("publisheddate_t")] public string DisplayDate { get; set; } } } SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  19. 19. Highlights of Content Search API ISearchIndex index = ContentSearchManager.GetIndex("sitecore_master_index") using (IProviderSearchContext context = index.CreateSearchContext()) { var results = context.GetQueryable<TestClass>().Where(x => x.Content.Contains(“Test")); } SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  20. 20. How the Query Works: Get a handle to the search index you want to work on The GetIndex(string indexName) method on the ContentSearchManager instance that returns a ISearchIndex instance. The ISearchIndex instance represents the given search index, where you will be able to get different informations about the actual index, but you can also do things like triggering a rebuilding of the index SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  21. 21. How the Query Works: Open a connection to the search index This is done by calling the CreateSearchContext() method, that effectively opens a connection to the search index It’s like opening a database connection SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  22. 22. Perform queries on the search index Call the GetQueryable<T>() method context instance , that returns an instance of type IQueryable<T> This is where the really cool part comes, as you are now able to write standard LINQ queries using the IQueryable<T> instance, where you can tune your search query against data in the search index. SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  23. 23. Important for Content Search API The generic parameter T can be of any type, as long as it either is, or inherits from, the SearchResultItem base class, which is the default implementation provided by Sitecore. SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  24. 24. Features of ContentSearch API Sorting Pages The many face(t)s of a search query Dealing with more complex queries SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  25. 25. Example of Content Search API with Coveo Content Search API model class with Coveo public class BlogItem : SearchResultItem { [IndexField("RelatedContent")] [TypeConverter(typeof(IndexFieldEnumerableConverter))] public virtual IEnumerable<ID> RelatedContentIds { get; set; } } Configuration File Entry <fieldType fieldName="RelatedContent" isMultiValue="true" isSortable="false" isFacet="false" includeForFreeTextSearch="false" settingType='Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework' /> SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  26. 26. Example of Computed Field SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA
  27. 27. Credits UI/UX  Saurabh Sinha , Sapient Nitro Topic Selection  Sateesh Chandolu, Sapient Nitro SITECORE USER GROUP BANGALORESITECORE USER GROUP BANGALORE BY NIDHI SINHA

×