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:
Then click on create.
Open the search service, from dashboard click on import data.
Select the data source, then click Ok.
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.
Next, we need to create indexer, fill in the name and define the schedule, then click Ok.
From search, dashboard click on search explore to test the result
Type search keyword and check the results.
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,
Right click on the reference and select Manage NuGet Packages
Select browse and type Microsoft.Azure.Search in the search textbox
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.
In Visual Studio create a new class, remove all auto generate code, from menu select edit, past special, past JSON as Classes,
- public class Rootobject {
- public string odatacontext {
- get;
- set;
- }
- public SearchResault[] searchResault {
- get;
- set;
- }
- }
- public class SearchResault {
- public float searchscore {
- get;
- set;
- }
- public string content {
- get;
- set;
- }
- public int metadata_storage_size {
- get;
- set;
- }
- public DateTime metadata_storage_last_modified {
- get;
- set;
- }
- public string metadata_storage_name {
- get;
- set;
- }
- public string metadata_storage_path {
- get;
- set;
- }
- }
Under Controllers folder create homeController, then add the following code:
- private static SearchIndexClient CreateSearchIndexClient() {
- string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
- string queryApiKey = ConfigurationManager.AppSettings["SearchServiceQueryApiKey"];
- SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, "azureblob-index1", new SearchCredentials(queryApiKey));
- return indexClient;
- }
-
- public ActionResult Index() {
- return View();
- }
- [HttpPost]
- public ActionResult Index(string key) {
- var r = CreateSearchIndexClient().Documents.Search < SearchResault > (key, parameters);
- return View(r.Results.ToList());
- }
Next, create index view and add the following code
- @model IList<Microsoft.Azure.Search.Models.SearchResult<AzureSearchWebApp.SearchResault>>
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- @using (Html.BeginForm())
- {
- <label>Search</label>
- <input type="text" name="key" value="" />
- <input type="submit" name="search" value="Search" />
- }
- <br />
- @if (Model != null)
- {
- foreach (var item in Model)
- {
- <b>@item.Document.metadata_storage_name</b>
- <br />
- @item.Document.content
- <hr />
- }
- }
Next, open the Web.Config and add the following app settings
- <add key="SearchServiceName" value="lab02"/>
- <add key="SearchServiceQueryApiKey" value="6A58C8E30AD9DB8F8BFFAAE0AC78B549"/>
The Search Service Name,
The Search Service Query Api Key,
New, test the web app and verify the Azure search service result.