-
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.
Add sample for sticky query sessions
- Loading branch information
Showing
5 changed files
with
273 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* @summary Demonstrates user sticky sessions, a way to reduce inconsistent behavior by targeting a | ||
* single replica. | ||
*/ | ||
|
||
import { | ||
AzureKeyCredential, | ||
SearchClient, | ||
SearchIndexClient, | ||
odata, | ||
} from "@azure/search-documents"; | ||
import { createIndex, WAIT_TIME, delay } from "./setup"; | ||
import { Hotel } from "./interfaces"; | ||
|
||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
/** | ||
* If you're querying a replicated index, Azure AI Search may target any replica with your queries. | ||
* As these replicas may not be in a consistent state, the service may appear to have inconsistent | ||
* states between distinct queries. To avoid this, you can use a sticky session. A sticky session | ||
* is used to indicate to the Azure AI Search service that you'd like all requests with the same | ||
* `sessionId` to be directed to the same replica. The service will then make a best effort to do | ||
* so. | ||
* | ||
* Please see the | ||
* {@link https://learn.microsoft.com/en-us/azure/search/index-similarity-and-scoring#scoring-statistics-and-sticky-sessions | documentation} | ||
* for more information. | ||
*/ | ||
const endpoint = process.env.ENDPOINT || ""; | ||
const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; | ||
const TEST_INDEX_NAME = "example-index-sample-3"; | ||
|
||
async function main(): Promise<void> { | ||
if (!endpoint || !apiKey) { | ||
console.error( | ||
"Be sure to set valid values for `endpoint` and `apiKey` with proper authorization.", | ||
); | ||
return; | ||
} | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
const indexClient: SearchIndexClient = new SearchIndexClient(endpoint, credential); | ||
const searchClient: SearchClient<Hotel> = indexClient.getSearchClient<Hotel>(TEST_INDEX_NAME); | ||
|
||
// The session id is defined by the user. | ||
const sessionId = "session1"; | ||
|
||
try { | ||
await createIndex(indexClient, TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
|
||
// The service will make a best effort attempt to direct these queries to the same replica. As | ||
// this overrides load balancing, excessive use of the same `sessionId` may result in | ||
// performance degradation. Be sure to use a distinct `sessionId` for each sticky session. | ||
const ratingQueries = [2, 4]; | ||
for (const rating of ratingQueries) { | ||
const response = await searchClient.search("*", { | ||
filter: odata`rating ge ${rating}`, | ||
sessionId, | ||
}); | ||
|
||
const hotelNames = []; | ||
for await (const result of response.results) { | ||
const hotelName = result.document.hotelName; | ||
if (typeof hotelName === "string") { | ||
hotelNames.push(hotelName); | ||
} | ||
} | ||
|
||
if (hotelNames.length) { | ||
console.log(`Hotels with at least a rating of ${rating}:`); | ||
hotelNames.forEach(console.log); | ||
} | ||
} | ||
} finally { | ||
await indexClient.deleteIndex(TEST_INDEX_NAME); | ||
} | ||
} | ||
|
||
main(); |
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
77 changes: 77 additions & 0 deletions
77
sdk/search/search-documents/samples/v12-beta/javascript/stickySession.js
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,77 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* @summary Demonstrates user sticky sessions, a way to reduce inconsistent behavior by targeting a | ||
* single replica. | ||
*/ | ||
|
||
const { AzureKeyCredential, SearchIndexClient, odata } = require("@azure/search-documents"); | ||
const { createIndex, WAIT_TIME, delay } = require("./setup"); | ||
|
||
require("dotenv").config(); | ||
|
||
/** | ||
* If you're querying a replicated index, Azure AI Search may target any replica with your queries. | ||
* As these replicas may not be in a consistent state, the service may appear to have inconsistent | ||
* states between distinct queries. To avoid this, you can use a sticky session. A sticky session | ||
* is used to indicate to the Azure AI Search service that you'd like all requests with the same | ||
* `sessionId` to be directed to the same replica. The service will then make a best effort to do | ||
* so. | ||
* | ||
* Please see the | ||
* {@link https://learn.microsoft.com/en-us/azure/search/index-similarity-and-scoring#scoring-statistics-and-sticky-sessions | documentation} | ||
* for more information. | ||
*/ | ||
const endpoint = process.env.ENDPOINT || ""; | ||
const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; | ||
const TEST_INDEX_NAME = "example-index-sample-3"; | ||
|
||
async function main() { | ||
if (!endpoint || !apiKey) { | ||
console.error( | ||
"Be sure to set valid values for `endpoint` and `apiKey` with proper authorization.", | ||
); | ||
return; | ||
} | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
const indexClient = new SearchIndexClient(endpoint, credential); | ||
const searchClient = indexClient.getSearchClient(TEST_INDEX_NAME); | ||
|
||
// The session id is defined by the user. | ||
const sessionId = "session1"; | ||
|
||
try { | ||
await createIndex(indexClient, TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
|
||
// The service will make a best effort attempt to direct these queries to the same replica. As | ||
// this overrides load balancing, excessive use of the same `sessionId` may result in | ||
// performance degradation. Be sure to use a distinct `sessionId` for each sticky session. | ||
const ratingQueries = [2, 4]; | ||
for (const rating of ratingQueries) { | ||
const response = await searchClient.search("*", { | ||
filter: odata`rating ge ${rating}`, | ||
sessionId, | ||
}); | ||
|
||
const hotelNames = []; | ||
for await (const result of response.results) { | ||
const hotelName = result.document.hotelName; | ||
if (typeof hotelName === "string") { | ||
hotelNames.push(hotelName); | ||
} | ||
} | ||
|
||
if (hotelNames.length) { | ||
console.log(`Hotels with at least a rating of ${rating}:`); | ||
hotelNames.forEach(console.log); | ||
} | ||
} | ||
} finally { | ||
await indexClient.deleteIndex(TEST_INDEX_NAME); | ||
} | ||
} | ||
|
||
main(); |
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
84 changes: 84 additions & 0 deletions
84
sdk/search/search-documents/samples/v12-beta/typescript/src/stickySession.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,84 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* @summary Demonstrates user sticky sessions, a way to reduce inconsistent behavior by targeting a | ||
* single replica. | ||
*/ | ||
|
||
import { | ||
AzureKeyCredential, | ||
SearchClient, | ||
SearchIndexClient, | ||
odata, | ||
} from "@azure/search-documents"; | ||
import { createIndex, WAIT_TIME, delay } from "./setup"; | ||
import { Hotel } from "./interfaces"; | ||
|
||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
/** | ||
* If you're querying a replicated index, Azure AI Search may target any replica with your queries. | ||
* As these replicas may not be in a consistent state, the service may appear to have inconsistent | ||
* states between distinct queries. To avoid this, you can use a sticky session. A sticky session | ||
* is used to indicate to the Azure AI Search service that you'd like all requests with the same | ||
* `sessionId` to be directed to the same replica. The service will then make a best effort to do | ||
* so. | ||
* | ||
* Please see the | ||
* {@link https://learn.microsoft.com/en-us/azure/search/index-similarity-and-scoring#scoring-statistics-and-sticky-sessions | documentation} | ||
* for more information. | ||
*/ | ||
const endpoint = process.env.ENDPOINT || ""; | ||
const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; | ||
const TEST_INDEX_NAME = "example-index-sample-3"; | ||
|
||
async function main(): Promise<void> { | ||
if (!endpoint || !apiKey) { | ||
console.error( | ||
"Be sure to set valid values for `endpoint` and `apiKey` with proper authorization.", | ||
); | ||
return; | ||
} | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
const indexClient: SearchIndexClient = new SearchIndexClient(endpoint, credential); | ||
const searchClient: SearchClient<Hotel> = indexClient.getSearchClient<Hotel>(TEST_INDEX_NAME); | ||
|
||
// The session id is defined by the user. | ||
const sessionId = "session1"; | ||
|
||
try { | ||
await createIndex(indexClient, TEST_INDEX_NAME); | ||
await delay(WAIT_TIME); | ||
|
||
// The service will make a best effort attempt to direct these queries to the same replica. As | ||
// this overrides load balancing, excessive use of the same `sessionId` may result in | ||
// performance degradation. Be sure to use a distinct `sessionId` for each sticky session. | ||
const ratingQueries = [2, 4]; | ||
for (const rating of ratingQueries) { | ||
const response = await searchClient.search("*", { | ||
filter: odata`rating ge ${rating}`, | ||
sessionId, | ||
}); | ||
|
||
const hotelNames = []; | ||
for await (const result of response.results) { | ||
const hotelName = result.document.hotelName; | ||
if (typeof hotelName === "string") { | ||
hotelNames.push(hotelName); | ||
} | ||
} | ||
|
||
if (hotelNames.length) { | ||
console.log(`Hotels with at least a rating of ${rating}:`); | ||
hotelNames.forEach(console.log); | ||
} | ||
} | ||
} finally { | ||
await indexClient.deleteIndex(TEST_INDEX_NAME); | ||
} | ||
} | ||
|
||
main(); |