This package contains an isomorphic SDK for CustomSearchClient.
- Node.js version 6.x.x or higher
- Browser JavaScript
npm install @azure/cognitiveservices-customsearch
nodejs - Authentication, client creation and search customInstance as an example written in TypeScript.
npm install @azure/ms-rest-azure-js
The following sample performs a search for given query on a custom configuration. The custom configuration can be setup using the Custom search portal. To know more, refer to the Azure Documentation Bing Custom Search
import {
CustomSearchClient,
CustomSearchModels
} from "@azure/cognitiveservices-customsearch";
import { CognitiveServicesCredentials } from "@azure/ms-rest-azure-js";
async function main(): Promise<void> {
const customSearchKey = process.env["customSearchKey"] || "<customSearchKey>";
const customSearchEndPoint =
process.env["customSearchEndPoint"] || "<customSearchEndPoint>";
const customConfig = process.env["customConfig"] || "<customConfig>";
const cognitiveServiceCredentials = new CognitiveServicesCredentials(
customSearchKey
);
const client = new CustomSearchClient(cognitiveServiceCredentials, {
endpoint: customSearchEndPoint
});
const query = "World Peace";
const options: CustomSearchModels.CustomInstanceSearchOptionalParams = {
count: 10,
safeSearch: "Moderate"
};
client.customInstance
.search(customConfig, query, options)
.then(result => {
console.log("The result is: ");
console.log(result);
})
.catch(err => {
console.log("An error occurred:");
console.error(err);
});
}
main();
browser - Authentication, client creation and search customInstance as an example written in JavaScript.
- index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>@azure/cognitiveservices-customsearch sample</title>
<script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script>
<script src="node_modules/@azure/cognitiveservices-customsearch/dist/cognitiveservices-customsearch.js"></script>
<script type="text/javascript">
const customSearchKey = "<YOUR_CUSTOM_SEARCH_KEY>";
const customSearchEndPoint = "<YOUR_CUSTOM_SEARCH_ENDPOINT>";
const customConfig = "<YOUR_CUSTOM_CONFIG>";
const cognitiveServiceCredentials = new msRest.ApiKeyCredentials({
inHeader: {
"Ocp-Apim-Subscription-Key": customSearchKey
}
});
const client = new Azure.CognitiveservicesCustomsearch.CustomSearchClient(
cognitiveServiceCredentials,
{
endpoint: customSearchEndPoint
}
);
const query = "World Peace";
const options = {
count: 10,
safeSearch: "Moderate"
};
client.customInstance
.search(customConfig, query, options)
.then(result => {
console.log("The result is: ");
console.log(result);
})
.catch(err => {
console.log("An error occurred:");
console.error(err);
});
</script>
</head>
<body></body>
</html>