Skip to content

Latest commit

 

History

History
86 lines (58 loc) · 4.38 KB

File metadata and controls

86 lines (58 loc) · 4.38 KB
page_type languages name description products urlFragment
sample
javascript
typescript
nodejs
JavaScript quickstart for Azure AI Search
Learn how to create, load, and query an Azure AI Search index using the Azure SDK for Javascript/Typescript.
azure
azure-cognitive-search
javascript-quickstart

JavaScript quickstart for Azure AI Search

Quickstart sample MIT license badge

Demonstrates using JavaScript and the Azure SDK for JavaScript/TypeScript to create an Azure AI Search index, load it with documents, and execute a few queries. The index is modeled on a subset of the Hotels dataset, reduced for readability and comprehension. Index definition and documents are included in the code.

This Node.js console application is featured in Quickstart: Create an Azure AI Search index using the JavaScript SDK. When you run the program, a console window emits output messages for each step: deleting and then re-creating a hotels-quickstart index, loading documents, running queries. This sample uses the Azure SDK for JavaScript/TypeScript and runs on a search service using connection information that you provide.

Prerequisites

Set up the sample

  1. Clone or download this sample repository.

  2. Open the folder in Visual Studio Code and navigate to the quickstart folder:

    cd quickstart
  3. Install the dependencies using npm:

    npm install
  4. Edit the file sample.env, adding the connection information that's valid for your Azure AI Search service. See

    SEARCH_API_KEY=<search-admin-key>
    SEARCH_API_ENDPOINT=https://<search-service-name>.search.windows.net
    
  5. Rename sample.env to just .env. The quickstart will read the .env file automatically.

Run the sample

  1. Run the following command to start the program.

    node index.js

You should see a series of messages relating to the creation of the search index, adding documents to it, and, finally, results of a series of queries.

If you get a 401 error, make sure the API key is correct (you need an admin API key to create objects), and make sure the search service is configured for key-based authentication.

Key concepts

The file hotels_quickstart_index.json holds the definition of an index for the data in the file hotels.json. Review those files to see the fields, which ones are searchable, etc.

The file index.js automatically reads the .env file which contains the SEARCH_API_KEY and SEARCH_API_ENDPOINT needed to create the SearchIndexClient. The sleep function is used to pause execution in between major steps such as creating the index, submitting data for indexing, etc. Such pauses are generally only needed in test, demo, and sample code.

The run function :

  • Checks if the hotels-quickstart index exists.
  • If so, the program deletes the existing index.
  • Creates a new hotels-quickstart index from the structure in hotels_quickstart_index.json.
  • Adds the data from hotels.json to the hotels-quickstart index.
  • Executes a few basic queries against the search index.

Next steps

You can learn more about Azure AI Search on the official documentation site.

You can view additional samples for JavaScript/TypeScript in the azure-sdk-for-js repo or see the documentation.