Back to List

How to Use Azure Search Services

Steven Nelson Steven Nelson  |  
Apr 10, 2018
 
Azure has a unique service offering aptly named “Azure Search”. This is a search-as-a-service cloud solution that lets you add a rich search service to your apps.
 
The search service abstracts the complexities of document retrieval through both a REST API and a .NET SDK. Imagine, if you will, the search engines of Google, Bing, Yahoo, etc. The Azure search service provides many of the same benefits and ease-of-use that these major search engines do. In addition to the API surface, the Azure portal contains all the management and administration you will need to control the indexes over your data.
 

Connect to any data sources and index out of your database

One of the cool features of the Azure Search service is that it can connect to any of your data sources and index data directly out of your database. The search service can connect to a view in your database, pull the results from the view, and create searchable documents out of the results. The data source can be an Azure SQL database, a SQL server on an azure VM, a Cosmos DB, Azure blob or Azure table storage.
 

Control field definitions

You can control the definition of the fields in the document. You can decide which fields you want to search over, which data type the field is (text, number, bool, timestamp, geography, or collections), which fields you want to use as sortable, and so on.
 

Built-in Search Explorer

There is a Search Explorer tool built into the Azure portal you can use to inspect the documents and verify that your search process worked correctly. The Search Explorer is accessible directly from the Azure portal within your search service.
 
Another side-benefit of this being an Azure service is that there is built-in support for DevOps PowerShell scripts that can be used for automation and deployment of your search services.  In a DevOps script you can create the service, define the index, connect to your database, and run the indexer from PowerShell. This makes it very easy to add to existing automated deployment processes.
 

Let’s dive in and look around

I’m going to create a search index of restaurants. The restaurant data contains the name and address of the restaurant, along with GPS latitude and longitude coordinates for mapping.
 
Once you find the “Search Services” on the Azure portal, it’s easy to create a new service by simply giving it a name and assigning it to a resource group (all standard stuff, and the same as all other Azure services).  
 
The magic comes from the Import Data wizard on the Search Service blade. Here is a screenshot:
 
import data wizard
 
In this wizard, you can specify the data source to connect to your data. In my case, I’m going to connect to a SQL Azure database. After you select the data source, it will allow you to pick a table, or view from the database to use for the search index.
 
sql azure database
 
Now the search service connects to the database and reads the schema from the table or view. In my case, I’m going to use a view because it aggregates different tables to select the columns I want to use in the search index.
 
The next screen is Customize Target Index. This is where you define the fields you want exposed from the search index and define what the fields can do. Here is what mine looks like:
 
custom target index
 
I’ve selected all fields from my view as Retrievable. I’ve set the Name, City, County and PostalCode fields as searchable. The Name and PostalCode are also marked as sortable so I can allow the results to be ordered by either of these fields.
 
With this configuration, I have a search index where the user can find restaurants by name, postal code, city, or county. With search criteria of “apple”, this will look for restaurants with a name that contains the word “apple”, a city or county that contains the word “apple”, or a zip code as well (but that won’t have results because the zip codes are standard U.S. numeric zip codes).
 
The final step, called Import Your Data, lets you define an indexer that can run periodically and update the documents in the index. The indexer can be run on a schedule, on-demand, or via code. When the indexer runs, it will connect to your database and update the documents in your search index.
 

Using the Azure Search SDK in .NET

From the .NET side, when you want to run queries against your search index to retrieve documents, you use the Nuget package Microsoft.Azure.Search (version 3.0.5 is current at this time).
 
With that package dependency installed, you use the SearchIndexClient object to form queries to fetch documents. Here is an example of what the code looks like:
 
search index client
 
Line 31 is where the search is performed using whatever searchString is provided. This will have the effect of querying the search index for any restaurant where the name, city, county or postal code contains the search string. It will match if the search string is anywhere in the field. If I search on the searchTerm of ‘taco’, then I get the following results:
 
search string term
 
Notice that most of the hits are on the Name field, but there is one ‘Hamburger Hut’ in the city of ‘Taconite’. Notice that the city of ‘Taconite’ contains the phrase ‘taco’ in it. Nice!
 

Conclusion

This is just the tip of the iceberg on the Azure Search Service. There is an extensive query language you can use, as well as a REST API to execute queries if .NET isn’t your thing. 
 
Keep in mind that if the data is changing, then the indexer needs to be run so that changes are reflected in the index. The indexer can be run on a schedule, be invoked programmatically, run via PowerShell, or run manually from the Azure portal.
 
Now, go visit Taconite, Minnesota to find the fictional Hamburger Hut and create your own Azure Search Service!
 

Where to Learn More

General info on Azure Search:
https://docs.microsoft.com/en-us/azure/search/
https://docs.microsoft.com/en-us/dotnet/api/?term=microsoft.azure.search
 
Azure Search Explorer:
https://docs.microsoft.com/lt-lt/azure/search/search-explorer
 
How to get started with Azure PowerShell:
https://docs.microsoft.com/en-us/powershell/azure/overview?view=azurermps-5.5.0
https://docs.microsoft.com/en-us/powershell/azure/install-azurerm-ps?view=azurermps-5.5.0
 
Azure.Net Programming

 

Love our Blogs?

Sign up to get notified of new Skyline posts.

 


Related Content


Blog Article
Machine Monitoring IoT Solution with Azure Services and Power BI
Eric SaltzmannEric Saltzmann  |  
Jun 11, 2019
We often hear organizations ask how they can drive more insights out of their connected devices. Though the Internet of Things (IoT) has been a buzzword for the last few years, many organizations are still struggling through the headache of implementing an IoT pilot or solution. Most of the...
Blog Article
Azure Tips & Tricks: Application Insights Snapshot Debugger
Todd TaylorTodd Taylor  |  
May 21, 2019
A painful memory that is burned into my developer-brain is a production support issue for a .NET web API that I wrote years ago. I worked for a large retailer at the time, and the bug was preventing electronic pricing signs from displaying the most up-to-date price for hundreds of products at...
Blog Article
Azure Tips & Tricks: Moving Operations in API Management
Todd TaylorTodd Taylor  |  
May 07, 2019
Azure API Management (APIM) helps developers save a lot of time by doing most of the heavy lifting involved in creating an API gateway and developer portal. However, the APIM administrative UI is missing a few minor times-saving features, such as the ability to move API operations between APIs...
Blog Article
Creating and Installing Project Templates in .NET Core
Ben BuhrBen Buhr  |  
Apr 30, 2019
In my previous blog article, we examined the .NET Core Command Line Interface (CLI). As part of that, we saw that templates in .NET Core can be very useful. Templates in .NET Core are very easy to create, and there already are a ton of very helpful ones available. They allow us to quickly get an...
Blog Article
How to Implement a Cloud-based Business Continuity Plan
Skyline Technologies  |  
Apr 23, 2019
First and foremost, you need to clarify your definitions. Make sure your team, your IT team, your business leaders, and your organization understand the definitions of RPO and RTO. Also make sure they understand how those impact their business and their business continuity plan.   Case...