Editar

Partilhar via


Quickstart: Use the Bing Entity Search JavaScript client library

Use this quickstart to begin searching for entities with the Bing Entity Search client library for JavaScript. While Bing Entity Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub.

Prerequisites

To install the Bing Entity Search SDK:

  1. Run npm install ms-rest-azure in your development environment.
  2. Run npm install @azure/cognitiveservices-entitysearch in your development environment.

Create and initialize the application

  1. Create a new JavaScript file in your favorite IDE or editor, and add the following requirements.

    const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
    const EntitySearchAPIClient = require('@azure/cognitiveservices-entitysearch');
    
  2. Create an instance of CognitiveServicesCredentials using your subscription key. Then create an instance of the search client with it.

    let credentials = new CognitiveServicesCredentials('YOUR-ACCESS-KEY');
    let entitySearchApiClient = new EntitySearchAPIClient(credentials);
    

Send a request and receive a response

  1. Send an entities search request with entitiesOperations.search(). After receiving a response, print out the queryContext, number of returned results, and the description of the first result.

    entitySearchApiClient.entitiesOperations.search('seahawks').then((result) => {
        console.log(result.queryContext);
        console.log(result.entities.value);
        console.log(result.entities.value[0].description);
    }).catch((err) => {
        throw err;
    });
    

Next steps