Azure Search API

Introduction

Azure Search is a search-as-a-service cloud solution that gives developers APIs and tools for adding a rich search experience over private, heterogeneous content in web, mobile, and enterprise applications.

Retrieve the search result from Azure Blob.

In this article, I will create the MVC web application that receives the search result from the Azure Blob.

First, we need to create the Azure search service in the portal.

Open Azure portal Click the plus sign ("+") in the top left corner. Select Web + Mobile > Azure Search.

Fill in the required fields as in the below image:

Overview Of Azure Search

 

Then click on create.

Overview Of Azure Search

 

Open the search service, from dashboard click on import data.

Overview Of Azure Search

 

Select the data source, then click Ok.

Overview Of Azure Search

 

Ignore Cognitive Search option

Now, we need to Customize target index by clicking on index option, fill in the index name, key, and then select the search result fields, then click OK.

Overview Of Azure Search

 

Next, we need to create indexer, fill in the name and define the schedule, then click Ok.

Overview Of Azure Search

 

From search, dashboard click on search explore to test the result 

Overview Of Azure Search

 

Type search keyword and check the results.

Overview Of Azure Search

 

Next, we need to create MVC App to query your Azure Search index using the .NET

Open Visual Studio, create a new project, select web, select ASP.NET Web Application, select MVC,

Overview Of Azure Search

 

Right click on the reference and select Manage NuGet Packages

Overview Of Azure Search

 

Select browse and type Microsoft.Azure.Search in the search textbox

Azure

 

Download the Microsoft.Azure.Search package.

Next, we need to create the search result entity class, back to search service dashboard click on search explore, a query about any keyword, copy the JSON result.

Overview Of Azure Search

 

In Visual Studio create a new class, remove all auto generate code, from menu select edit, past special, past JSON as Classes,

Overview Of Azure Search

 

  1. public class Rootobject {  
  2.     public string odatacontext {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public SearchResault[] searchResault {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  
  11. public class SearchResault {  
  12.     public float searchscore {  
  13.         get;  
  14.         set;  
  15.     }  
  16.     public string content {  
  17.         get;  
  18.         set;  
  19.     }  
  20.     public int metadata_storage_size {  
  21.         get;  
  22.         set;  
  23.     }  
  24.     public DateTime metadata_storage_last_modified {  
  25.         get;  
  26.         set;  
  27.     }  
  28.     public string metadata_storage_name {  
  29.         get;  
  30.         set;  
  31.     }  
  32.     public string metadata_storage_path {  
  33.         get;  
  34.         set;  
  35.     }  
  36. }  

 

Under Controllers folder create homeController, then add the following code:

  1. private static SearchIndexClient CreateSearchIndexClient() {  
  2.     string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];  
  3.     string queryApiKey = ConfigurationManager.AppSettings["SearchServiceQueryApiKey"];  
  4.     SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, "azureblob-index1"new SearchCredentials(queryApiKey));  
  5.     return indexClient;  
  6. }  
  7. // GET: Home  
  8. public ActionResult Index() {  
  9.         return View();  
  10.     }  
  11.     [HttpPost]  
  12. public ActionResult Index(string key) {  
  13.     var r = CreateSearchIndexClient().Documents.Search < SearchResault > (key, parameters);  
  14.     return View(r.Results.ToList());  
  15. }  

 

Next, create index view and add the following code

  1. @model IList<Microsoft.Azure.Search.Models.SearchResult<AzureSearchWebApp.SearchResault>>  
  2. @{  
  3.    ViewBag.Title = "Index";  
  4. }  
  5. <h2>Index</h2>  
  6. @using (Html.BeginForm())  
  7. {  
  8.    <label>Search</label>  
  9.    <input type="text" name="key" value="" />  
  10.    <input type="submit" name="search" value="Search" />  
  11. }  
  12. <br />  
  13. @if (Model != null)  
  14.    {  
  15.    foreach (var item in Model)  
  16.       {  
  17.          <b>@item.Document.metadata_storage_name</b>  
  18.          <br />  
  19.          @item.Document.content  
  20.       <hr />  
  21.    }  
  22. }  

 

Next, open the Web.Config and add the following app settings

  1. <add key="SearchServiceName" value="lab02"/>  
  2. <add key="SearchServiceQueryApiKey" value="6A58C8E30AD9DB8F8BFFAAE0AC78B549"/> 

The Search Service Name,

Overview Of Azure Search

 

The Search Service Query Api Key,

Overview Of Azure Search

 

New, test the web app and verify the Azure search service result.

Overview Of Azure Search