Implementing Sitecore Search With Azure

15 September 2017
1060 Views

Did you know? Approximately 30% of visitors online, prefer using the search site box for searching any product. It is these users who perhaps show some intention to make a purchase of a possible product.

On any website, a perfectly implemented search function can greatly enhance the user experience.  Search doesn’t mean it should only check for exact match of a word/sentences, it should be a robust search that corrects spelling errors, allows users to refine searches and automatically suggests possible results while users type and many more.

Why Azure Search?

Azure is a cloud based service. It’s very easy to integrate it with any website. Since it runs in the cloud, it does not increase the load on your servers. Microsoft adds more resources to Azure Search as needed to keep the service’s performance high

Implementing Azure Search With Sitecore

So now, we will see how to integrate Azure search with Sitecore. Before starting this tutorial ahead, make sure you have a Azure Subscription (if not, you can setup a 30 day trial account on Azure). I am implementing this tutorial on a vanilla sitecore instance (8.2 Update 5) named AzureSerch and VS 2015.

lets_start

Lets Start Azure Search

 

  • Login to your Azure portal account (portal.azure.com)
  • Click More Services
  • In the Filter field, enter Search service and click Search services.
configure-azure-search-picture

Search Service on Azure Portal

  • In the Search services window, click Add.
  • Specify the name of your Search Service instance. (For this, I have given AzureSearch)
  • Choose the type of subscription that suits you.
  • Choose the appropriate resource group, location, and pricing tier that suits you. (Sitecore uses 15 search indexes, and therefore requires the S Standard pricing tier as a minimum)
  • When the Search Service instance is configured, click Create.
  • Open your newly created search service and click SettingsKeys
  • Copy the Primary admin key or the Secondary admin key to the apiKey parameter. It doesn’t matter which key you choose to use, but the key will be used in the connection string

Once all the above steps gets completed, search service will look like this

azure search service

Azure Search Service Screen

 

Till now we have only created a Search Service in Azure Portal. We haven’t linked it with any Sitecore instance.

Now, we will see how to map this service with Sitecore. For this we need three things, using which we will create a connection string

  • Service Url
  • API Version
  • API Key

Service URL in this case is https://azuresitecor.search.windows.net

Current API Version is 2016-09-01

API Key you can get from Azure Portal (Search Service–>Key)

Format the connection string in below format

<add name=”cloud.search” connectionString=”serviceUrl=https://azuresitecor.search.windows.net;apiVersion=2016-09-01;apiKey=YOURKEYGOESHERE” />

Add the key in connectionstring.config file.

Enabling Azure Search Files

  • Go to include folder of your website
  • Search for Lucene
  • Disable all Lucene files (either add .disabled or delete them)
  • Search for Azure (21 result will appear)
  •  Enable all the azure files by removing .example or .disabled from file name.

Now, login to sitecore. Navigate to control panel, indexing manager, and rebuild all the indexes. It may take upto 15 min to do indexing for the first time.

Once all the indexes will be done, you can see the indexes on Azure Portal under your Search Service.

Azure Search Indexes

Azure Search Indexes

Azure Search Fun Begins

Before executing query, I have created a item just under Home, with name “Nitish” and provided title as “Nitish”.

Publish the item, so that indexing gets fired at publish end event, and indexes got updated on the web and master index.

Before executing any query, we need to know the fields of the indexes. To know the fields click on Search Services, then click on Search Index (i.e. sitecore_web_index)

search index fields

Now there are three ways to execute the query

  • Execute Query On Azure Portal
  • Execute Query using Rest API
  • Execute Query using .net Code

Executing Query on Azure Portal

To execute query using Azure Portal, Click on index on which you want to hit the query and then click on Search Explorer.

Search Explorer window will open. Default text box will open with suggested text. We will search for the item whose “title” is “nitish”. Write “title”:”nitish” and press search.

Search Query on Azure Portal

 

A json formatted result will come. It will have all the fields which are related to that item.

Execute Query using Rest API

To Execute Query using Rest API, there are two ways. GET or POST. I will demonstrate POST method using Postman tool.

For posting query using POST tool we need to know following things

  • URL : https://azuresitecor.search.windows.net/indexes/sitecore-web-index/docs/search?api-version=2016-09-01 [Change index name, if you want to search on some other index

We need to pass three Parameters in header along with POST request

Post Request Header

Post Request Header

And in body we will format our query. For this I am passing query like Search for “nitish” and return only “title” and “account” field

Body Post Request

Body Post Request

Click on Send, a json formatted result will come.

Execute Query using .net

Before executing query using .net, we need to create a Model, which can hold the result. For this blog purpose, I have created a class having “title” field only.

model

You need to add a nuget of “Microsoft.Azure.Search”

Format the code in the following manner

Azure Search Code

Azure Search Code

Kindly Note, only pass ServiceName (i.e. azuresitecor) without https and search.window.net

I have applied debugger and result will come in following manner

Azure Search Result

Azure Search Result

You can see, Count=1 and result is having title as “nitish”.

Finally

Finally Azure Search

Get in touch with me, if you have any query. I will be delighted to answer your query.

 

 

 

 

 

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *