Skip to content

Commit 37f1884

Browse files
authored
Merge pull request #73 from diberry/diberry/0626-vector-code-ids
Vector Fixes
2 parents 4d48216 + a7f5d20 commit 37f1884

File tree

5 files changed

+82
-16
lines changed

5 files changed

+82
-16
lines changed

quickstart-vector-js/src/manageIndex.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// <Index_dependencies>
12
import { DefaultAzureCredential } from "@azure/identity";
23
import {
34
SearchIndexClient,
@@ -12,7 +13,8 @@ export const indexName = process.env.AZURE_SEARCH_INDEX_NAME;
1213

1314
console.log(`Using Azure Search endpoint: ${searchEndpoint}`);
1415
console.log(`Using index name: ${indexName}`);
15-
16+
// </Index_dependencies>
17+
// <Index_createIndex>
1618
export async function createIndex() {
1719

1820
const indexClient = new SearchIndexClient(searchEndpoint, credential);
@@ -212,6 +214,8 @@ export async function deleteIndex(searchIndexClient) {
212214
console.error("Failed to delete index:", ex);
213215
}
214216
}
217+
// </Index_createIndex>
218+
// <Index_uploadDocuments>
215219
export async function uploadDocuments() {
216220
const searchClient = new SearchClient(searchEndpoint, indexName, credential);
217221

@@ -225,7 +229,27 @@ export async function uploadDocuments() {
225229
for (const r of result.results) {
226230
console.log(`Key: ${r.key}, Succeeded: ${r.succeeded}, ErrorMessage: ${r.errorMessage || 'none'}`);
227231
}
232+
await waitUntilIndexed();
228233
} catch (ex) {
229234
console.error("Failed to upload documents:", ex);
230235
}
231236
}
237+
// </Index_uploadDocuments>
238+
// <Index_waitTillIndexed>
239+
export async function waitUntilIndexed() {
240+
try {
241+
const searchClient = new SearchClient(searchEndpoint, indexName, credential);
242+
do {
243+
const count = await searchClient.getDocumentsCount();
244+
if (count == DOCUMENTS.length) {
245+
console.log("All documents indexed successfully.");
246+
break;
247+
}
248+
console.log(`Waiting for indexing... Current count: ${count}`);
249+
await new Promise((resolve) => setTimeout(resolve, 10000)); // Wait for 10 seconds
250+
} while (true);
251+
} catch (ex) {
252+
console.error("Failed to wait until indexed:", ex);
253+
}
254+
}
255+
// </Index_waitTillIndexed>

quickstart-vector-js/src/search.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// <Search_dependencies>
12
import { SearchClient, AzureKeyCredential } from "@azure/search-documents";
23
import { vector } from "./queryVector.js";
34
import { indexName, searchEndpoint } from "./manageIndex.js";
@@ -8,7 +9,9 @@ const searchClient = new SearchClient(
89
indexName,
910
new DefaultAzureCredential()
1011
);
12+
// </Search_dependencies>
1113

14+
// <Search_singleVectorSearch>
1215
export async function singleVectorSearch() {
1316
try {
1417

@@ -45,7 +48,8 @@ export async function singleVectorSearch() {
4548
throw ex;
4649
}
4750
}
48-
51+
// </Search_singleVectorSearch>
52+
// <Search_singleVectorSearchWithFilter>
4953
export async function singleVectorSearchWithFilter() {
5054
try {
5155

@@ -70,7 +74,7 @@ export async function singleVectorSearchWithFilter() {
7074
};
7175
const results = await searchClient.search("*", searchOptions);
7276

73-
console.log(`\n\nSingle Vector search with filter found ${results.count} then limited to top ${searchOptions.top}`);
77+
console.log(`\n\nSingle Vector search with filter found ${results.count}`);
7478

7579
for await (const result of results.results) {
7680
// Log each result
@@ -83,7 +87,8 @@ export async function singleVectorSearchWithFilter() {
8387
throw ex;
8488
}
8589
}
86-
90+
// </Search_singleVectorSearchWithFilter>
91+
// <Search_vectorQueryWithGeoFilter>
8792
export async function vectorQueryWithGeoFilter() {
8893
try {
8994

@@ -110,7 +115,7 @@ export async function vectorQueryWithGeoFilter() {
110115
};
111116
const results = await searchClient.search("*", searchOptions);
112117

113-
console.log(`\n\nVector search with geo filter found ${results.count} then limited to top ${searchOptions.top}`);
118+
console.log(`\n\nVector search with geo filter found ${results.count}`);
114119

115120
for await (const result of results.results) {
116121

@@ -135,8 +140,8 @@ export async function vectorQueryWithGeoFilter() {
135140
throw ex;
136141
}
137142
}
138-
139-
143+
// </Search_vectorQueryWithGeoFilter>
144+
// <Search_hybridSearch>
140145
export async function hybridSearch() {
141146

142147
try {
@@ -185,6 +190,8 @@ export async function hybridSearch() {
185190
}
186191

187192
}
193+
// </Search_hybridSearch>
194+
// <Search_semanticHybridSearch>
188195
export async function semanticHybridSearch() {
189196

190197
try {
@@ -238,4 +245,5 @@ export async function semanticHybridSearch() {
238245
console.error("Semantic hybrid search failed:", ex);
239246
throw ex;
240247
}
241-
}
248+
}
249+
// </Search_semanticHybridSearch>

quickstart-vector-ts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "quickstart-vector-ts",
33
"version": "1.0.0",
44
"main": "dist/index.js",
5+
"type": "module",
56
"scripts": {
67
"build": "tsc",
78
"start": "npm run build && node -r dotenv/config dist/index.js"

quickstart-vector-ts/src/manageIndex.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// <Index_dependencies>
12
import { DefaultAzureCredential } from "@azure/identity";
23
import {
34
SearchIndexClient,
@@ -37,7 +38,8 @@ export interface HotelDocument {
3738
"@search.score"?: number;
3839
"@search.reranker_score"?: number;
3940
}
40-
41+
// </Index_dependencies>
42+
// <Index_createIndex>
4143
export async function createIndex(): Promise<SearchIndexClient> {
4244

4345
const indexClient = new SearchIndexClient(searchEndpoint, credential);
@@ -237,6 +239,8 @@ export async function deleteIndex(searchIndexClient: SearchIndexClient): Promise
237239
console.error("Failed to delete index:", ex);
238240
}
239241
}
242+
// </Index_createIndex>
243+
// <Index_uploadDocuments>
240244
export async function uploadDocuments(): Promise<void> {
241245
const searchClient = new SearchClient(searchEndpoint, indexName, credential);
242246

@@ -250,7 +254,28 @@ export async function uploadDocuments(): Promise<void> {
250254
for (const r of result.results) {
251255
console.log(`Key: ${r.key}, Succeeded: ${r.succeeded}, ErrorMessage: ${r.errorMessage || 'none'}`);
252256
}
257+
await waitUntilIndexed();
253258
} catch (ex) {
254259
console.error("Failed to upload documents:", ex);
255260
}
256261
}
262+
// </Index_uploadDocuments>
263+
// <Index_waitTillIndexed>
264+
export async function waitUntilIndexed(): Promise<void> {
265+
try {
266+
const searchClient = new SearchClient(searchEndpoint, indexName, credential);
267+
do {
268+
const count = await searchClient.getDocumentsCount();
269+
if (count == DOCUMENTS.length) {
270+
console.log("All documents indexed successfully.");
271+
break;
272+
}
273+
console.log(`Waiting for indexing... Current count: ${count}`);
274+
await new Promise((resolve) => setTimeout(resolve, 10000)); // Wait for 10 seconds
275+
} while (true);
276+
} catch (ex) {
277+
console.error("Failed to wait until indexed:", ex);
278+
}
279+
}
280+
// </Index_waitTillIndexed>
281+

quickstart-vector-ts/src/search.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// <Search_dependencies>
12
import { SearchClient, SearchDocumentsResult, VectorQuery, SearchOptions, SearchResult, AzureKeyCredential } from "@azure/search-documents";
23
import { vector } from "./queryVector.js";
34
import { HotelDocument, indexName, searchEndpoint } from "./manageIndex.js";
@@ -15,7 +16,9 @@ const searchClient = new SearchClient<HotelDocument>(
1516
indexName,
1617
new DefaultAzureCredential()
1718
);
19+
// </Search_dependencies>
1820

21+
// <Search_singleVectorSearch>
1922
export async function singleVectorSearch(): Promise<void> {
2023
try {
2124

@@ -52,7 +55,8 @@ export async function singleVectorSearch(): Promise<void> {
5255
throw ex;
5356
}
5457
}
55-
58+
// </Search_singleVectorSearch>
59+
// <Search_singleVectorSearchWithFilter>
5660
export async function singleVectorSearchWithFilter(): Promise<void> {
5761
try {
5862

@@ -77,7 +81,7 @@ export async function singleVectorSearchWithFilter(): Promise<void> {
7781
};
7882
const results: SearchDocumentsResult<HotelDocument> = await searchClient.search("*", searchOptions);
7983

80-
console.log(`\n\nSingle Vector search with filter found ${results.count} then limited to top ${searchOptions.top}`);
84+
console.log(`\n\nSingle Vector search with filter found ${results.count}`);
8185

8286
for await (const result of results.results) {
8387
// Log each result
@@ -90,7 +94,8 @@ export async function singleVectorSearchWithFilter(): Promise<void> {
9094
throw ex;
9195
}
9296
}
93-
97+
// </Search_singleVectorSearchWithFilter>
98+
// <Search_vectorQueryWithGeoFilter>
9499
export async function vectorQueryWithGeoFilter(): Promise<void> {
95100
try {
96101

@@ -117,7 +122,7 @@ export async function vectorQueryWithGeoFilter(): Promise<void> {
117122
};
118123
const results: SearchDocumentsResult<HotelDocument> = await searchClient.search("*", searchOptions);
119124

120-
console.log(`\n\nVector search with geo filter found ${results.count} then limited to top ${searchOptions.top}`);
125+
console.log(`\n\nVector search with geo filter found ${results.count}`);
121126

122127
for await (const result of results.results) {
123128

@@ -142,8 +147,8 @@ export async function vectorQueryWithGeoFilter(): Promise<void> {
142147
throw ex;
143148
}
144149
}
145-
146-
150+
// </Search_vectorQueryWithGeoFilter>
151+
// <Search_hybridSearch>
147152
export async function hybridSearch(): Promise<void> {
148153

149154
try {
@@ -193,6 +198,8 @@ export async function hybridSearch(): Promise<void> {
193198
}
194199

195200
}
201+
// </Search_hybridSearch>
202+
// <Search_semanticHybridSearch>
196203
export async function semanticHybridSearch(): Promise<void> {
197204

198205
try {
@@ -247,4 +254,5 @@ export async function semanticHybridSearch(): Promise<void> {
247254
console.error("Semantic hybrid search failed:", ex);
248255
throw ex;
249256
}
250-
}
257+
}
258+
// </Search_semanticHybridSearch>

0 commit comments

Comments
 (0)