Skip to content

Commit

Permalink
add geoshape support (#2788)
Browse files Browse the repository at this point in the history
copied from what leibele did for v5
  • Loading branch information
sjpotter authored Jul 10, 2024
1 parent 7d43a97 commit a1bee1c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
38 changes: 37 additions & 1 deletion packages/search/lib/commands/CREATE.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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(
Expand Down
24 changes: 22 additions & 2 deletions packages/search/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ export enum SchemaFieldTypes {
NUMERIC = 'NUMERIC',
GEO = 'GEO',
TAG = 'TAG',
VECTOR = 'VECTOR'
VECTOR = 'VECTOR',
GEOSHAPE = 'GEOSHAPE'
}

type CreateSchemaField<
Expand Down Expand Up @@ -257,14 +258,26 @@ type CreateSchemaHNSWVectorField = CreateSchemaVectorField<VectorAlgorithms.HNSW
EF_RUNTIME?: number;
}>;

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<SchemaFieldTypes.GEOSHAPE, {
COORD_SYSTEM?: SchemaGeoShapeFieldCoordSystem;
}>;

export interface RediSearchSchema {
[field: string]:
CreateSchemaTextField |
CreateSchemaNumericField |
CreateSchemaGeoField |
CreateSchemaTagField |
CreateSchemaFlatVectorField |
CreateSchemaHNSWVectorField;
CreateSchemaHNSWVectorField |
CreateSchemaGeoShapeField
}

export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a1bee1c

Please sign in to comment.