🔥 Make sure you star the repo to keep up to date with new tips and tricks.
💡 Learn more : Cognitive Search - Azure Search with AI.
📺 Watch the video : How to make your data searchable with Azure Search and AI
If your app exposes any data to users, you probably need a search feature. And search is traditionally difficult to implement. Users have high expectations of a search feature and expect it to work the same in your app as it works in Bing or Google. If you would have to build a Bing-like search feature, with autocomplete, filtering, sorting and all the other bells and whistles, you would have to spend a lot of time and effort. Luckily, Azure Search provides an easy way to incorporate a search feature into your app. And Azure Search can be enhanced by Azure Cognitive Services to provide AI capabilities.
In this post, we'll create a simple application with Azure Search that uses AI.
If you want to follow along, you'll need the following:
We'll create an Azure Search instance and a Cognitive Service in Azure that will analyze a sample data source. We will start by creating the Azure Search instance in the Azure portal:
(Create Azure Search blade in the Azure portal)
When the Azure Search instance is created, we can create a search index in it, based on sample data:
(The import data button in the Azure portal)
The first step of the wizard is to select a data source to perform search on. In the Data Source dropdown box, select Samples and select realestate-us-sample a. This sample data source represents a SQL Database containing example data for real-estate in the United States. Here are some more sample data sources that you can use with Azure Search
Next, we will add cognitive search. This enhances Azure Search by adding AI capabilities from Azure Cognitive Services a. First, attach a Cognitive Service by selecting an existing one. If you don't have one yet, click the Create new Cognitive Services resource to create one and click Refresh once you've created it b. In the next step, Add enrichments, we can create a skillset that enhances the search index:
(Configure Cognitive search in the Azure portal)
c. Click Next: Customize target index
In the step to customize the target index, you can tell Azure Search which fields of the data you want to be searchable, which ones you want to sortable, which ones you want to use to filter on and so on. Leave everything as it is and click Next: Create an indexer
(Customize the search index in the Azure portal)
The index that we've created uses Azure Cognitive Services to analyze the description text of the data. We've chosen to split that data up into sentences, so that the Cognitive Service can deal with it better. This split does mean that it will take 15 minutes or more to create the search index. In a production scenario, this doesn't matter a lot, because Azure Search would run on multiple replicas an you would update the index of just one of them, leaving the rest available to be queried.
When the index is created, we can test it:
(Search results in the Azure portal)
Having the search index ready to be queried is great. But we really want an application to call the Azure Search API to use the search index. There are many ways to implement this including the Azure Search SDK for .NET and calling the REST API directly. We can also use helper libraries to generate a search UI for us. Let's try that:
Fill in the Query Key value. You can get this by either using a read/write Key or generating a Query-only Key in the Keys menu of the Azure Search instance
Go to the Azure Search instance in the Azure portal and open the search index that we've created earlier. Click on the CORS tab. We need to allow the UI that will be generated to access the search index a. Select All for the Allowed origin type b. Click Save to save the CORS setting
(CORS setting in the Azure portal)
Still in the search index in the Azure portal, click on the Index Definition (JSON) tab a. Copy all the JSON content b. Go to the UI search generator website and paste the JSON content into the Azure Search index JSON field
In the UI search generator website, fill in the name of the Azure Search instance in the Service Name field
Click Generate App. This will generate a HTML file
Open the generated HTML file. This is a UI that talks to your search index and uses common UI elements to enhance your search experience. Search for medina to see how the UI behaves. You can use this HTML file to integrate the search experience into your app. You can also make it a lot more appealing by tweaking it slightly. Take a look at this sample website to see an enhanced example
(Search results in the generated HTML file)