Skip to content

Commit fd5b6a3

Browse files
committed
Creates index
1 parent 9bad524 commit fd5b6a3

File tree

5 files changed

+289
-5
lines changed

5 files changed

+289
-5
lines changed

quickstart/AzureSearchClient.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class AzureSearchClient {
2+
3+
4+
constructor (searchServiceHelper) {
5+
this.searchServiceHelper = searchServiceHelper;
6+
}
7+
8+
indexExists() {
9+
console.log("\n Checking if index exists...");
10+
const endpoint = this.searchServiceHelper.getIndexExistsUrl();
11+
this.searchServiceHelper.request(endpoint, "GET", null)
12+
.then((response) => {
13+
return response.status == 200;
14+
});
15+
}
16+
17+
createIndex(definition) {
18+
console.log("\n Creating index...");
19+
const endpoint = this.searchServiceHelper.getCreateIndexUrl();
20+
21+
this.searchServiceHelper.request(endpoint, "PUT", definition)
22+
.then((response) => {
23+
console.log(response);
24+
this.searchServiceHelper.throwOnHttpError(response.status);
25+
});
26+
27+
}
28+
}
29+
30+
module.exports = AzureSearchClient;

quickstart/SearchServiceHelper.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fetch = require('node-fetch');
2+
3+
class SearchServiceHelper {
4+
constructor(searchServiceName, apiKey, indexName) {
5+
this.searchServiceName = searchServiceName;
6+
this.apiKey = apiKey;
7+
this.indexName = indexName;
8+
this.apiVersion = '2019-05-06';
9+
console.log(`${searchServiceName} | ${this.searchServiceName}`);
10+
}
11+
12+
_indexUrl() { return `https://${this.searchServiceName}.search.windows.net/indexes/${this.indexName}?api-version=${this.apiVersion}`; }
13+
14+
getIndexExistsUrl() { return this._indexUrl(); }
15+
getCreateIndexUrl() { return this._indexUrl(); }
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+
20+
request(url, method, bodyContent = null) {
21+
const headers = {
22+
'content-type' : 'application/json',
23+
'api-key' : this.apiKey,
24+
'proxy' : 'http://127.0.0.1:8888'
25+
};
26+
const init = bodyContent === null ?
27+
{
28+
method : method,
29+
headers : headers
30+
}
31+
:
32+
{
33+
method : method,
34+
headers : headers,
35+
body : JSON.stringify(bodyContent)
36+
};
37+
console.log(url);
38+
console.log(`init = ${init.body}`)
39+
return fetch(url, init);
40+
}
41+
42+
throwOnHttpError(statusCode) {
43+
if (statusCode >= 500){
44+
console.log(`Request returned error code ${statusCode}`);
45+
throw new UserException(`Failure in request. HTTP Status was ${statusCode}`);
46+
}
47+
}
48+
}
49+
50+
module.exports = SearchServiceHelper;
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{
2+
"name": "hotels",
3+
"fields": [
4+
{
5+
"name": "HotelId",
6+
"type": "Edm.String",
7+
"key": true,
8+
"filterable": true
9+
},
10+
{
11+
"name": "HotelName",
12+
"type": "Edm.String",
13+
"searchable": true,
14+
"filterable": false,
15+
"sortable": true,
16+
"facetable": false
17+
},
18+
{
19+
"name": "Description",
20+
"type": "Edm.String",
21+
"searchable": true,
22+
"filterable": false,
23+
"sortable": false,
24+
"facetable": false,
25+
"analyzer": "en.lucene"
26+
},
27+
{
28+
"name": "Description_fr",
29+
"type": "Edm.String",
30+
"searchable": true,
31+
"filterable": false,
32+
"sortable": false,
33+
"facetable": false,
34+
"analyzer": "fr.lucene"
35+
},
36+
{
37+
"name": "Category",
38+
"type": "Edm.String",
39+
"searchable": true,
40+
"filterable": true,
41+
"sortable": true,
42+
"facetable": true
43+
},
44+
{
45+
"name": "Tags",
46+
"type": "Collection(Edm.String)",
47+
"searchable": true,
48+
"filterable": true,
49+
"sortable": false,
50+
"facetable": true
51+
},
52+
{
53+
"name": "ParkingIncluded",
54+
"type": "Edm.Boolean",
55+
"filterable": true,
56+
"sortable": true,
57+
"facetable": true
58+
},
59+
{
60+
"name": "LastRenovationDate",
61+
"type": "Edm.DateTimeOffset",
62+
"filterable": true,
63+
"sortable": true,
64+
"facetable": true
65+
},
66+
{
67+
"name": "Rating",
68+
"type": "Edm.Double",
69+
"filterable": true,
70+
"sortable": true,
71+
"facetable": true
72+
},
73+
{
74+
"name": "Address",
75+
"type": "Edm.ComplexType",
76+
"fields": [
77+
{
78+
"name": "StreetAddress",
79+
"type": "Edm.String",
80+
"filterable": false,
81+
"sortable": false,
82+
"facetable": false,
83+
"searchable": true
84+
},
85+
{
86+
"name": "City",
87+
"type": "Edm.String",
88+
"searchable": true,
89+
"filterable": true,
90+
"sortable": true,
91+
"facetable": true
92+
},
93+
{
94+
"name": "StateProvince",
95+
"type": "Edm.String",
96+
"searchable": true,
97+
"filterable": true,
98+
"sortable": true,
99+
"facetable": true
100+
},
101+
{
102+
"name": "PostalCode",
103+
"type": "Edm.String",
104+
"searchable": true,
105+
"filterable": true,
106+
"sortable": true,
107+
"facetable": true
108+
},
109+
{
110+
"name": "Country",
111+
"type": "Edm.String",
112+
"searchable": true,
113+
"filterable": true,
114+
"sortable": true,
115+
"facetable": true
116+
}
117+
]
118+
},
119+
{
120+
"name": "Location",
121+
"type": "Edm.GeographyPoint",
122+
"filterable": true,
123+
"sortable": true
124+
},
125+
{
126+
"name": "Rooms",
127+
"type": "Collection(Edm.ComplexType)",
128+
"fields": [
129+
{
130+
"name": "Description",
131+
"type": "Edm.String",
132+
"searchable": true,
133+
"filterable": false,
134+
"sortable": false,
135+
"facetable": false,
136+
"analyzer": "en.lucene"
137+
},
138+
{
139+
"name": "Description_fr",
140+
"type": "Edm.String",
141+
"searchable": true,
142+
"filterable": false,
143+
"sortable": false,
144+
"facetable": false,
145+
"analyzer": "fr.lucene"
146+
},
147+
{
148+
"name": "Type",
149+
"type": "Edm.String",
150+
"searchable": true
151+
},
152+
{
153+
"name": "BaseRate",
154+
"type": "Edm.Double",
155+
"filterable": true,
156+
"facetable": true
157+
},
158+
{
159+
"name": "BedOptions",
160+
"type": "Edm.String",
161+
"searchable": true
162+
},
163+
{
164+
"name": "SleepsCount",
165+
"type": "Edm.Int32",
166+
"filterable": true,
167+
"facetable": true
168+
},
169+
{
170+
"name": "SmokingAllowed",
171+
"type": "Edm.Boolean",
172+
"filterable": true,
173+
"facetable": true
174+
},
175+
{
176+
"name": "Tags",
177+
"type": "Collection(Edm.String)",
178+
"searchable": true,
179+
"filterable": true,
180+
"facetable": true
181+
}
182+
]
183+
}
184+
],
185+
"suggesters": [
186+
{
187+
"name": "sg",
188+
"searchMode": "analyzingInfixMatching",
189+
"sourceFields": [
190+
"HotelName"
191+
]
192+
}
193+
]
194+
}

