From 3574a687bbfd5e24cf17ca10a3a1053052c438f5 Mon Sep 17 00:00:00 2001 From: Shaya Potter Date: Wed, 10 Jul 2024 15:53:50 +0300 Subject: [PATCH] add geoshape support copied from what leibele did for v5 --- packages/search/lib/commands/CREATE.spec.ts | 38 ++++++++++++++++++++- packages/search/lib/commands/index.ts | 24 +++++++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/packages/search/lib/commands/CREATE.spec.ts b/packages/search/lib/commands/CREATE.spec.ts index 1a0a4f244bd..31eaf02dbba 100644 --- a/packages/search/lib/commands/CREATE.spec.ts +++ b/packages/search/lib/commands/CREATE.spec.ts @@ -1,7 +1,7 @@ import { strict as assert } from 'assert'; import testUtils, { GLOBAL } from '../test-utils'; import { transformArguments } from './CREATE'; -import { SchemaFieldTypes, SchemaTextFieldPhonetics, RedisSearchLanguages, VectorAlgorithms } from '.'; +import { SchemaFieldTypes, SchemaTextFieldPhonetics, RedisSearchLanguages, VectorAlgorithms, SCHEMA_GEO_SHAPE_COORD_SYSTEM } from '.'; describe('CREATE', () => { describe('transformArguments', () => { @@ -196,6 +196,42 @@ describe('CREATE', () => { }); }); + describe('GEOSHAPE', () => { + describe('without options', () => { + it('SCHEMA_FIELD_TYPE.GEOSHAPE', () => { + assert.deepEqual( + transformArguments('index', { + field: SchemaFieldTypes.GEOSHAPE + }), + ['FT.CREATE', 'index', 'SCHEMA', 'field', 'GEOSHAPE'] + ); + }); + + it('{ type: SCHEMA_FIELD_TYPE.GEOSHAPE }', () => { + assert.deepEqual( + transformArguments('index', { + field: { + type: SchemaFieldTypes.GEOSHAPE + } + }), + ['FT.CREATE', 'index', 'SCHEMA', 'field', 'GEOSHAPE'] + ); + }); + }); + + it('with COORD_SYSTEM', () => { + assert.deepEqual( + transformArguments('index', { + field: { + type: SchemaFieldTypes.GEOSHAPE, + COORD_SYSTEM: SCHEMA_GEO_SHAPE_COORD_SYSTEM.SPHERICAL + } + }), + ['FT.CREATE', 'index', 'SCHEMA', 'field', 'GEOSHAPE', 'COORD_SYSTEM', 'SPHERICAL'] + ); + }); + }); + describe('with generic options', () => { it('with AS', () => { assert.deepEqual( diff --git a/packages/search/lib/commands/index.ts b/packages/search/lib/commands/index.ts index 75a2b4e380a..053e2fab38f 100644 --- a/packages/search/lib/commands/index.ts +++ b/packages/search/lib/commands/index.ts @@ -185,7 +185,8 @@ export enum SchemaFieldTypes { NUMERIC = 'NUMERIC', GEO = 'GEO', TAG = 'TAG', - VECTOR = 'VECTOR' + VECTOR = 'VECTOR', + GEOSHAPE = 'GEOSHAPE' } type CreateSchemaField< @@ -257,6 +258,17 @@ type CreateSchemaHNSWVectorField = CreateSchemaVectorField; +export const SCHEMA_GEO_SHAPE_COORD_SYSTEM = { + SPHERICAL: 'SPHERICAL', + FLAT: 'FLAT' +} as const; + +export type SchemaGeoShapeFieldCoordSystem = typeof SCHEMA_GEO_SHAPE_COORD_SYSTEM[keyof typeof SCHEMA_GEO_SHAPE_COORD_SYSTEM]; + +type CreateSchemaGeoShapeField = CreateSchemaCommonField; + export interface RediSearchSchema { [field: string]: CreateSchemaTextField | @@ -264,7 +276,8 @@ export interface RediSearchSchema { CreateSchemaGeoField | CreateSchemaTagField | CreateSchemaFlatVectorField | - CreateSchemaHNSWVectorField; + CreateSchemaHNSWVectorField | + CreateSchemaGeoShapeField } export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema) { @@ -361,6 +374,13 @@ export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema }); continue; // vector fields do not contain SORTABLE and NOINDEX options + + case SchemaFieldTypes.GEOSHAPE: + if (fieldOptions.COORD_SYSTEM !== undefined) { + args.push('COORD_SYSTEM', fieldOptions.COORD_SYSTEM); + } + + continue; // geo shape fields do not contain SORTABLE and NOINDEX options } if (fieldOptions.SORTABLE) {