Up to this point in our examples, we have created an account and search index on Azure. We have also written an uploader that leverages the BlackBarLabs.Search.Azure library to import information about 982 airports from across the southeastern United States.
In this post, we will now begin to explore ways that we can search the data that we have in our search index. We will modify our ASP.NET sample project to provide a simple search box that will initiate a text search and will provide results to the user. Please keep in mind that we will not be spending any time building a beautiful UI. This is a series of posts to demonstrate how to quickly get the Azure Search service to work for you, so we will keep our focus there.
Before we get started with code, let’s add a reference to the BlackBarLabs.Search.Azure project. To do this, right-click on the References node under AFDSearch and click Add Reference… In the dialog that pops up, click on the Projects node on the left and then select BlackBarLabs.Search.Azure from the list and click OK.
Let’s now go set up our models. To do so, right-click on the Models folder in the AFDSearch project and select Add\New item… Select a C# class. Name the class AirportSearch and click Add.
Now add this code to the AirportSearch.cs file:
This model contains the SearchText from the user and the resulting airport models that are returned. Since we will have a list of airport models, let’s go and create that model now. Right-click on the Models folder and add a new model named Airport, just like you did when adding AirportSearch. In the newly created Airport.cs file, add this code, which is all of the fields that we have stored in our search:
Now, let’s go to our AFDSearch project, go to the Views folder and start making some changes. First, let’s strip everything extraneous out of ~\Views\Home\Index.cshtml and ~\Views\Shared\_Layout. You should open ~Views\Home\Index.cshtml and replace it’s contents with this:
This will strip the index form down to a text box with a search button.
Now, open the ~\Views\Shared\_Layout file and replace its contents with these:
This will strip out the extraneous header and footer information in the ASP.NET template.
Now let’s go create the one other view that we will need, the airport search view. To do this, add a new folder under Views called Airport. Then, right-click on the Airport folder and click Add\View… In the dialog that pops up, change the View name to Search and click Add.
Add the following code to the Search.cshtml file:
This view will use the AirportSearch model to populate this view. The model will contain a list of airports returned from search and we will put this list into an html table. Since we will be using this model in this view, let’s go create it now.
Now we just need to create a controller for airport search. Right-click on the Controllers folder and click Add\Controller… Select MVC 5 Controller – Empty and click Add.
In the subsequent dialog, enter AirportController:
Now copy this code into the AirportController.cs file:
Let me be the first to say that this is not a good separation of concerns! My controller is really too heavy. Normally, the controller would negotiate with a business layer who would dictate the rules of these transactions and that layer would, in turn, communicate with the persistence (or in our case, search) layer. Since we are doing a quick demo, however, we will accept this heavy controller as a matter of demonstration.
The controller, on load, goes to the Web.config file, pulls in our search information (name, keys, etc.), and creates a new AzureSearchEngine from the BlackBarLabs.Search.Azure library. Then when called on search, SearchDocumentsAsync will be called asynchronously to get our search results. The results will be called back here for assembly into the model of our choice (in this case Airport) and will then be returned in the result. The results will then be assembled into our resultsModel and set to our view for rendering into the results table.
Before we try to run our code, you will need to go to the Web.config file and add these lines in the <appSettings> section:
Of course, replace the SearchServiceName and SearchServiceApiKey values with the values from your search that you created in the Azure Portal. Save this file after adding your keys.
You should now be ready to test out your new search tool. Run the project. You will see a simple form load in your default broswer:
Enter ATL and click the Search button:
The view should now show your search text along with a table with your search results:
Now enter GA in the search box and click the Search button again:
You will now see all of the airports from the state of Georgia.
Bear in mind that Azure is combing through all of the fields that we set as searchable, looking for a string match. This could lead to more results coming back than just those from the state of GA if, for instance, an airport name from another state was “GA”. You can fine tune this by judiciously selecting searchable fields.
So, in this post, we have explored the simplest foray into search, the simple text-based search. In our next post, Microsoft Azure Search – A Practical Introduction – Part 4, Faceted Search, we will look at adding facets to our search.