Using Azure Search for full-text search

Introduction

Azure Search is a cloud-based search-as-a-service solution offered by Microsoft Azure. It allows you to add scalable, full-text search capabilities to your applications, websites, and other data sources. With Azure Search’s powerful search algorithms, you can quickly find relevant data in large amounts of structured and unstructured data sources.

In this tutorial, we’ll demonstrate how to use Azure Search for full-text search purposes. We’ll set up an Azure Search service, create an index, and populate it with data. Then, we’ll build a search interface that retrieves the data using the Azure Search REST API.

Prerequisites

  • An Azure account with an active subscription.
  • A basic understanding of REST APIs, JSON, and C#.
  • Visual Studio 2019 or later installed on your machine.

Create an Azure Search Service

Before we start, we need to create an Azure Search service in the Azure portal. Follow these steps:

  1. Log into your Azure portal account.
  2. Click on the Create a resource button on the left-hand side navigation menu.
  3. Search for Azure Search in the search bar and select the Azure Search service in the results.
  4. Click Create to start the wizard for creating an Azure Search service.
  5. Enter your subscription, resource group, and search service information, such as name and location.
  6. Go through the remaining steps and click Create to create your Azure Search service.

Once your Azure Search service is created, you can access it from the Azure portal dashboard.

Create an index

An index is a collection of documents or data sources that we want to search. Azure Search allows us to create an index based on our unique requirements. Here’s how to create an index:

  1. In the Azure portal, go to your newly created Azure Search service.
  2. Click on the Indexes option on the left-hand side menu.
  3. Click on the Add Index button to start creating an index.
  4. Enter the index name, select the data source you want to use for the index, and choose the fields you want to include in the index.
  5. Click on the Create button to create your index.

After creating the index, you need to populate it with data.

Populate the Index with Data

We need to add data to the index so that we can search for it. Azure Search offers several ways of adding data to an index, such as:

  • Indexing data from an Azure SQL database
  • Using the Azure Search REST API to push data to the index
  • Indexing data from Azure Blob Storage
  • Indexing data from Azure Table Storage
  • And many more…

In this tutorial, we’ll use the Azure Search REST API to populate our index. Here’s how we can add data to our index:

  1. Create a new C# console application project in Visual Studio.
  2. Add the Newtonsoft.Json NuGet package to the project.
  3. Add the following code snippet to your Main method:
string serviceName = "<your-search-service-name>";
string apiKey = "<your-search-service-admin-key>";
string indexName = "<your-index-name>";

string url = $"https://{serviceName}.search.windows.net/indexes/{indexName}/docs/index?api-version=2020-06-30";

string jsonBody = @"
    {
        'value': [
            {
                'id': '1',
                'title': 'C# for Beginners',
                'description': 'A guide to learn C# from scratch'
            },
            {
                'id': '2',
                'title': 'ASP.NET Core MVC',
                'description': 'A beginner-friendly guide to building web applications using ASP.NET Core MVC'
            },
            {
                'id': '3',
                'title': 'Azure Fundamentals',
                'description': 'A beginner-friendly guide to the azure fundamentals certification'
            }
        ]
    }
";

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("api-key", apiKey);

    var httpContent = new StringCo

ntent(jsonBody, Encoding.UTF8, "application/json");

    using (var response = await client.PostAsync(url, httpContent))
    {
        var responseString = await re<div>
<script async="async" data-cfasync="false" src="//pl19778731.highrevenuegate.com/602f7c8f28b3e110025d9ff64d760d

09/invoke.js"></script>
<div id="container-602f7c8f28b3e110025d9ff64d760d09"></div>
</div>sponse.Content.ReadAsStringAsync();
        Console.WriteLine(responseString);
    }
}

Note: Be sure to replace the <your-search-service-name>, <your-search-service-admin-key>, and <your-index-name> placeholders with the actual values for your Azure Search service.

This code snippet creates a new HttpClient, adds the Azure Search API Key to its headers, and makes a POST request to the REST API endpoint to upload the data to our index. The body of the request is a JSON object that contains an array of documents we want to add to the index.

Search for Data

With our Azure Search service and index set up, and data added to the index, we can start searching for data. We can use the Azure Search REST API to programmatically search for data, or we can use the search box widget that Azure Search provides.

In this tutorial, we’ll use the Azure Search REST API to search for data. Here’s how to search for data using the REST API:

  1. Add the following code snippet to your console application’s Main method to search for data:
string serviceName = "<your-search-service-name>";
string apiKey = "<your-search-service-query-key>";
string indexName = "<your-index-name>";
string searchText = "guide";

// Specify the search fields and fields to return
string fields = "title, description";
string select = "id, title";

string url = $"https://{serviceName}.search.windows.net/indexes/{indexName}/docs?search={searchText}&searchFields={fields}&$select={select}&api-version=2020-06-30";

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("api-key", apiKey);

    using (var response = await client.GetAsync(url))
    {
        var responseString = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseString);
    }
}

Note: Be sure to replace the <your-search-service-name>, <your-search-service-query-key>, and <your-index-name> placeholders with the actual values for your Azure Search service.

This code snippet constructs a URL to the REST API endpoint with your search query parameters, issues a GET request using an HttpClient object, and handles the response.

Conclusion

Searching for data is an essential part of modern applications, and Azure Search is an excellent choice to add robust and scalable full-text search capabilities. In this tutorial, we’ve demonstrated how to set up an Azure Search service, create an index, populate it with data, and search for that data using the Azure Search REST API. With this knowledge, you’ll be well on your way to adding full-text search to your applications using Azure Search.

Related Post