27
Sitecore Content Search API USING SOLR SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Content search api in sitecore 8.1

Embed Size (px)

Citation preview

Page 1: Content search api in sitecore 8.1

Sitecore Content Search API USING SOLR

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 2: Content search api in sitecore 8.1

SolrSolr 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 BANGALORE SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 3: Content search api in sitecore 8.1

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

Page 4: Content search api in sitecore 8.1

Solr SetUpJava 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:\MyProject\Solr\solr-5.4.1

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 5: Content search api in sitecore 8.1

Running Solr as Windows service using NSSMNSSM 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

Page 6: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

NSSM SetupDownload NSSM

Download nssm 2.24 from https://nssm.cc/release/nssm-2.24.zip

Extract NSSM zip into folder for eg: D:\MyProject\nssm-2.24\nssm- 2.24\

Run the below command in command prompt

D:\MyProject\nssm-2.24\nssm- 2.24\win64\nssm install solr5.4.1

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 7: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Installing SOLR….This command will open NSSM service installer window

Enter D:\MyProject\Solr\solr-5.4.1\bin\solr.cmd in Path

Enter D:\MyProject\Solr\solr-5.4.1\bin in Startup Directory

Enter start –f –p 8983 in Arguments

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 8: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Check for successful installationIf 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 BANGALORE BY NIDHI SINHA

Page 9: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Check if service is running or notRight click on this service and click on Start

Now go to browser and browse http://localhost:8983/solr

It will show Solr interface.

Page 10: Content search api in sitecore 8.1

Integrating Sitecore with SolrDisable / Delete Lucene configuration files

Take the back up of your Website

Go to \Website\App_Config\Include 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

Page 11: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Enable Solr configuration filesGo to \Website\App_Config\Include folder in website and search for “Solr”

Enable all the Solr config files by removing .disabled/.example from the file names

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 12: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Sitecore IndexingLogin 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 BANGALORE BY NIDHI SINHA

Page 13: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Group Query on Solrhttp://localhost:8983/solr/sitecore_web_index/select?q=_group:02853efdd2864a7eaef955676dc5a735

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 14: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

What is SOLRNet and how its usedSolrNet 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 BANGALORE BY NIDHI SINHA

Page 15: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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 BANGALORE BY NIDHI SINHA

Page 16: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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;

Page 17: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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);

Page 18: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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 BANGALORE BY NIDHI SINHA

Page 19: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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 BANGALORE BY NIDHI SINHA

Page 20: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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 BANGALORE BY NIDHI SINHA

Page 21: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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 BANGALORE BY NIDHI SINHA

Page 22: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

 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 BANGALORE BY NIDHI SINHA

Page 23: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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 BANGALORE BY NIDHI SINHA

Page 24: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Features of ContentSearch API Sorting

Pages

The many face(t)s of a search query

Dealing with more complex queries

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 25: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

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 BANGALORE BY NIDHI SINHA

Page 26: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Example of Computed Field

SITECORE USER GROUP BANGALORE BY NIDHI SINHA

Page 27: Content search api in sitecore 8.1

SITECORE USER GROUP BANGALORE

Credits UI/UX Saurabh Sinha , Sapient Nitro

Topic Selection Sateesh Chandolu, Sapient Nitro

SITECORE USER GROUP BANGALORE BY NIDHI SINHA