Azure Search With Step By Step Demo - Part Two

In this article, I will conclude the demo by showing how we can query the records or documents in the index of an Azure Search Service. I will show you how we can do it in both the portal and in a basic Visual Studio application.
  1. var indexClient = searchClient.Indexes.GetClient("ktawdb-index");    
You will have to go through the first part of this article in order to follow up with the demo. You can find the first part from the link below.
Now, let us continue ahead.
 
Step 1

Click on the indexer name.
 
indexer name 
 
Step 2

Click on Search Explorer.
 
indexer name 
 
Step 3

There is a request URL and you can use this in the application. We are going to search for some queries in a C# application using the SDK. You can actually use a request URL like this with a search equal to star and have all the documents retrieved.
 
indexer name 
 
Step 4

Click on search and they will be retrieved below in the results window.
 
indexer name 
 
Step 5

Type a query string $filter=FirstName eq 'David'  and click on search. You will see the results with FirstName as David. And, that will be added to the request URL as well.
 
indexer name 
 
Step 6

You can also order the documents by a query string like $orderby=ModifiedDate desc and it will order the data in the descending order of the date that they were modified.
 
indexer name 
 
Step 7

Create a new project in Visual Studio, an ASP.NET Web Application.
 
indexer name 
 
Step 8

Select an Empty template with MVC folder references.
 
indexer name 
 
Step 9

Right click on the project name and click on Manage NuGet Packages.
 
indexer name 
 
Step 10

Go to browse, search for Microsoft.Azure.Search, and install the package.
 
indexer name
 
Step 11

Right click on References and click on Add Reference.
 
indexer name 
 
Step 12

Tick System.Net.Http and click on OK.
 
indexer name 
 
Step 13

Right click on Controller and add a new controller.
 
indexer name 
 
Step 14

Choose the MVC5 Empty Controller and click on Add.
 
indexer name 
 
Step 15

Name it as HomeController.
 
 indexer name
 
Step 16

In the portal, go to the keys tab in the search service and copy the primary admin key.
 
indexer name
 
Step 17

Back in the HomeController, create two variables for the host portion. First one would be the name of the search service and the second one would be the API key that you just copied from the portal.
 
indexer name 
  1. var searchServiceName = "ktsearch";  
  2. var apikey = YOUR_OWN_API_KEY_COPIED_FROM_PORTAL   
Step 18

To set up the search client, import the Azure Search namespace. Create a new variable searchClient where we pass in the searchServiceName and for the credentials, the apiKey.
 
indexer name
  1. using Microsoft.Azure.Search;  
  2.   
  3. var searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));   
Step 19

We have our searchClient. Now, for the indexClient, we'll do searchClient.Indexes.GetClient("IndexName").
 
indexer name 
  1. var indexClient = searchClient.Indexes.GetClient("ktawdb-index");  
Step 20

Import Microsoft.Azure.Search.Models to define the Search Parameters below. We have kept search mode as well so it will look out for the query in all the fields.
 
indexer name
  1. using Microsoft.Azure.Search.Models;  
  2.   
  3. SearchParameters sp = new SearchParameters() { SearchMode = SearchMode.All};  
Step 21

We are implementing this thing in code. Just search through a query string. Here I have searched 'A Bike Store' and it has looked for that data in every field. We are going to do the same thing with the code.
 
indexer name 
 
Step 22

This line of code grabs our collection of documents. We have passed in the search criteria and the search parameters. The result will come back in a JSON format and then that could be returned back to the client. In this case, this is just a basic web application so we'll just return the JSON data to the browser.
 
indexer name 
  1. var docs = indexClient.Documents.Search("A Bike Store", sp);   
In a real-time application, there would be a front end for the user of the application, there would have been a search form and things like that.
Step 23

Instead of View, we'll return JSON here. Pass the docs.Results and set the JsonRequestBehavior to AllowGet. We are testing just the basics here so all this is fine.
 
indexer name 
  1. return Json(docs.Results, JsonRequestBehavior.AllowGet);   
Microsoft has provided the demo code for the job site Web Application on GitHub that you can use and implement in your own applications as well.

Now, for the final step, let us run this application and check the output.

Step 24

The browser would display the same JSON documents successfully just like the ones that you saw in the portal.
 
 indexer name
With this, I conclude this article on Azure Search service. 


Similar Articles