quickstart/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env/node
22

3-
const nconf = require('nconf')
3+
const nconf = require('nconf');
4+
const SearchServiceHelper = require('./SearchServiceHelper.js');
5+
const AzureSearchClient = require('./AzureSearchClient.js');
46

57
function getAzureConfiguration() {
68
const config = nconf.file({ file: 'azure_search_config.json' });
@@ -10,10 +12,17 @@ function getAzureConfiguration() {
1012
return config;
1113
}
1214

13-
const run = async() => {
15+
const run = () => {
1416
try {
15-
const cfg = getAzureConfiguration();
16-
console.log("Hello, Node from CLI");
17+
const cfg = getAzureConfiguration();
18+
const helper = new SearchServiceHelper(cfg.get("serviceName"), cfg.get("apiKey"), "hotels");
19+
const client = new AzureSearchClient(helper);
20+
if (client.indexExists()) {
21+
client.deleteIndex();
22+
}
23+
24+
const indexDefinition = require('./hotels_quickstart_index.json');
25+
client.createIndex(indexDefinition);
1726
} catch (x) {
1827
console.log(x);
1928
}

quickstart/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"url": "."
1212
},
1313
"dependencies": {
14-
"nconf": "^0.9.0"
14+
"nconf": "^0.9.0",
15+
"node-fetch": "^2.6.0"
1516
},
1617
"author": "Larry O'Brien",
1718
"license": "MIT",

0 commit comments

Comments
 (0)