Searching The Index In Azure Portal And Implementation Using Visual Studio

In this article we will see how to do searching in Azure portal and search implementation through Visual Studio.

Before starting I assume you have knowledge about how to create a search service, and how to create an index on the service. And if you want to know more read my previous articles:  Part-1, Part-2

Searching in the Azure Portal

In my previous article I created Azure search service and an index on it using import data.

Searching The Index In Azure Portal And Implementation Using Visual Studio

Go to Indexes 

Searching The Index In Azure Portal And Implementation Using Visual Studio

Search explorer -> enter query string to search -> Click Search -> Results shown below:

Searching The Index In Azure Portal And Implementation Using Visual Studio
Query String – enter the string you want to search.

The text to search for All searchable fields are searched by default unless searchFields is specified.

To match any term, use * (this can be useful for Boolean filter queries). To read more about simple query syntax read msdn here

You can read more about filters using MSDN link here

Searching The Index In Azure Portal And Implementation Using Visual Studio

NOTE
String comparisons are case sensitive [when you specify a filter]

For example - when we use:

Searching The Index In Azure Portal And Implementation Using Visual Studio
then we will get the exact result:
 
Searching The Index In Azure Portal And Implementation Using Visual Studio

BUT,

If we replace ‘Matthew’ with ‘matthew’ the result will be as shown:

Searching The Index In Azure Portal And Implementation Using Visual Studio

So string comparisons are case sensitive. [when you specify a filter]

Searching The Index In Azure Portal And Implementation Using Visual Studio
 
Implementation of Searching using Visual studio STEP 1: Create empty MVC web application

Open Visual studio

Click on File-> new-> Project

Searching The Index In Azure Portal And Implementation Using Visual Studio

Select asp.net web application, enter project name as searchingIndex and click ok,

Searching The Index In Azure Portal And Implementation Using Visual Studio

Choose empty MVC application and click ok:

Searching The Index In Azure Portal And Implementation Using Visual Studio
Searching The Index In Azure Portal And Implementation Using Visual Studio

Empty MVC application is created.

Searching The Index In Azure Portal And Implementation Using Visual Studio

STEP 2 - Install Microsoft.Azure.search using NuGet Package manager

Right click on references and select Manage NuGet Pakages

Searching The Index In Azure Portal And Implementation Using Visual Studio

OR,

Go to Tools -> NuGet Pakage Manager -> Manage NuGet Package for Solution

Searching The Index In Azure Portal And Implementation Using Visual Studio

Search for Microsoft.azure.search

Searching The Index In Azure Portal And Implementation Using Visual Studio

Click on Install,

Searching The Index In Azure Portal And Implementation Using Visual Studio
Searching The Index In Azure Portal And Implementation Using Visual Studio

Click on Accept,

Searching The Index In Azure Portal And Implementation Using Visual Studio

Microsoft.Azure.Search is installed successfully as shown.

Searching The Index In Azure Portal And Implementation Using Visual Studio

Searching The Index In Azure Portal And Implementation Using Visual Studio

STEP 3 - Add Reference for System.Net.Http

Right click on references -> add reference

Searching The Index In Azure Portal And Implementation Using Visual Studio

Search System.Net.Http and click ok

Searching The Index In Azure Portal And Implementation Using Visual Studio

Reference added successfully in solution.

Searching The Index In Azure Portal And Implementation Using Visual Studio
STEP 4 - Add a new empty controller

Right click on Controller folder -> click on Add -> Select Controller as shown.

Searching The Index In Azure Portal And Implementation Using Visual Studio

Select MVC 5 controller - Empty

Searching The Index In Azure Portal And Implementation Using Visual Studio

Name the controller as “HomeController” and click on Add.

Searching The Index In Azure Portal And Implementation Using Visual Studio

Controller created as shown

Searching The Index In Azure Portal And Implementation Using Visual Studio
 
STEP 5 - Declare Service Name

Define a variable to hold service name as we created in the Azure portal. 

  1. //declare serch service name - host portion of search service URL in azure portal  
  2. var sSearchService = "stestsearchservice";  

The service name is the host portion of search service URL in Azure portal

Searching The Index In Azure Portal And Implementation Using Visual Studio

STEP 6 - Declare apiKey

As specified on MSDN about api-Key

Searching The Index In Azure Portal And Implementation Using Visual Studio

So under Azure search service -> click on keys – Copy Primary admin key

Searching The Index In Azure Portal And Implementation Using Visual Studio

