|
| 1 | +const fetch = require('node-fetch'); |
| 2 | + |
| 3 | +class AzureSearchClient { |
| 4 | + constructor(searchServiceName, adminKey, queryKey, indexName) { |
| 5 | + this.searchServiceName = searchServiceName; |
| 6 | + this.adminKey = adminKey; |
| 7 | + // The query key is used for read-only requests and so can be distributed with less risk of abuse. |
| 8 | + this.queryKey = queryKey; |
| 9 | + this.indexName = indexName; |
| 10 | + this.apiVersion = '2019-05-06'; |
| 11 | + } |
| 12 | + |
| 13 | + getIndexUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}?api-version=${this.apiVersion}`; } |
| 14 | + |
| 15 | + getPostDataUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs/index?api-version=${this.apiVersion}`; } |
| 16 | + |
| 17 | + getSearchUrl(searchTerm) { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs?api-version=${this.apiVersion}&search=${searchTerm}&searchMode=all`; } |
| 18 | + |
| 19 | + static async request(url, method, apiKey, bodyJson = null) { |
| 20 | + // Uncomment the following for request details: |
| 21 | + /* |
| 22 | + console.log(`\n${method} ${url}`); |
| 23 | + console.log(`\nKey ${apiKey}`); |
| 24 | + if (bodyJson !== null) { |
| 25 | + console.log(`\ncontent: ${JSON.stringify(bodyJson, null, 4)}`); |
| 26 | + } |
| 27 | + */ |
| 28 | + |
| 29 | + const headers = { |
| 30 | + 'content-type' : 'application/json', |
| 31 | + 'api-key' : apiKey |
| 32 | + }; |
| 33 | + const init = bodyJson === null ? |
| 34 | + { |
| 35 | + method, |
| 36 | + headers |
| 37 | + } |
| 38 | + : |
| 39 | + { |
| 40 | + method, |
| 41 | + headers, |
| 42 | + body : JSON.stringify(bodyJson) |
| 43 | + }; |
| 44 | + return fetch(url, init); |
| 45 | + } |
| 46 | + |
| 47 | + static throwOnHttpError(response) { |
| 48 | + const statusCode = response.status; |
| 49 | + if (statusCode >= 300){ |
| 50 | + console.log(`Request failed: ${JSON.stringify(response, null, 4)}`); |
| 51 | + throw new Error(`Failure in request. HTTP Status was ${statusCode}`); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + async indexExistsAsync() { |
| 56 | + console.log("\n Checking if index exists..."); |
| 57 | + const endpoint = this.getIndexUrl(); |
| 58 | + const response = await AzureSearchClient.request(endpoint, "GET", this.adminKey); |
| 59 | + // Success has a few likely status codes: 200 or 204 (No Content), but accept all in 200 range... |
| 60 | + const exists = response.status >= 200 && response.status < 300; |
| 61 | + return exists; |
| 62 | + } |
| 63 | + |
| 64 | + async deleteIndexAsync() { |
| 65 | + console.log("\n Deleting existing index..."); |
| 66 | + const endpoint = this.getIndexUrl(); |
| 67 | + const response = await AzureSearchClient.request(endpoint, "DELETE", this.adminKey); |
| 68 | + AzureSearchClient.throwOnHttpError(response); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + async createIndexAsync(definition) { |
| 73 | + console.log("\n Creating index..."); |
| 74 | + const endpoint = this.getIndexUrl(); |
| 75 | + const response = await AzureSearchClient.request(endpoint, "PUT", this.adminKey, definition); |
| 76 | + AzureSearchClient.throwOnHttpError(response); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + async postDataAsync(hotelsData) { |
| 81 | + console.log("\n Adding hotel data..."); |
| 82 | + const endpoint = this.getPostDataUrl(); |
| 83 | + const response = await AzureSearchClient.request(endpoint,"POST", this.adminKey, hotelsData); |
| 84 | + AzureSearchClient.throwOnHttpError(response); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + async queryAsync(searchTerm) { |
| 89 | + console.log("\n Querying...") |
| 90 | + const endpoint = this.getSearchUrl(searchTerm); |
| 91 | + const response = await AzureSearchClient.request(endpoint, "GET", this.queryKey); |
| 92 | + AzureSearchClient.throwOnHttpError(response); |
| 93 | + return response; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = AzureSearchClient; |
0 commit comments