In this article we will see how to do searching in Azure portal and search implementation through Visual Studio.
Before starting I assume you have knowledge about how to create a search service, and how to create an index on the service. And if you want to know more read my previous articles:
Part-1,
Part-2
Searching in the Azure Portal
In my previous article I created Azure search service and an index on it using import data.
Search explorer -> enter query string to search -> Click Search -> Results shown below:
Query String – enter the string you want to search.
The text to search for All searchable fields are searched by default unless searchFields is specified.
To match any term, use * (this can be useful for Boolean filter queries). To read more about simple query syntax read msdn here
You can read more about filters using MSDN link here
NOTE
String comparisons are case sensitive [when you specify a filter]
For example - when we use:
then we will get the exact result:
BUT,
If we replace ‘Matthew’ with ‘matthew’ the result will be as shown:
So string comparisons are case sensitive. [when you specify a filter]
Implementation of Searching using Visual studio
STEP 1: Create empty MVC web application
Open Visual studio
Click on File-> new-> Project
Select asp.net web application, enter project name as “searchingIndex” and click ok,
Choose empty MVC application and click ok:
Empty MVC application is created.
STEP 2 - Install Microsoft.Azure.search using NuGet Package manager
Right click on references and select Manage NuGet Pakages
OR,
Go to Tools -> NuGet Pakage Manager -> Manage NuGet Package for Solution
Search for Microsoft.azure.search
Click on Install,
Microsoft.Azure.Search is installed successfully as shown.
STEP 3 - Add Reference for System.Net.Http
Right click on references -> add reference
Search System.Net.Http and click ok
Reference added successfully in solution.
STEP 4 - Add a new empty controller
Right click on Controller folder -> click on Add -> Select Controller as shown.
Select MVC 5 controller - Empty
Name the controller as “HomeController” and click on Add.
Controller created as shown
STEP 5 - Declare Service Name
Define a variable to hold service name as we created in the Azure portal.
-
- var sSearchService = "stestsearchservice";
The service name is the host portion of search service URL in Azure portal
STEP 6 - Declare apiKey
As specified on MSDN about api-Key
So under Azure search service -> click on keys – Copy Primary admin key
Use that apiKey here in the code.
- public ActionResult Index() {
-
- var sSearchService = "stestsearchservice";
-
- var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";
-
- var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));
-
- var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");
-
- SearchParameters sp = new SearchParameters() {
- SearchMode = SearchMode.All
- };
-
- var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);
- return Json(sDocs.Results, JsonRequestBehavior.AllowGet);
- }
STEP 7 - Search Service Client
Define the Search client [ import Microsoft.Azure.Search namespace]
Pass the ‘searchServiceName’ and ‘searchCredentials’ to searchServiceClient class as shown.
- public ActionResult Index() {
-
- var sSearchService = "stestsearchservice";
-
- var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";
-
- var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));
-
- var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");
-
- SearchParameters sp = new SearchParameters() {
- SearchMode = SearchMode.All
- };
-
- var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);
- return Json(sDocs.Results, JsonRequestBehavior.AllowGet);
- }
Inside SearchCredentials we need to specify apiKey which is used to authenticate Azure Search service,
STEP 8 - Index Client
Now we are going to get index using searchSearvieclient and the using that index we will get Index client.
Index client is used to manage documents and query inside a specified index.
- public ActionResult Index() {
-
- var sSearchService = "stestsearchservice";
-
- var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";
-
- var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));
-
- var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");
-
- SearchParameters sp = new SearchParameters() {
- SearchMode = SearchMode.All
- };
-
- var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);
- return Json(sDocs.Results, JsonRequestBehavior.AllowGet);
- }
STEP 9 - Define Search Parameters
There are two types of search modes,
The following offers a good explanation:
To read more about operators in simple query syntax read here,
Specify the string you want to search
- public ActionResult Index()
- {
-
- var sSearchService = "stestsearchservice";
-
- var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";
-
- var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));
-
- var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");
-
- SearchParameters sp = new SearchParameters() {
- SearchMode = SearchMode.All
- };
-
- var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);
- return Json(sDocs.Results, JsonRequestBehavior.AllowGet);
- }
Instead of returning view, I’ll return Json:
- public ActionResult Index() {
-
- var sSearchService = "stestsearchservice";
-
- var sApiKey = "ADAFF3B1E329E4E1181DC88C23D49A37";
-
- var sSearchClient = new SearchServiceClient(sSearchService, new SearchCredentials(sApiKey));
-
- var sIndexClient = sSearchClient.Indexes.GetClient("azuresql-index");
-
- SearchParameters sp = new SearchParameters() {
- SearchMode = SearchMode.All
- };
-
- var sDocs = sIndexClient.Documents.Search("Affordable Sports Equipment", sp);
- return Json(sDocs.Results, JsonRequestBehavior.AllowGet);
- }
Run the solution, and we will get the search result in json as we saw in the portal.
Summary
In this article we saw,
- How to search in Azure portal using simple query string
- How to create a Visual Studio solution for searching
In the next article I’ll show you how to use $filter in searching the documents.
Keep reading!!!