How to Do a Search Query on Azure Search Service Programmatically using C#

Sathish Nadarajan
 
Solution Architect
November 20, 2018
 
Rate this article
 
Views
3054

Sometime back in the earlier article, we saw how to create an Azure Search Service, Datasource, indexer, Index etc., through the Azure Portal. In this article, let us extend the same and learn how to execute a Search Query on against a particular Index in the Azure Search Service Programmatically using C#.

Microsoft has given a bunch of SDKs and APIs to play with the Azure. i.e., technically, whatever we are able to do through the Console, should be done through coding as well.

In our code, we need to add the NuGet Package Microsoft.Azure.Search as below.

clip_image002

The Simple Console application below will do the search against the Azure Search Service Index.

 namespace Help.Console
 {
     using Microsoft.Azure.Search;
     using Microsoft.Azure.Search.Models;
 
     class Program
     {
         static void Main(string[] args)
         {
             string searchServiceName = "SearchSErviceName";
             string queryApiKey = "APIKey(Can be taken from the Portal)";
 
             SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, "18octindex", new SearchCredentials(queryApiKey));
 
             SearchParameters parameters =
                             new SearchParameters()
                             {
                                 Filter = "Filterable Column eq 'FilterValue'",
                                 Select = new[] { "SelectColumn1", "SelectColumn2" },
                                 Top = 1
                             };
 
             DocumentSearchResult results = indexClient.Documents.Search("*", parameters);
 
             
         }
     }
 }
 

Once, we get the results on the variable results, that can be processed accordingly. In my case, I wanted to get the content of the file. Hence, the below will work.

 var fileContent = Convert.ToString(searchResult.Results[0].Document["merged_content"]).ToLower();

As we saw, the search query execution is very simple and straight forward. The same kind of executions are possible using the APIs as well. This will be very much useful, for the client side development, where we don’t have a possibility to code in C#.

There is a detailed documentation on https://docs.microsoft.com/en-us/rest/api/searchservice/search-documents

Both GET and POST methods can be used for doing the search.

Courtesy : Microsoft Docs

When to use POST instead of GET

When you use HTTP GET to call the Search Documents API, you need to be aware that the length of the request URL cannot exceed 8 KB. This is usually enough for most applications. However, some applications produce very large queries or OData filter expressions. For these applications, using HTTP POST is a better choice because it allows larger filters and queries than GET. With POST, the number of terms or clauses in a query is the limiting factor, not the size of the raw query since the request size limit for POST is approximately 16 MB.

For further reading about the Search Parameters class, refer https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.search.models.searchparameters?view=azure-dotnet

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment