-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Commit for Advanced Batching (#11544)
* Initial Commit for Advanced Batching * Formatting changes * Additional Changes * Formatting changes * Additional Changes * Formatting changes * Response to PR Comments * Formatting changes * Update Message
- Loading branch information
1 parent
7c069d6
commit b6b8037
Showing
11 changed files
with
1,022 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...rch-documents/samples/typescript/src/bufferedSender/uploadDocuments/autoFlushSizeBased.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
SearchIndexingBufferedSender, | ||
AzureKeyCredential, | ||
SearchClient, | ||
GeographyPoint, | ||
SearchIndexClient | ||
} from "@azure/search-documents"; | ||
import { createIndex, WAIT_TIME } from "../../utils/setup"; | ||
import { Hotel } from "../../utils/interfaces"; | ||
import { delay } from "@azure/core-http"; | ||
|
||
/** | ||
* This sample is to demonstrate the use of SearchIndexingBufferedSender. | ||
* In this sample, the autoFlush is set to true. i.e. the user does not | ||
* want to call the flush manually. The upload action happen automatically | ||
* when the size of the batch is greater than threshold (which is set to 1000) | ||
* by default. | ||
*/ | ||
const endpoint = process.env.SEARCH_API_ENDPOINT || ""; | ||
const apiKey = process.env.SEARCH_API_KEY || ""; | ||
const TEST_INDEX_NAME = "hotel-live-sample-test3"; | ||
|
||
function getDocumentsArray(size: number): Hotel[] { | ||
const array: Hotel[] = []; | ||
for (let i = 1; i <= size; i++) { | ||
array.push({ | ||
hotelId: `${i}`, | ||
description: | ||
"Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa, " + | ||
"and a really helpful concierge. The location is perfect -- right downtown, close to all " + | ||
"the tourist attractions. We highly recommend this hotel.", | ||
descriptionFr: | ||
"Meilleur hôtel en ville si vous aimez les hôtels de luxe. Ils ont une magnifique piscine " + | ||
"à débordement, un spa et un concierge très utile. L'emplacement est parfait – en plein " + | ||
"centre, à proximité de toutes les attractions touristiques. Nous recommandons fortement " + | ||
"cet hôtel.", | ||
hotelName: "Fancy Stay", | ||
category: "Luxury", | ||
tags: ["pool", "view", "wifi", "concierge"], | ||
parkingIncluded: false, | ||
lastRenovationDate: new Date(2010, 5, 27), | ||
rating: 5, | ||
location: new GeographyPoint(47.678581, -122.131577) | ||
}); | ||
} | ||
return array; | ||
} | ||
|
||
export async function main() { | ||
console.log(`Running SearchIndexingBufferedSender-uploadDocuments-With Auto Flush Sizes Sample`); | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
const searchClient: SearchClient<Hotel> = new SearchClient<Hotel>( | ||
endpoint, | ||
TEST_INDEX_NAME, | ||
credential | ||
); | ||
const indexClient: SearchIndexClient = new SearchIndexClient(endpoint, credential); | ||
|
||
await createIndex(indexClient, TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
|
||
const bufferedClient: SearchIndexingBufferedSender<Hotel> = searchClient.getSearchIndexingBufferedSenderInstance( | ||
{ | ||
autoFlush: true | ||
} | ||
); | ||
|
||
bufferedClient.on("batchAdded", (response: any) => { | ||
console.log("Batch Added Event has been receieved...."); | ||
}); | ||
|
||
bufferedClient.on("beforeDocumentSent", (response: any) => { | ||
console.log("Before Document Sent Event has been receieved...."); | ||
}); | ||
|
||
bufferedClient.on("batchSucceeded", (response: any) => { | ||
console.log("Batch Succeeded Event has been receieved...."); | ||
console.log(response); | ||
}); | ||
|
||
bufferedClient.on("batchFailed", (response: any) => { | ||
console.log("Batch Failed Event has been receieved...."); | ||
console.log(response); | ||
}); | ||
|
||
const documents: Hotel[] = getDocumentsArray(1001); | ||
bufferedClient.uploadDocuments(documents); | ||
|
||
await WAIT_TIME; | ||
|
||
let count = await searchClient.getDocumentsCount(); | ||
while (count !== documents.length) { | ||
await delay(WAIT_TIME); | ||
count = await searchClient.getDocumentsCount(); | ||
} | ||
|
||
// When the autoFlush is set to true, the user | ||
// has to call the dispose method to clear the | ||
// timer. | ||
bufferedClient.dispose(); | ||
await indexClient.deleteIndex(TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
} | ||
|
||
main(); |
96 changes: 96 additions & 0 deletions
96
...ch-documents/samples/typescript/src/bufferedSender/uploadDocuments/autoFlushTimerBased.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
SearchIndexingBufferedSender, | ||
AzureKeyCredential, | ||
SearchClient, | ||
GeographyPoint, | ||
SearchIndexClient, | ||
DEFAULT_FLUSH_WINDOW | ||
} from "@azure/search-documents"; | ||
import { createIndex, WAIT_TIME } from "../../utils/setup"; | ||
import { Hotel } from "../../utils/interfaces"; | ||
import { delay } from "@azure/core-http"; | ||
|
||
/** | ||
* This sample is to demonstrate the use of SearchIndexingBufferedSender. | ||
* In this sample, the autoFlush is set to true. i.e. the user does not | ||
* want to call the flush manually. The upload action happen automatically | ||
* when the time interval is met. The time interval is set to 60000ms | ||
* by default. | ||
*/ | ||
const endpoint = process.env.SEARCH_API_ENDPOINT || ""; | ||
const apiKey = process.env.SEARCH_API_KEY || ""; | ||
const TEST_INDEX_NAME = "hotel-live-sample-test2"; | ||
|
||
export async function main() { | ||
console.log(`Running SearchIndexingBufferedSender-uploadDocuments-With Auto Flush Timer Sample`); | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
const searchClient: SearchClient<Hotel> = new SearchClient<Hotel>( | ||
endpoint, | ||
TEST_INDEX_NAME, | ||
credential | ||
); | ||
const indexClient: SearchIndexClient = new SearchIndexClient(endpoint, credential); | ||
|
||
await createIndex(indexClient, TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
|
||
const bufferedClient: SearchIndexingBufferedSender<Hotel> = searchClient.getSearchIndexingBufferedSenderInstance( | ||
{ | ||
autoFlush: true | ||
} | ||
); | ||
|
||
bufferedClient.on("batchAdded", (response: any) => { | ||
console.log("Batch Added Event has been receieved...."); | ||
}); | ||
|
||
bufferedClient.on("beforeDocumentSent", (response: any) => { | ||
console.log("Before Document Sent Event has been receieved...."); | ||
}); | ||
|
||
bufferedClient.on("batchSucceeded", (response: any) => { | ||
console.log("Batch Succeeded Event has been receieved...."); | ||
console.log(response); | ||
}); | ||
|
||
bufferedClient.on("batchFailed", (response: any) => { | ||
console.log("Batch Failed Event has been receieved...."); | ||
console.log(response); | ||
}); | ||
|
||
bufferedClient.uploadDocuments([ | ||
{ | ||
hotelId: "1", | ||
description: | ||
"Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa, " + | ||
"and a really helpful concierge. The location is perfect -- right downtown, close to all " + | ||
"the tourist attractions. We highly recommend this hotel.", | ||
descriptionFr: | ||
"Meilleur hôtel en ville si vous aimez les hôtels de luxe. Ils ont une magnifique piscine " + | ||
"à débordement, un spa et un concierge très utile. L'emplacement est parfait – en plein " + | ||
"centre, à proximité de toutes les attractions touristiques. Nous recommandons fortement " + | ||
"cet hôtel.", | ||
hotelName: "Fancy Stay", | ||
category: "Luxury", | ||
tags: ["pool", "view", "wifi", "concierge"], | ||
parkingIncluded: false, | ||
lastRenovationDate: new Date(2010, 5, 27), | ||
rating: 5, | ||
location: new GeographyPoint(47.678581, -122.131577) | ||
} | ||
]); | ||
|
||
const wait_time = DEFAULT_FLUSH_WINDOW + 5000; | ||
console.log(`Waiting for ${wait_time} ms to meet the flush window interval....`); | ||
await delay(wait_time); | ||
|
||
// When the autoFlush is set to true, the user | ||
// has to call the dispose method to clear the | ||
// timer. | ||
bufferedClient.dispose(); | ||
await indexClient.deleteIndex(TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
} | ||
|
||
main(); |
87 changes: 87 additions & 0 deletions
87
...rch/search-documents/samples/typescript/src/bufferedSender/uploadDocuments/manualFlush.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
SearchIndexingBufferedSender, | ||
AzureKeyCredential, | ||
SearchClient, | ||
GeographyPoint, | ||
SearchIndexClient | ||
} from "@azure/search-documents"; | ||
import { createIndex, WAIT_TIME } from "../../utils/setup"; | ||
import { Hotel } from "../../utils/interfaces"; | ||
import { delay } from "@azure/core-http"; | ||
|
||
/** | ||
* This sample is to demonstrate the use of SearchIndexingBufferedSender. | ||
* In this sample, the autoFlush is set to false. i.e. the user | ||
* wants to call the flush manually. | ||
*/ | ||
const endpoint = process.env.SEARCH_API_ENDPOINT || ""; | ||
const apiKey = process.env.SEARCH_API_KEY || ""; | ||
const TEST_INDEX_NAME = "hotel-live-sample-test1"; | ||
|
||
export async function main() { | ||
console.log(`Running SearchIndexingBufferedSender-uploadDocuments-Without AutoFlush Sample`); | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
const searchClient: SearchClient<Hotel> = new SearchClient<Hotel>( | ||
endpoint, | ||
TEST_INDEX_NAME, | ||
credential | ||
); | ||
const indexClient: SearchIndexClient = new SearchIndexClient(endpoint, credential); | ||
|
||
await createIndex(indexClient, TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
|
||
const bufferedClient: SearchIndexingBufferedSender<Hotel> = searchClient.getSearchIndexingBufferedSenderInstance( | ||
{ | ||
autoFlush: false | ||
} | ||
); | ||
|
||
bufferedClient.on("batchAdded", (response: any) => { | ||
console.log("Batch Added Event has been receieved...."); | ||
}); | ||
|
||
bufferedClient.on("beforeDocumentSent", (response: any) => { | ||
console.log("Before Document Sent Event has been receieved...."); | ||
}); | ||
|
||
bufferedClient.on("batchSucceeded", (response: any) => { | ||
console.log("Batch Succeeded Event has been receieved...."); | ||
console.log(response); | ||
}); | ||
|
||
bufferedClient.on("batchFailed", (response: any) => { | ||
console.log("Batch Failed Event has been receieved...."); | ||
console.log(response); | ||
}); | ||
|
||
bufferedClient.uploadDocuments([ | ||
{ | ||
hotelId: "1", | ||
description: | ||
"Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa, " + | ||
"and a really helpful concierge. The location is perfect -- right downtown, close to all " + | ||
"the tourist attractions. We highly recommend this hotel.", | ||
descriptionFr: | ||
"Meilleur hôtel en ville si vous aimez les hôtels de luxe. Ils ont une magnifique piscine " + | ||
"à débordement, un spa et un concierge très utile. L'emplacement est parfait – en plein " + | ||
"centre, à proximité de toutes les attractions touristiques. Nous recommandons fortement " + | ||
"cet hôtel.", | ||
hotelName: "Fancy Stay", | ||
category: "Luxury", | ||
tags: ["pool", "view", "wifi", "concierge"], | ||
parkingIncluded: false, | ||
lastRenovationDate: new Date(2010, 5, 27), | ||
rating: 5, | ||
location: new GeographyPoint(47.678581, -122.131577) | ||
} | ||
]); | ||
|
||
await bufferedClient.flush(); | ||
bufferedClient.dispose(); | ||
await indexClient.deleteIndex(TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
} | ||
|
||
main(); |
Oops, something went wrong.