Use that apiKey here in the code.

  1. public ActionResult Index() {  
  2.     //declare serch service name - host portion of search service URL in azure portal  
  3.     var sSearchService = "stestsearchservice";  
  4.     // apiKey here  
  5.     var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";  
  6.     //search client  
  7.     var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));  
  8.     //index client  
  9.     var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");  
  10.     //define serach parameters  
  11.     SearchParameters sp = new SearchParameters() {  
  12.         SearchMode = SearchMode.All  
  13.     };  
  14.     //Grab collection of documents  
  15.     var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);  
  16.     return Json(sDocs.Results, JsonRequestBehavior.AllowGet);  
  17. }  
STEP 7 - Search Service Client

Define the Search client [ import Microsoft.Azure.Search namespace]

Pass the ‘searchServiceName’ and ‘searchCredentials’ to searchServiceClient class as shown.

  1. public ActionResult Index() {  
  2.     //declare serch service name - host portion of search service URL in azure portal  
  3.     var sSearchService = "stestsearchservice";  
  4.     // apiKey here  
  5.     var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";  
  6.     //search client  
  7.     var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));  
  8.     //index client  
  9.     var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");  
  10.     //define serach parameters  
  11.     SearchParameters sp = new SearchParameters() {  
  12.         SearchMode = SearchMode.All  
  13.     };  
  14.     //Grab collection of documents  
  15.     var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);  
  16.     return Json(sDocs.Results, JsonRequestBehavior.AllowGet);  
  17. }  

 Inside SearchCredentials we need to specify apiKey which is used to authenticate Azure Search service,

Searching The Index In Azure Portal And Implementation Using Visual Studio

STEP 8 - Index Client

Now we are going to get index using searchSearvieclient and the using that index we will get Index client.

Index client is used to manage documents and query inside a specified index.

  1. public ActionResult Index() {  
  2.     //declare serch service name - host portion of search service URL in azure portal  
  3.     var sSearchService = "stestsearchservice";  
  4.     // apiKey here  
  5.     var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";  
  6.     //search client  
  7.     var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));  
  8.     //index client  
  9.     var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");  
  10.     //define serach parameters  
  11.     SearchParameters sp = new SearchParameters() {  
  12.         SearchMode = SearchMode.All  
  13.     };  
  14.     //Grab collection of documents  
  15.     var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);  
  16.     return Json(sDocs.Results, JsonRequestBehavior.AllowGet);  

 

STEP 9 - Define Search Parameters

Searching The Index In Azure Portal And Implementation Using Visual Studio

There are two types of search modes,

  1. All
  2. Any (Default)
The following offers a good explanation:

Searching The Index In Azure Portal And Implementation Using Visual Studio

To read more about operators in simple query syntax read here,

Searching The Index In Azure Portal And Implementation Using Visual Studio
 
Specify the string you want to search

  1. public ActionResult Index()  
  2. {  
  3.     //declare serch service name - host portion of search service URL in azure portal  
  4.     var sSearchService = "stestsearchservice";  
  5.     // apiKey here  
  6.     var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";  
  7.     //search client  
  8.     var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));  
  9.     //index client  
  10.     var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");  
  11.     //define serach parameters  
  12.     SearchParameters sp = new SearchParameters() {  
  13.         SearchMode = SearchMode.All  
  14.     };  
  15.     //Grab collection of documents  
  16.     var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);  
  17.     return Json(sDocs.Results, JsonRequestBehavior.AllowGet);  
  18. }  

 

Instead of returning view, I’ll return Json:

Searching The Index In Azure Portal And Implementation Using Visual Studio

  1. public ActionResult Index() {  
  2.     //declare serch service name - host portion of search service URL in azure portal  
  3.     var sSearchService = "stestsearchservice";  
  4.     // apiKey here  
  5.     var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";  
  6.     //search client  
  7.     var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));  
  8.     //index client  
  9.     var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");  
  10.     //define serach parameters  
  11.     SearchParameters sp = new SearchParameters() {  
  12.         SearchMode = SearchMode.All  
  13.     };  
  14.     //Grab collection of documents  
  15.     var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);  
  16.     return Json(sDocs.Results, JsonRequestBehavior.AllowGet);  
  17. }  
Run the solution, and we will get the search result in json as we saw in the portal.

 

Searching The Index In Azure Portal And Implementation Using Visual Studio

Searching The Index In Azure Portal And Implementation Using Visual Studio

Summary

In this article we saw,

  • How to search in Azure portal using simple query string
  • How to create a Visual Studio solution for searching
In the next article I’ll show you how to use $filter in searching the documents.

Keep reading!!!