Introduction to Azure Search

Azure Search is a fully-managed cloud search service that provides an easy-to-use set of APIs to add powerful search capabilities to any application. It supports full-text search, filtering, faceted navigation, geospatial search, and custom analyzers. Azure Search is built on top of Azure Cognitive Search which uses artificial intelligence and natural language processing to extract insights from unstructured data.

In this tutorial, we will cover the basics of getting started with Azure Search using the Azure Portal. We will create an index, load some data into it, and then demonstrate how to use the APIs to search the data.

Prerequisites

Before we get started, make sure you have:

  • An Azure subscription. If you don’t have one, create a free account.
  • Some sample data that you want to search.

Creating an Azure Search Service

To get started with Azure Search, the first step is to create a Search service in the Azure portal. Here are the steps:

  1. Log in to the Azure Portal.
  2. Click on Create a resource, search for Azure Search in the search bar, and select Azure Search service from the search results.
  3. Click on Create to start the creation process.
  4. Fill out the basic information for your Search service including the Subscription, Resource group, Name, Location, and Pricing tier. For this tutorial, we will use the Free pricing tier which is perfect for testing and development purposes.
  5. Click on Review + create and then Create to create your Search service.

It will take a few minutes for Azure to create your Search service. Once it is created, navigate to it in the Azure portal to continue.

Creating an Index

The next step in building a search solution with Azure Search is to create an index. An index is a schema that defines the structure of the data that you want to search. Here are the steps to create an index:

  1. In the Azure portal, navigate to your Azure Search service and click on Indexes in the left menu.
  2. Click on Add index to start creating a new index.
  3. In the Name field, enter a name for your index. For this tutorial, we will use the name hotels.
  4. In the Data source section, select the type of data source where you want to get data from. In this tutorial, we will use the Upload your documents and images option to upload sample data directly to the index.
  5. In the Field mapping section, we will define the names of the fields in our data source that will be indexed. Add the fields you want to include in your index. You can also choose the type of each field. For this tutorial, we will use the following fields:
    {
        "HotelId": "Edm.String",
        "HotelName": "Edm.String",
        "Description": "Edm.String",
        "Category": "Edm.String",
        "Tags": "Collection(Edm.String)",
        "Address": {
            "StreetAddress": "Edm.String",
            "City": "Edm.String",
            "State": "Edm.String",
            "PostalCode": "Edm.String",
            "Country": "Edm.String"
        },
        "Rooms": "Edm.Int32"
    }
    
  6. In the Searchable fields section, select the fields that will be included in full-text search. For this tutorial, we will select HotelName and Description.
  7. In the Sortable fields section, select the fields that will be used for sorting. For this tutorial, we will select HotelId and Category.
  8. Click on Create index to create your index.

Loading data into the Index

Now that we have created an index, the next step is to load some data into it. In this tutorial, we will use sample data stored in a JSON file named hotels.json. You can create your own sample data or download it from here.

To load data into the index, follow these steps:

  1. In the Azure portal, navigate to your Azure Search service and click on Indexes in the left menu.
  2. Click on the hotels index to open it.
  3. Click on the Upload data button to upload your data.
  4. In the Data format drop-down, select JSON Lines which is a format where each line is a JSON object.
  5. In the Document content section, upload the hotels.json file.
  6. In the Indexing behavior section, select Merge to merge the data with existing documents in the index if any.
  7. Click on Upload to start the upload process.

It will take a few minutes to upload and index all the data. Once the data is uploaded, you can see it in the Search explorer.

Searching the Index

Now that we have loaded data into our index, the next step is to search it using the Azure Search APIs. Azure Search provides a REST API that you can use to search the index and retrieve search results. Here are the steps to search an index:

  1. Open a browser and navigate to the Azure Search API documentation.
  2. Get the API key for your Azure Search service. You will use this key to authenticate your requests to the API.
  3. Copy the following sample query to search the hotels index for hotels in Seattle:
    https://[SEARCH SERVICE NAME].search.windows.net/indexes/hotels/docs?api-version=2020-06-30&search=Seattle&api-key=[YOUR API KEY]
    

    Replace the [SEARCH SERVICE NAME] and [YOUR API KEY] placeholders with your own values.

  4. Paste the query in a new browser window and press Enter.

  5. You should see a JSON response with search results that match your query.

Azure Search supports many more features like faceted navigation, geospatial search, autocomplete, and more which you can explore in the API documentation.

Conclusion

In this tutorial, we covered the basics of getting started with Azure Search. We created an Azure Search service, created an index, loaded some data into it, and demonstrated how to use the search API to search the data. Azure Search is a powerful tool that can help you add powerful search capabilities to any application. With its scalable and easy-to-use APIs, you can build search solutions that can handle millions of documents and provide lightning-fast search results to your users.

Related Post