From 3db0f1383f8d384f726ff9a836535a58298127f5 Mon Sep 17 00:00:00 2001 From: Adil Ansari Date: Tue, 11 Apr 2023 18:21:28 -0700 Subject: [PATCH] feat: dot notation for filters (#298) * default facet query type * facet query docs * changing filters to be of string types * tests --- src/__tests__/index.spec.ts | 1 - src/__tests__/tigris.filters.spec.ts | 14 +++++--------- src/__tests__/tigris.rpc.spec.ts | 4 ++-- src/__tests__/tigris.utility.spec.ts | 17 ++++++++--------- src/search/query.ts | 9 ++------- src/types.ts | 4 +++- src/utility.ts | 17 +++++++++-------- 7 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/__tests__/index.spec.ts b/src/__tests__/index.spec.ts index 70cd085..c878c67 100644 --- a/src/__tests__/index.spec.ts +++ b/src/__tests__/index.spec.ts @@ -30,7 +30,6 @@ const EXPECTED_EXPORTS = [ "DocStatus", "DropCollectionResponse", "FacetCount", - "FacetQueryFieldType", "FacetStats", "Field", "FindQueryOptions", diff --git a/src/__tests__/tigris.filters.spec.ts b/src/__tests__/tigris.filters.spec.ts index d183b99..1aab273 100644 --- a/src/__tests__/tigris.filters.spec.ts +++ b/src/__tests__/tigris.filters.spec.ts @@ -142,9 +142,7 @@ describe("filters tests", () => { id: BigInt(1), name: "alice", balance: 12.34, - address: { - city: "San Francisco", - }, + "address.city": "San Francisco", }, }; expect(Utility.filterToString(tigrisFilter)).toBe( @@ -166,9 +164,7 @@ describe("filters tests", () => { const tigrisFilter: SelectorFilter = { op: SelectorFilterOperator.LTE, fields: { - address: { - zipcode: 10, - }, + "address.zipcode": 10, }, }; expect(Utility.filterToString(tigrisFilter)).toBe('{"address.zipcode":{"$lte":10}}'); @@ -255,6 +251,7 @@ describe("filters tests", () => { }, }, { + // Selector filter on nested field as an object. Alternate: "address.city": "Paris" address: { city: "Paris", }, @@ -267,9 +264,8 @@ describe("filters tests", () => { { op: SelectorFilterOperator.GTE, fields: { - address: { - zipcode: 1200, - }, + // filter on nested field as dot notation + "address.zipcode": 1200, }, }, { diff --git a/src/__tests__/tigris.rpc.spec.ts b/src/__tests__/tigris.rpc.spec.ts index e34f489..c41c69e 100644 --- a/src/__tests__/tigris.rpc.spec.ts +++ b/src/__tests__/tigris.rpc.spec.ts @@ -508,7 +508,7 @@ describe("rpc tests", () => { const query: SearchQuery = { q: "philosophy", facets: { - tags: Utility.createFacetQueryOptions(), + tags: Utility.defaultFacetingOptions(), }, }; @@ -530,7 +530,7 @@ describe("rpc tests", () => { const query: SearchQuery = { q: "philosophy", facets: { - tags: Utility.createFacetQueryOptions(), + tags: Utility.defaultFacetingOptions(), }, }; let bookCounter = 0; diff --git a/src/__tests__/tigris.utility.spec.ts b/src/__tests__/tigris.utility.spec.ts index 271ce24..ea10e84 100644 --- a/src/__tests__/tigris.utility.spec.ts +++ b/src/__tests__/tigris.utility.spec.ts @@ -4,7 +4,6 @@ import { FacetFieldOptions, FacetFields, FacetFieldsQuery, - FacetQueryFieldType, MATCH_ALL_QUERY_STRING, SearchQuery, SearchQueryOptions, @@ -27,17 +26,17 @@ describe("utility tests", () => { }); it("generates default facet query options", () => { - const generatedOptions = Utility.createFacetQueryOptions(); + const generatedOptions = Utility.defaultFacetingOptions(); expect(generatedOptions.size).toBe(10); - expect(generatedOptions.type).toBe(FacetQueryFieldType.VALUE); + expect(generatedOptions.type).toBe("value"); }); it("backfills missing facet query options", () => { - const generatedOptions = Utility.createFacetQueryOptions({ + const generatedOptions = Utility.defaultFacetingOptions({ size: 55, }); expect(generatedOptions.size).toBe(55); - expect(generatedOptions.type).toBe(FacetQueryFieldType.VALUE); + expect(generatedOptions.type).toBe("value"); }); it("serializes FacetFields to string", () => { @@ -50,8 +49,8 @@ describe("utility tests", () => { it("serializes FacetFieldOptions to string", () => { const fields: FacetFieldOptions = { - field_1: Utility.createFacetQueryOptions(), - field_2: { size: 10, type: FacetQueryFieldType.VALUE }, + field_1: Utility.defaultFacetingOptions(), + field_2: { size: 10 }, }; const serialized: string = Utility.facetQueryToString(fields); expect(serialized).toBe( @@ -62,8 +61,8 @@ describe("utility tests", () => { it("equivalent serialization of FacetFieldsQuery", () => { const facetFields: FacetFieldsQuery = ["field_1", "field_2"]; const fieldOptions: FacetFieldsQuery = { - field_1: Utility.createFacetQueryOptions(), - field_2: { size: 10, type: FacetQueryFieldType.VALUE }, + field_1: Utility.defaultFacetingOptions(), + field_2: { size: 10, type: "value" }, }; const serializedFields = Utility.facetQueryToString(facetFields); expect(serializedFields).toBe(Utility.facetQueryToString(fieldOptions)); diff --git a/src/search/query.ts b/src/search/query.ts index 04dd64a..e39b228 100644 --- a/src/search/query.ts +++ b/src/search/query.ts @@ -75,25 +75,20 @@ export type FacetFields = Array; /** * Information to build facets in search results - * Use `Utility.createFacetQueryOptions()` to generate using defaults * - * @see {@link Utility.createFacetQueryOptions} */ export type FacetQueryOptions = { /** * Maximum number of facets to include in results + * default - 10 */ size: number; /** * Type of facets to build */ - type: FacetQueryFieldType; + type?: "value"; }; -export enum FacetQueryFieldType { - VALUE = "value", -} - export enum Case { /** * Case insensitive collation case diff --git a/src/types.ts b/src/types.ts index 8936168..95bbad5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -683,6 +683,7 @@ type Paths = { /** * This type helps to infer the type of the path that Paths (above) has generated. */ +// eslint-disable-next-line @typescript-eslint/no-unused-vars type PathType = P extends keyof T ? T[P] : P extends `${infer L}.${infer R}` @@ -691,8 +692,9 @@ type PathType = P extends keyof T : never : never; +// eslint-disable-next-line @typescript-eslint/no-unused-vars export type Selector = Partial<{ - [K in Paths]: Partial>; + [K in string]: unknown; }>; export type SelectorFilter = Partial<{ diff --git a/src/utility.ts b/src/utility.ts index 38af012..388c267 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -30,7 +30,6 @@ import { import { TigrisClientConfig } from "./tigris"; import { FacetFieldsQuery, - FacetQueryFieldType, FacetQueryOptions, MATCH_ALL_QUERY_STRING, SearchQuery, @@ -536,21 +535,23 @@ export const Utility = { return this.jsonStringToObj(Buffer.from(b64String, "base64").toString("utf8"), config); }, - createFacetQueryOptions(options?: Partial): FacetQueryOptions { - const defaults = { size: 10, type: FacetQueryFieldType.VALUE }; + defaultFacetingOptions(options?: Partial): FacetQueryOptions { + const defaults: FacetQueryOptions = { size: 10, type: "value" }; return { ...defaults, ...options }; }, facetQueryToString(facets: FacetFieldsQuery): string { + const optionsMap = {}; if (Array.isArray(facets)) { - const optionsMap = {}; for (const f of facets) { - optionsMap[f] = this.createFacetQueryOptions(); + optionsMap[f] = this.defaultFacetingOptions(); + } + } else if (typeof facets === "object") { + for (const f in facets) { + optionsMap[f] = this.defaultFacetingOptions(facets[f]); } - return this.objToJsonString(optionsMap); - } else { - return this.objToJsonString(facets); } + return this.objToJsonString(optionsMap); }, _vectorQueryToString(q: VectorQuery): string {