Skip to content

Commit d942d1e

Browse files
committed
All parts working individually....
1 parent 4e55e29 commit d942d1e

File tree

7 files changed

+125
-18
lines changed

7 files changed

+125
-18
lines changed

quickstart/AzureSearchClient.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ class AzureSearchClient {
99
console.log("\n Checking if index exists...");
1010
const endpoint = this.searchServiceHelper.getIndexExistsUrl();
1111
const response = await this.searchServiceHelper.request(endpoint, "GET", null);
12-
console.log(`The response was ${response}`);
1312
// Success has a few likely status codes: 200 or 204 (No Content), but accept all in 200 range...
1413
const exists = response.status >= 200 && response.status < 300;
15-
console.log(`Index exists? ${exists}`);
14+
this.searchServiceHelper.throwOnHttpError(response);
1615
return exists;
1716
}
1817

1918
async deleteIndexAsync() {
2019
console.log("\n Deleting existing index...");
2120
const endpoint = this.searchServiceHelper.getIndexExistsUrl();
22-
const response = await this.searchServiceHelper.request(endpoint, "DELETE", null);
23-
this.searchServiceHelper.throwOnHttpError(response.status);
21+
const response = await this.searchServiceHelper.request(endpoint, "DELETE");
22+
this.searchServiceHelper.throwOnHttpError(response);
2423
return this;
2524
}
2625

@@ -29,10 +28,26 @@ class AzureSearchClient {
2928
const endpoint = this.searchServiceHelper.getCreateIndexUrl();
3029

3130
const response = await this.searchServiceHelper.request(endpoint, "PUT", definition);
32-
console.log(response);
33-
this.searchServiceHelper.throwOnHttpError(response.status);
31+
this.searchServiceHelper.throwOnHttpError(response);
3432
return this;
3533
}
34+
35+
async loadDataAsync(hotelsData) {
36+
console.log("\n Adding hotel data...");
37+
const endpoint = this.searchServiceHelper.getPostDataUrl();
38+
39+
console.log(JSON.stringify(hotelsData));
40+
const response = await this.searchServiceHelper.request(endpoint,"POST", hotelsData);
41+
this.searchServiceHelper.throwOnHttpError(response);
42+
return this;
43+
}
44+
45+
async queryAsync(searchTerm) {
46+
const endpoint = this.searchServiceHelper.getSearchUrl(searchTerm);
47+
const response = await this.searchServiceHelper.request(endpoint, "GET");
48+
this.searchServiceHelper.throwOnHttpError(response);
49+
return response;
50+
}
3651
}
3752

3853
module.exports = AzureSearchClient;

quickstart/SearchServiceHelper.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ class SearchServiceHelper {
1111
_indexUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}?api-version=${this.apiVersion}`; }
1212

1313
getIndexExistsUrl() { return this._indexUrl(); }
14-
getCreateIndexUrl() { return this._indexUrl(); }
14+
getCreateIndexUrl() { return this._indexUrl(); }
1515

16-
getSearchURL(searchTerm) { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs?api-version=${this.apiVersion}&search=${searchTerm}&searchMode=all`; }
16+
getPostDataUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs/index?api-version=${this.apiVersion}`; }
17+
18+
getSearchUrl(searchTerm) { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}/docs?api-version=${this.apiVersion}&search=${searchTerm}&searchMode=all`; }
1719

1820

