{"id":3987,"date":"2023-11-04T23:13:58","date_gmt":"2023-11-04T23:13:58","guid":{"rendered":"http:\/\/localhost:10003\/using-azure-search-for-full-text-search\/"},"modified":"2023-11-05T05:48:25","modified_gmt":"2023-11-05T05:48:25","slug":"using-azure-search-for-full-text-search","status":"publish","type":"post","link":"http:\/\/localhost:10003\/using-azure-search-for-full-text-search\/","title":{"rendered":"Using Azure Search for full-text search"},"content":{"rendered":"
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.<\/p>\n
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.<\/p>\n
Before we start, we need to create an Azure Search service in the Azure portal. Follow these steps:<\/p>\n
Once your Azure Search service is created, you can access it from the Azure portal dashboard.<\/p>\n
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:<\/p>\n
After creating the index, you need to populate it with data.<\/p>\n
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:<\/p>\n
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:<\/p>\n
string serviceName = \"<your-search-service-name>\";\nstring apiKey = \"<your-search-service-admin-key>\";\nstring indexName = \"<your-index-name>\";\n\nstring url = $\"https:\/\/{serviceName}.search.windows.net\/indexes\/{indexName}\/docs\/index?api-version=2020-06-30\";\n\nstring jsonBody = @\"\n {\n 'value': [\n {\n 'id': '1',\n 'title': 'C# for Beginners',\n 'description': 'A guide to learn C# from scratch'\n },\n {\n 'id': '2',\n 'title': 'ASP.NET Core MVC',\n 'description': 'A beginner-friendly guide to building web applications using ASP.NET Core MVC'\n },\n {\n 'id': '3',\n 'title': 'Azure Fundamentals',\n 'description': 'A beginner-friendly guide to the azure fundamentals certification'\n }\n ]\n }\n\";\n\nusing (var client = new HttpClient())\n{\n client.DefaultRequestHeaders.Add(\"api-key\", apiKey);\n\n var httpContent = new StringCo\n\nntent(jsonBody, Encoding.UTF8, \"application\/json\");\n\n using (var response = await client.PostAsync(url, httpContent))\n {\n var responseString = await re<div>\n<script async=\"async\" data-cfasync=\"false\" src=\"\/\/pl19778731.highrevenuegate.com\/602f7c8f28b3e110025d9ff64d760d\n\n09\/invoke.js\"><\/script>\n<div id=\"container-602f7c8f28b3e110025d9ff64d760d09\"><\/div>\n<\/div>sponse.Content.ReadAsStringAsync();\n Console.WriteLine(responseString);\n }\n}\n<\/code><\/pre>\nNote: Be sure to replace the <your-search-service-name><\/code>, <your-search-service-admin-key><\/code>, and <your-index-name><\/code> placeholders with the actual values for your Azure Search service.<\/p>\nThis code snippet creates a new HttpClient<\/code>, adds the Azure Search API Key to its headers, and makes a POST<\/code> 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.<\/p>\nSearch for Data<\/h1>\n
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.<\/p>\n
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:<\/p>\n
\n- Add the following code snippet to your console application’s Main method to search for data:<\/li>\n<\/ol>\n
string serviceName = \"<your-search-service-name>\";\nstring apiKey = \"<your-search-service-query-key>\";\nstring indexName = \"<your-index-name>\";\nstring searchText = \"guide\";\n\n\/\/ Specify the search fields and fields to return\nstring fields = \"title, description\";\nstring select = \"id, title\";\n\nstring url = $\"https:\/\/{serviceName}.search.windows.net\/indexes\/{indexName}\/docs?search={searchText}&searchFields={fields}&$select={select}&api-version=2020-06-30\";\n\nusing (var client = new HttpClient())\n{\n client.DefaultRequestHeaders.Add(\"api-key\", apiKey);\n\n using (var response = await client.GetAsync(url))\n {\n var responseString = await response.Content.ReadAsStringAsync();\n Console.WriteLine(responseString);\n }\n}\n<\/code><\/pre>\nNote: Be sure to replace the <your-search-service-name><\/code>, <your-search-service-query-key><\/code>, and <your-index-name><\/code> placeholders with the actual values for your Azure Search service.<\/p>\nThis code snippet constructs a URL to the REST API endpoint with your search query parameters, issues a GET request using an HttpClient<\/code> object, and handles the response.<\/p>\nConclusion<\/h1>\n
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.<\/p>\n","protected":false},"excerpt":{"rendered":"
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. Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[394,708,707,705,355,212,706,385,709,386],"yoast_head":"\nUsing Azure Search for full-text search - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n