Azure Search With Step By Step Demo - Part One

One of the most important components of a modern web application is the ability to have a great search experience. Building it from scratch might be tedious so Microsoft has come up with Azure Search service. There is a demo web application created by Microsoft which you can see here.
 
Its source code is available on GitHub and this sample application implements a lot of interesting things. You can search up at the top side and you will get search results and suggestions for information, course, spelling mistakes and content as well. The information is also faceted and has categories as well.
 
Azure Search 
  • On the right hand side of the application, we have some geospatial information coming back as a part of the search so if we want to include maps in our search, we can implement that as well.
  • We also have ranking of the search results and also pagination.
  • We also have preview for some of the search items below which is called hit highlight.
  • You can build your own application around this service and build the user interface on your own.
  • You can have data from multiple sources in your indexes and it gives the ability to do a much more robust search system in your applications.

How does it work?

Azure Search 
  • Firstly, you are going to create an instance of the service which is basically creating a resource in the portal.
  • Then you are going to set up an index that your application can query to return search results to end results.
  • It works on a push-pull mechanism. So you can push information into the index or you can pull it.
  • You have got native support for pulling information from Cosmos DB, SQL Server Database, Azure SQL Database or even data that is stored in Azure Storage, particularly data in table storage.
  • Eventually everything is going to be JSON documents in the index so you just have to be able to push the data into that.
  • There are command line tools and APIs and SDKs that help you to do that.
  • This is the basic idea behind the Azure Search. Let us go ahead and implement that.
Step 1

Create a new SQL Database and configure it as below. Give name. Create a new resource group. Select source as Sample AdventureWorksLT and configure a new server.
 
Azure Search 
 
Step 2

Select pricing tier as basic.
 
pricing tier 
 
Step 3

Click on Set Server Firewall.
 
Set Server Firewall
 
Step 4

Click on Add Client IP and your IP address will be added. Click on Save.
 
Add Client IP and your IP address 
Step 5

Now, go to the Database overview and copy the server name.
 
Azure Search - server name
 
Step 6

Open Visual Studio and go to server explorer. Right click on Data Connections and click on Add Connection.
 
 Add Connection
 
Step 7

Paste the server name. Select SQL Server Authentication. Enter the username and password. Selected the database as AWDB.
 
SQL Server Authentication 
 
Step 8

Click on Test Connection. Once the test connection gets succeeded, click on OK.
 
Azure Search - test connection 
 
Step 9

Once it gets connected, you will be able to see all the tables that the database has. Later in the demo, we are going to use the Customer table.
 
Azure Search - Customer table 
 
Step 10

To view the table data, go to Cloud explorer and find your database name under your subscription name. Click on database name and click on Open SQL Server Object Explorer.
 
SQL Server Object Explorer 
 
Step 11

It will open up a new window. Your server name, authentication, username will be already there. Type in the password and change the database as AWDB and not something like ktazuresearch/AWDB. Then click on connect.
 
Azure Search 
 
Step 12

In the SQL Server Object Explorer, find your server name, click on the Database name, go to tables --> External Tables and you will see the list of all the tables. Right click on the Customer table and click on view data to view the table data.
 
SQL Server Object Explorer 
 
Step 13

We have got multiple fields in this table such as CustomerID, FirstName, MiddleName, LastName, Company Name and so on.
 
multiple fields in this table
    • So next we are going to build an Azure Search instance, we'll configure an index to pull this data as JSON documents into that index and then we can use it from our own application.

    • You can start using the Azure Search service for free. If you need to go outside of the multitenant environment and need more scalability, you can go with a paid tier.
Step 14

Click on Create a resource and go to web category. Find Azure Search and click on it.
 
Azure Search 
 
Step 15

Give some unique URL name, select the subscription, put in the same resource group as the database, select the location and select the Basic pricing tier. Click on create.
 
Basic pricing tier 
 
Step 16

We don't have any indexes yet but we have got our primary and secondary keys in the keys tab. We will use those keys to authenticate programmatically to this service and we will use the public URL to do that in code.
 
primary and secondary keys
 
Step 17

If you ever need to scale the pricing tier, you can do that from the Scale tab on the left. We have got the ability to scale three replicas. Next, we will import the data and create our first index.
 
Azure Search - first index
 
Step 18

There are multiple ways to get data into an index. You can click on Add index to build the index and then push the data into that index or pull it with an indexer. The other way, which we are going to do is by importing the data from a database. So, click on Import Data.
 
Azure Search - Import Data
 
Step 19

For the data source, there are multiple options to choose from. Samples, Cosmos DB, Azure Table and Blob Storage and so on. We are going to choose the Azure SQL Database.
 
Azure SQL Database 
 
Step 20

Give a unique name. Select the database. User ID will be added automatically. Input the password and test the connection.
 
Azure Search database 
 
Step 21

Then, select the table and click on OK.
 
Azure Search - table  
 
Step 22

Then you will be able to see the Cognitive Skills options but it is only available in the South Central US and West Europe regions (at the time of performing this demo). It won't be available for us since we have put our resource in the Southeast Asia region. By enabling this, you can add cognitive search skills as well. Click on OK.
 
Cognitive Skills options 
 
Step 23

In the target index, give index name. Then keep the below-mentioned fields only and delete the rest of the others by right clicking on them and delete. Keep FirstName, LastName, CompanyName, EmailAddress, Phone and ModifiedDate.
 
Azure Search - index 
 
Step 24

Click on the checkboxes above because we want to have the fields and data be retrievable, filter-able, sortable and searchable. We can also facet our data by turning on the facet-able but we are not going to do that here. Click on OK.
 
Azure Search - checkboxes 
 
Step 25

Next up in creating the indexer, give it some unique name. Keep the schedule as hourly.
 
Azure Search - creating the indexer 
 
Step 26

The high watermark column will keep your index up to date with your database. So if you are modifying the database, this is how you are going to update your index. Select ModifiedDate as the column because that will represent in the database anytime we update the record and then it will easily reflect here in the index as well. The same thing goes with Track Deletions but in this example we are not going to implement it. Click on OK.
 
Azure Search - high watermark column 
 
Step 27

We have configured everything here so click on OK.
 
Azure Search 
 
Step 28

You will find your index in the list of indexes. You can wait until it automatically syncs the documents or you can manually do that as well.
 
index in the list of indexes
 
Step 29

Scroll down and click on Indexers.
 
Azure Search Indexers
 
Step 30

You will see the number of docs succeeded. Click on the indexer.
 
indexer 
 
Step 31

You can click on Run to manually sync the documents/data as well.
 
manually sync
 
Step 32

Refresh the overview page and you will be able to see the number of documents and the storage size as well.
 
storage size
 
In the next part of the article, we will see how we can query the records or documents in the index. We will do that both in the portal and in a basic Visual Studio application. 


Similar Articles