Get started with Azure Search in Node.js
Learn how to build a custom Node.js search application that uses Azure Search for its search experience. This tutorial uses the Azure Search Service REST API to construct the objects and operations used in this exercise.
About the data
This sample application uses data from the United States Geological Services (USGS), filtered on the state of Rhode Island to reduce the dataset size. We'll use this data to build a search application that returns landmark buildings such as hospitals and schools, as well as geological features like streams, lakes, and summits.
In this application, the DataIndexer program builds and loads the index using an Indexer construct, retrieving the filtered USGS dataset from a public Azure SQL Database. Credentials and connection information to the online data source is provided in the program code. No further configuration is necessary.
We applied a filter on this dataset to stay under the 10,000 document limit of the free pricing tier. If you use the standard tier, this limit does not apply. For details about capacity for each pricing tier, see Search service limits.
Find the service name and api-key of your Azure Search service
After you create the service, return to the portal to get the URL or
api-key. Connections to your Search service require that you have both the URL and an
api-key to authenticate the call.
- Sign in to the Azure portal.
- In the jump bar, click Search service to list all Azure Search services provisioned for your subscription.
- Select the service you want to use.
- On the service dashboard, you should see tiles for essential information, such as the key icon for accessing the admin keys.
- Copy the service URL, an admin key, and a query key. You need all three later when you add them to the config.js file.
Download the sample files
Use either one of the following approaches to download the sample.
- Go to search-node-indexer-demo.
- Click Download ZIP, save the .zip file, and then extract all the files it contains.
All subsequent file modifications and run statements are made against files in this folder.
Update the config.js. with your Search service URL and api-key
Using the URL and api-key that you copied earlier, specify the URL, admin-key, and query-key in configuration file.
Admin keys grant full control over service operations, including creating or deleting an index and loading documents. In contrast, query keys are for read-only operations, typically used by client applications that connect to Azure Search.
In this sample, we include the query key to help reinforce the best practice of using the query key in client applications.
The following screenshot shows config.js open in a text editor, with the relevant entries demarcated so that you can see where to update the file with the values that are valid for your search service.
Host a runtime environment for the sample
The sample requires an HTTP server, which you can install globally using npm.
Use a PowerShell window for the following commands.
- Navigate to the folder that contains the package.json file.
npm install -g http-server.
Build the index and run the application
npm run indexDocuments.
npm run build.
npm run start_server.
- Direct your browser at
Search on USGS data
The USGS data set includes records that are relevant to the state of Rhode Island. If you click Search on an empty search box, you get the top 50 entries, which is the default.
Entering a search term gives the search engine something to go on. Try entering a regional name. "Roger Williams" was the first governor of Rhode Island. Numerous parks, buildings, and schools are named after him.
You could also try any of these terms:
- goose +cape
This is the first Azure Search tutorial based on Node.js and the USGS dataset. Over time, we'll extend this tutorial to demonstrate additional search features you might want to use in your custom solutions.
If you already have some background in Azure Search, you can use this sample as a springboard for trying suggesters (type-ahead or auto-complete queries), filters, and faceted navigation. You can also improve upon the search results page by adding counts and batching documents so that users can page through the results.
New to Azure Search? We recommend trying other tutorials to develop an understanding of what you can create. Visit our documentation page to find more resources.