Skip to content

Commit

Permalink
Feature/full text functionality (#31703)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR


### Issues associated with this PR


### Describe the problem that is addressed by this PR


### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)

---------

Co-authored-by: Manik Khandelwal <[email protected]>
Co-authored-by: Ujjwal Soni <[email protected]>
  • Loading branch information
3 people authored Nov 10, 2024
1 parent 636e745 commit b968275
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class GlobalStatisticsAggregator implements Aggregator {
};
}

public aggregate(other: GlobalStatistics) {
public aggregate(other: GlobalStatistics): void {
if (!other) {
return;
}
Expand Down Expand Up @@ -60,7 +60,7 @@ export class GlobalStatisticsAggregator implements Aggregator {
}
}

public getResult() {
public getResult(): GlobalStatistics {
return this.globalStatistics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class HybridQueryExecutionContext implements ExecutionContext {
const globalStaticsQueryOptions: FeedOptions = { maxItemCount: this.pageSize };
this.globalStatisticsAggregator = new GlobalStatisticsAggregator();

let globalStatisticsQuery =
const globalStatisticsQuery =
this.partitionedQueryExecutionInfo.hybridSearchQueryInfo.globalStatisticsQuery;
const globalStatisticsQueryExecutionInfo: PartitionedQueryExecutionInfo = {
partitionedQueryExecutionInfoVersion: 1,
Expand Down Expand Up @@ -81,7 +81,7 @@ export class HybridQueryExecutionContext implements ExecutionContext {
}
}
public async nextItem(diagnosticNode: DiagnosticNodeInternal): Promise<Response<any>> {
let nextItemRespHeaders = getInitialHeader();
const nextItemRespHeaders = getInitialHeader();
while (
(this.state === HybridQueryExecutionContextBaseStates.uninitialized ||
this.state === HybridQueryExecutionContextBaseStates.initialized) &&
Expand Down Expand Up @@ -113,8 +113,8 @@ export class HybridQueryExecutionContext implements ExecutionContext {
}

public async fetchMore(diagnosticNode: DiagnosticNodeInternal): Promise<Response<any>> {
let fetchMoreRespHeaders = getInitialHeader();
return await this.fetchMoreInternal(diagnosticNode, fetchMoreRespHeaders);
const fetchMoreRespHeaders = getInitialHeader();
return this.fetchMoreInternal(diagnosticNode, fetchMoreRespHeaders);
}

private async fetchMoreInternal(
Expand All @@ -136,8 +136,7 @@ export class HybridQueryExecutionContext implements ExecutionContext {
headers: headers,
};
case HybridQueryExecutionContextBaseStates.draining:
const result = await this.drain(headers);
return result;
return this.drain(headers);
case HybridQueryExecutionContextBaseStates.done:
return this.done(headers);
default:
Expand All @@ -155,7 +154,7 @@ export class HybridQueryExecutionContext implements ExecutionContext {
const globalStatistics: GlobalStatistics = result.result;
mergeHeaders(fetchMoreRespHeaders, result.headers);
if (globalStatistics) {
//iterate over the components update placeholders from globalStatistics
// iterate over the components update placeholders from globalStatistics
this.globalStatisticsAggregator.aggregate(globalStatistics);
}
}
Expand Down Expand Up @@ -217,7 +216,7 @@ export class HybridQueryExecutionContext implements ExecutionContext {
}
}

private applySkipAndTakeToBuffer() {
private applySkipAndTakeToBuffer(): void {
const { skip, take } = this.partitionedQueryExecutionInfo.hybridSearchQueryInfo;
if (skip) {
this.buffer = this.buffer.slice(skip);
Expand Down Expand Up @@ -276,7 +275,9 @@ export class HybridQueryExecutionContext implements ExecutionContext {
};
}

private sortHybridSearchResultByRRFScore(hybridSearchResult: HybridSearchQueryResult[]) {
private sortHybridSearchResultByRRFScore(
hybridSearchResult: HybridSearchQueryResult[],
): HybridSearchQueryResult[] {
const ranksArray: { rid: string; ranks: number[] }[] = hybridSearchResult.map((item) => ({
rid: item.rid,
ranks: new Array(item.componentScores.length).fill(0),
Expand Down
19 changes: 11 additions & 8 deletions sdk/cosmosdb/cosmos/src/request/hybridSearchQueryResult.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { ItemDefinition } from "../client";

const FieldNames = {
Rid: "_rid",
Payload: "payload",
ComponentScores: "componentScores",
};

export class HybridSearchQueryResult {
rid: string;
componentScores: number[];
data: any;
score: number;
ranks: number[];

constructor(rid: string, componentScores: number[], data: any) {
constructor(rid: string, componentScores: number[], data: Record<string, unknown>) {
this.rid = rid;
this.componentScores = componentScores;
this.data = data;
}

public static create(document: any): HybridSearchQueryResult {
public static create<T extends ItemDefinition>(document: T): HybridSearchQueryResult {
const rid = document[FieldNames.Rid];
if (!rid) {
throw new Error(`${FieldNames.Rid} must exist.`);
Expand All @@ -37,9 +46,3 @@ export class HybridSearchQueryResult {
return new HybridSearchQueryResult(rid, componentScores, innerPayload);
}
}

class FieldNames {
public static readonly Rid = "_rid";
public static readonly Payload = "payload";
public static readonly ComponentScores = "componentScores";
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ async function executeQueryAndVerifyOrder(
assert.equal(count, size);
}

describe("Full text search feature", async () => {
describe.skip("Full text search feature", async () => {
let database: Database;

before(async function () {
Expand Down Expand Up @@ -527,7 +527,7 @@ describe("Full text search feature", async () => {

const queryOptions = { forceQueryPlan: true };
const queryIterator = container.items.query(query, queryOptions);
let result = [];
const result = [];
while (queryIterator.hasMoreResults()) {
result.push(...(await queryIterator.fetchNext()).resources);
}
Expand Down
74 changes: 2 additions & 72 deletions sdk/cosmosdb/cosmos/test/public/integration/fullTextSearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ContainerDefinition, Container } from "../../../src";
import items from "./text-3properties-1536dimensions-100documents";
import { getTestContainer, removeAllDatabases } from "../common/TestHelpers";

describe("Validate full text search queries", function (this: Suite) {
describe.skip("Validate full text search queries", function (this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 20000);

const partitionKey = "id";
Expand Down Expand Up @@ -70,76 +70,8 @@ describe("Validate full text search queries", function (this: Suite) {
WHERE FullTextContains(c.title, 'John') OR FullTextContains(c.text, 'John') OR FullTextContains(c.text, 'United States')
ORDER BY RANK RRF(FullTextScore(c.title, ['John']), FullTextScore(c.text, ['United States']))`,
{
expected1: [61, 51, 49, 54, 75, 24, 77, 76, 80, 25, 22, 2, 66, 57, 85], // [ 61, 75, 54, 80, 2, 51, 49, 76, 77, 24, 66, 22, 57, 25, 85]
expected1: [61, 51, 49, 54, 75, 24, 77, 76, 80, 25, 22, 2, 66, 57, 85],
expected2: [61, 51, 49, 54, 75, 24, 77, 76, 80, 25, 22, 2, 66, 85, 57],

/**
*
* rrfScores array [
{ rid: '7oh8AIMy9ekIAAAAAAAAAg==', rrfScore: 0.03252247488101534 },
{ rid: '7oh8AIMy9ekUAAAAAAAADA==', rrfScore: 0.031009615384615385 },
{ rid: '7oh8AIMy9ekPAAAAAAAADA==', rrfScore: 0.031009615384615385 },
{ rid: '7oh8AIMy9ekUAAAAAAAABA==', rrfScore: 0.03036576949620428 },
{ rid: '7oh8AIMy9ekBAAAAAAAAAA==', rrfScore: 0.03009207275993712 },
{ rid: '7oh8AIMy9ekMAAAAAAAAAA==', rrfScore: 0.030017921146953404 },
{ rid: '7oh8AIMy9ekIAAAAAAAACA==', rrfScore: 0.029957522915269395 },
{ rid: '7oh8AIMy9ekPAAAAAAAAAA==', rrfScore: 0.029857397504456328 },
{ rid: '7oh8AIMy9ekTAAAAAAAABA==', rrfScore: 0.029850746268656716 },
{ rid: '7oh8AIMy9ekHAAAAAAAADA==', rrfScore: 0.028665028665028666 },
{ rid: '7oh8AIMy9ekJAAAAAAAAAg==', rrfScore: 0.028594771241830064 },
{ rid: '7oh8AIMy9ekGAAAAAAAADA==', rrfScore: 0.028577260665441927 },
{ rid: '7oh8AIMy9ekNAAAAAAAAAA==', rrfScore: 0.027799227799227798 },
{ rid: '7oh8AIMy9ekIAAAAAAAAAA==', rrfScore: 0.02761904761904762 },
{ rid: '7oh8AIMy9ekNAAAAAAAAAg==', rrfScore: 0.027031963470319637 }
]
[
75, 77, 22, 51, 2, 61,
80, 24, 49, 25, 76, 54,
57, 85, 66
]
rrfScores array [
{ rid: 'JfY-AKTpHRQNAAAAAAAAAA==', rrfScore: 0.03125763125763126 },
{ rid: 'JfY-AKTpHRQUAAAAAAAABA==', rrfScore: 0.03055037313432836 },
{ rid: 'JfY-AKTpHRQEAAAAAAAAAg==', rrfScore: 0.03021353930031804 },
{ rid: 'JfY-AKTpHRQNAAAAAAAACA==', rrfScore: 0.03021353930031804 },
{ rid: 'JfY-AKTpHRQBAAAAAAAABA==', rrfScore: 0.03009207275993712 },
{ rid: 'JfY-AKTpHRQLAAAAAAAAAg==', rrfScore: 0.03009207275993712 },
{ rid: 'JfY-AKTpHRQRAAAAAAAADA==', rrfScore: 0.02964426877470356 },
{ rid: 'JfY-AKTpHRQCAAAAAAAADA==', rrfScore: 0.02964426877470356 },
{ rid: 'JfY-AKTpHRQIAAAAAAAADA==', rrfScore: 0.029386529386529386 },
{ rid: 'JfY-AKTpHRQFAAAAAAAAAg==', rrfScore: 0.028991596638655463 },
{ rid: 'JfY-AKTpHRQPAAAAAAAADA==', rrfScore: 0.028991596638655463 },
{ rid: 'JfY-AKTpHRQJAAAAAAAADA==', rrfScore: 0.028958333333333336 },
{ rid: 'JfY-AKTpHRQNAAAAAAAABA==', rrfScore: 0.0288981288981289 },
{ rid: 'JfY-AKTpHRQWAAAAAAAABA==', rrfScore: 0.028258706467661692 },
{ rid: 'JfY-AKTpHRQMAAAAAAAADA==', rrfScore: 0.027777777777777776 }
]
[
75, 51, 80, 77, 2, 49,
61, 24, 22, 54, 66, 76,
57, 25, 85
]rrfScores array [
{ rid: 'CeBWAKZEhGkVAAAAAAAABA==', rrfScore: 0.03125763125763126 },
{ rid: 'CeBWAKZEhGkNAAAAAAAABA==', rrfScore: 0.031054405392392875 },
{ rid: 'CeBWAKZEhGkPAAAAAAAADA==', rrfScore: 0.030621785881252923 },
{ rid: 'CeBWAKZEhGkOAAAAAAAADA==', rrfScore: 0.030309988518943745 },
{ rid: 'CeBWAKZEhGkCAAAAAAAACA==', rrfScore: 0.03009207275993712 },
{ rid: 'CeBWAKZEhGkHAAAAAAAAAA==', rrfScore: 0.029957522915269395 },
{ rid: 'CeBWAKZEhGkNAAAAAAAADA==', rrfScore: 0.029906956136464335 },
{ rid: 'CeBWAKZEhGkGAAAAAAAAAg==', rrfScore: 0.029857397504456328 },
{ rid: 'CeBWAKZEhGkFAAAAAAAAAg==', rrfScore: 0.029709507042253523 },
{ rid: 'CeBWAKZEhGkIAAAAAAAAAA==', rrfScore: 0.029513888888888888 },
{ rid: 'CeBWAKZEhGkKAAAAAAAAAA==', rrfScore: 0.02904040404040404 },
{ rid: 'CeBWAKZEhGkJAAAAAAAACA==', rrfScore: 0.02803921568627451 },
{ rid: 'CeBWAKZEhGkGAAAAAAAACA==', rrfScore: 0.028006267136701922 },
{ rid: 'CeBWAKZEhGkHAAAAAAAAAg==', rrfScore: 0.027984344422700584 },
{ rid: 'CeBWAKZEhGkNAAAAAAAAAA==', rrfScore: 0.02761904761904762 }
]
*/
},
],
[
Expand Down Expand Up @@ -208,14 +140,12 @@ rrfScores array [
const results: any[] = [];
while (queryIterator.hasMoreResults()) {
const { resources: result } = await queryIterator.fetchNext();
console.log("fetchNext result - final", result);
if (result !== undefined) {
results.push(...result);
}
}

const indexes = results.map((result) => result.Index);
console.log("indexes", indexes);
const isMatch =
JSON.stringify(indexes) === JSON.stringify(expected1) ||
JSON.stringify(indexes) === JSON.stringify(expected2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/* eslint-disable no-loss-of-precision */
const items = [
{
title: "Parabolic reflector",
Expand Down

0 comments on commit b968275

Please sign in to comment.