# Friday, 01 March 2019

Azure Search allows you to create a service making your own data searchable, in much the same way that public search engines like Google and Bing make data on the Internet searchable.

In previous articles, I showed how to create an Azure Search Service; and how to import and index data in that service.

In this article, I will show how to use a REST API exposed by the Azure Search service to return indexed results, based on search criteria.

You can do some limited searching using the Azure portal. Navigate to the Azure portal and login; then, navigate to the Azure Search service, as shown in Fig. 1.

as01-OverviewBlade
Fig. 1

Click the [Search explorer] button (Fig. 2) to display the "Search explorer" blade, as shown in Fig. 3.

as02-SearchExplorerButton
Fig. 2

as03-SearchExplorerBlade
Fig. 3

At the "Query string" field, you can enter a search term and click the [Search] button to return all the data (in JSON format) that matches the search term in any field you marked "FILTERABLE" in your index. Clicking the [Search] button issues an HTTP GET against the Search service's REST API. The results are shown in Fig. 4.

as04-SearchExplorerResults
Fig. 4

You have more flexibility calling the REST API with a POST request. This is not possible through the portal; but you can use a tool like Postman to make these requests.

The URL to which you POST can be found on the Azure service's "Overview" tab, as shown in in Fig. 5.

as05-Url
Fig. 5

The URL takes the form:

https://<servicename>.search.windows.net

where <servicename> is the name you assigned to this service.

You will also need the Query key. You can find the Query key by opening the Azure Search service's "Keys" blade (Fig. 6) and clicking "Manage query keys" to display the "Manage query keys" blade, as shown in Fig. 7.

as06-KeysBlade
Fig. 6

as07-ManageQueryKeys
Fig. 7

To test POSTing to the REST API, open Postman and open a new tab, as shown in Fig. 8.

as08-Postman
Fig. 8

At the Verb dropdown, select "POST".

At the "Request URL" field, paste in the URL. This will be the URL copied from the "Overview" tab, followed by "/indexes/<indexname>/docs/search?api-version=2017-11-11

where <indexname> is the name of the index you created in the Azure Search service.

This is shown in Fig. 9.

as09-PostmanParamsTab
Fig. 9

Select the "Headers" tab, as shown in Fig. 10.

as10-PostmanHeaderTab
Fig. 10

Enter the following 2 key/value pairs:

Key="api-key"; value=the Query key copied from the service.

Key="Content-Type"; value="application/json"

These are shown in Fig. 11.

as11-PostmanHeaderTab
Fig. 11

Select the "Body" tab to enter search parameters, as shown in Fig. 12.

as12-PostmanBodyTab
Fig. 12

The example shown
{
   "select": "*",
  "filter": "state eq 'IL'",
  "orderby": "presentationDate desc"
}

instructs the API to select all the fields' to filter the data, returning only those in which the "state" field equals "IL"; and sort the results in descending order by presentation date.

The results are shown in Fig. 13.

as13-PostmanResults
Fig. 13

In this article, you learned how to use the REST API to access an Azure Search service.

Click  the [Send] button to POST to the API.