1921
request(url, method, bodyJson = null) {
@@ -36,9 +38,11 @@ class SearchServiceHelper {
3638
return fetch(url, init);
3739
}
3840

39-
throwOnHttpError(statusCode) {
40-
if (statusCode >= 500){
41-
console.log(`Request returned error code ${statusCode}`);
41+
throwOnHttpError(response) {
42+
const statusCode = response.status;
43+
console.log(`Response Status: ${statusCode}`);
44+
if (statusCode >= 300){
45+
console.log(`Request returned error code ${JSON.stringify(response)}`);
4246
throw new UserException(`Failure in request. HTTP Status was ${statusCode}`);
4347
}
4448
}

quickstart/data/hotel1.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"HotelId": "1",
3+
"HotelName": "Secret Point Motel",
4+
"Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
5+
"Description_fr": "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.",
6+
"Category": "Boutique",
7+
"Tags": [ "pool", "air conditioning", "concierge" ],
8+
"ParkingIncluded": false,
9+
"LastRenovationDate": "1970-01-18T00:00:00Z",
10+
"Rating": 3.60,
11+
"Address": {
12+
"StreetAddress": "677 5th Ave",
13+
"City": "New York",
14+
"StateProvince": "NY",
15+
"PostalCode": "10022"
16+
}
17+
}

quickstart/data/hotel2.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"HotelId": "2",
3+
"HotelName": "Twin Dome Motel",
4+
"Description": "The hotel is situated in a nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.",
5+
"Description_fr": "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.",
6+
"Category": "Boutique",
7+
"Tags": [ "pool", "free wifi", "concierge" ],
8+
"ParkingIncluded": "false",
9+
"LastRenovationDate": "1979-02-18T00:00:00Z",
10+
"Rating": 3.60,
11+
"Address": {
12+
"StreetAddress": "140 University Town Center Dr",
13+
"City": "Sarasota",
14+
"StateProvince": "FL",
15+
"PostalCode": "34243"
16+
}
17+
}

quickstart/data/hotel3.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"HotelId": "3",
3+
"HotelName": "Triple Landscape Hotel",
4+
"Description": "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services.",
5+
"Description_fr": "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.",
6+
"Category": "Resort and Spa",
7+
"Tags": [ "air conditioning", "bar", "continental breakfast" ],
8+
"ParkingIncluded": "true",
9+
"LastRenovationDate": "2015-09-20T00:00:00Z",
10+
"Rating": 4.80,
11+
"Address": {
12+
"StreetAddress": "3393 Peachtree Rd",
13+
"City": "Atlanta",
14+
"StateProvince": "GA",
15+
"PostalCode": "30326"
16+
}
17+
}

quickstart/data/hotel4.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"HotelId": "4",
3+
"HotelName": "Sublime Cliff Hotel",
4+
"Description": "Sublime Cliff Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Cliff is part of a lovingly restored 1800 palace.",
5+
"Description_fr": "Le sublime Cliff Hotel est situé au coeur du centre historique de sublime dans un quartier extrêmement animé et vivant, à courte distance de marche des sites et monuments de la ville et est entouré par l'extraordinaire beauté des églises, des bâtiments, des commerces et Monuments. Sublime Cliff fait partie d'un Palace 1800 restauré avec amour.",
6+
"Category": "Boutique",
7+
"Tags": [ "concierge", "view", "24-hour front desk service" ],
8+
"ParkingIncluded": true,
9+
"LastRenovationDate": "1960-02-06T00:00:00Z",
10+
"Rating": 4.60,
11+
"Address": {
12+
"StreetAddress": "7400 San Pedro Ave",
13+
"City": "San Antonio",
14+
"StateProvince": "TX",
15+
"PostalCode": "78216"
16+
}
17+
}

quickstart/index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,40 @@ function getAzureConfiguration() {
1212
return config;
1313
}
1414

15+
const queries = [
16+
"*&$count=true",
17+
"historic&$filter=Rating gt 4&"
18+
];
19+
20+
const hotelData = { value : [
21+
require('./data/hotel1.json'),
22+
require('./data/hotel2.json'),
23+
require('./data/hotel3.json'),
24+
require('./data/hotel4.json')
25+
]}
26+
1527
const run = async () => {
1628
try {
1729
const cfg = getAzureConfiguration();
1830
const helper = new SearchServiceHelper(cfg.get("serviceName"), cfg.get("apiKey"), "hotels");
1931
const client = new AzureSearchClient(helper);
2032

21-
const exists = await client.indexExistsAsync();
22-
await exists ? client.deleteIndexAsync() : Promise.resolve();
23-
const indexDefinition = require('./hotels_quickstart_index.json');
24-
await client.createIndexAsync(indexDefinition);
33+
//const exists = await client.indexExistsAsync();
34+
//await exists ? client.deleteIndexAsync() : Promise.resolve();
35+
//const indexDefinition = require('./hotels_quickstart_index.json');
36+
//await client.createIndexAsync(indexDefinition);
37+
//await client.loadDataAsync(hotelData);
38+
// var indexComplete = false
39+
// do {
40+
// indexComplete = await client.indexCompletedAsync();
41+
// }while(! indexComplete)
2542
queries.forEach(async (query) => {
26-
const result = await client.queryAsync(q);
27-
console.log(`Query: ${q} \n ${result}`);
28-
})
43+
const result = await client.queryAsync(query);
44+
const body = await result.json();
45+
const str = JSON.stringify(body, null, 4);
46+
console.log(`Query: ${query} \n ${str}`);
47+
});
48+
console.log("Finished.");
2949
} catch (x) {
3050
console.log(x);
3151
}

0 commit comments

Comments
 (0)