Install Required Libraries
- To work with the Azure Cognitive Search API from JavaScript, you need to use the `@azure/search-documents` library. Install it using npm:
npm install @azure/search-documents
Import and Configure Client
- Import the necessary modules and configure your Azure Cognitive Search client using your search service name and API key:
const { SearchClient, AzureKeyCredential } = require("@azure/search-documents");
const searchServiceName = "your-search-service-name";
const indexName = "your-index-name";
const apiKey = "your-api-key";
const client = new SearchClient(
`https://${searchServiceName}.search.windows.net/`,
indexName,
new AzureKeyCredential(apiKey)
);
Perform a Simple Search
- To perform a basic search, use the `search` method. You can add search parameters to refine your search:
async function simpleSearch() {
const results = await client.search("search-term");
for await (const result of results.results) {
console.log(result);
}
}
simpleSearch().catch(err => console.error("Error during search:", err));
Advanced Search Features
- For more advanced search capabilities, such as filtering, faceting, and pagination, you can pass an options object to the `search` method:
async function advancedSearch() {
const options = {
filter: "field eq 'value'",
facets: ["category,count"],
top: 10,
skip: 0
};
const results = await client.search("search-term", options);
for await (const result of results.results) {
console.log(result);
}
}
advancedSearch().catch(err => console.error("Error during advanced search:", err));
Autocomplete and Suggestions
- The Azure Cognitive Search API also supports autocomplete and suggestions. You can use the `autocomplete` and `suggest` methods:
async function getAutocomplete() {
const autoCompleteResults = await client.autocomplete("part-of-query", "suggester-name");
autoCompleteResults.results.forEach(complete => {
console.log("Autocomplete:", complete.text);
});
}
getAutocomplete().catch(err => console.error("Error in autocomplete:", err));
async function getSuggestions() {
const suggestResults = await client.suggest("partial-query", "suggester-name");
suggestResults.results.forEach(suggestion => {
console.log("Suggestion:", suggestion.text);
});
}
getSuggestions().catch(err => console.error("Error in suggestions:", err));
Error Handling
- Always include error handling in your API calls to manage any unexpected responses or connectivity issues:
try {
const results = await client.search("query");
// ... handle results
} catch (error) {
console.error("Search query failed:", error);
// You can implement retry logic or other handling here
}
Conclusion
- By following these examples, you can efficiently utilize Microsoft Azure Cognitive Search from JavaScript. Tailor your requests to meet specific application needs by adjusting search parameters, leveraging filtering capabilities, or employing autocomplete and suggestion functionalities. Always ensure to protect your API key and handle errors gracefully to build robust search experiences.