Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add geoshape support #2788

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading