Skip to content

Commit

Permalink
small refactor per discussion with leibele (#2801)
Browse files Browse the repository at this point in the history
* small refactor per discussion with leibele

* move true type to boolean type

* fix geoshape to support NOINDEX & SORTABLE, clean code

* fix for last commit

---------

Co-authored-by: Leibale Eidelman <[email protected]>
  • Loading branch information
sjpotter and leibale authored Jul 29, 2024
1 parent 179b9e0 commit 840ec57
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
66 changes: 36 additions & 30 deletions packages/search/lib/commands/CREATE.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ describe('CREATE', () => {
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TEXT', 'WITHSUFFIXTRIE']
);
});

it('with INDEXEMPTY', () => {
assert.deepEqual(
transformArguments('index', {
field: {
type: SchemaFieldTypes.TEXT,
INDEXEMPTY: true
}
}),
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TEXT', 'INDEXEMPTY']
);
});
});

it('NUMERIC', () => {
Expand Down Expand Up @@ -148,6 +160,18 @@ describe('CREATE', () => {
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TAG', 'WITHSUFFIXTRIE']
);
});

it('with INDEXEMPTY', () => {
assert.deepEqual(
transformArguments('index', {
field: {
type: SchemaFieldTypes.TAG,
INDEXEMPTY: true
}
}),
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TAG', 'INDEXEMPTY']
);
});
});

describe('VECTOR', () => {
Expand Down Expand Up @@ -282,6 +306,18 @@ describe('CREATE', () => {
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TEXT', 'NOINDEX']
);
});

it('with INDEXMISSING', () => {
assert.deepEqual(
transformArguments('index', {
field: {
type: SchemaFieldTypes.TEXT,
INDEXMISSING: true
}
}),
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TEXT', 'INDEXMISSING']
);
});
});
});

Expand Down Expand Up @@ -441,36 +477,6 @@ describe('CREATE', () => {
);
});
});

describe('Missing Values', () => {
it('with INDEXEMPTY', () => {
assert.deepEqual(
transformArguments('index', {
field: {
type: SchemaFieldTypes.TEXT,
MISSING_VALUES: {
INDEXEMPTY: true
}
}
}),
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TEXT', 'INDEXEMPTY']
);
});

it('with INDEXMISSING', () => {
assert.deepEqual(
transformArguments('index', {
field: {
type: SchemaFieldTypes.TEXT,
MISSING_VALUES: {
INDEXMISSING: true
}
}
}),
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TEXT', 'INDEXMISSING']
);
});
});
});

testUtils.testWithClient('client.ft.create', async client => {
Expand Down
83 changes: 40 additions & 43 deletions packages/search/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,46 +188,43 @@ export enum SchemaFieldTypes {
VECTOR = 'VECTOR',
GEOSHAPE = 'GEOSHAPE'
}

export interface MissingValues {
INDEXEMPTY?: boolean;
INDEXMISSING?: boolean;
}

function pushMissingValues(args: RedisCommandArguments, missingValues?: MissingValues) {
if (!missingValues) {
return;
}

if (missingValues.INDEXEMPTY) {
args.push("INDEXEMPTY");
}

if (missingValues.INDEXMISSING) {
args.push("INDEXMISSING");
}
}

type CreateSchemaField<
T extends SchemaFieldTypes,
E = Record<PropertyKey, unknown>
> = T | ({
type: T;
AS?: string;
MISSING_VALUES?: MissingValues;
INDEXMISSING?: boolean;
} & E);

type CommonFieldArguments = {
SORTABLE?: boolean | 'UNF';
NOINDEX?: boolean;
};

type CreateSchemaCommonField<
T extends SchemaFieldTypes,
E = Record<PropertyKey, unknown>
> = CreateSchemaField<
T,
({
SORTABLE?: true | 'UNF';
NOINDEX?: true;
} & E)
(CommonFieldArguments & E)
>;

function pushCommonFieldArguments(args: RedisCommandArguments, fieldOptions: CommonFieldArguments) {
if (fieldOptions.SORTABLE) {
args.push('SORTABLE');

if (fieldOptions.SORTABLE === 'UNF') {
args.push('UNF');
}
}

if (fieldOptions.NOINDEX) {
args.push('NOINDEX');
}
}

export enum SchemaTextFieldPhonetics {
DM_EN = 'dm:en',
DM_FR = 'dm:fr',
Expand All @@ -240,6 +237,7 @@ type CreateSchemaTextField = CreateSchemaCommonField<SchemaFieldTypes.TEXT, {
WEIGHT?: number;
PHONETIC?: SchemaTextFieldPhonetics;
WITHSUFFIXTRIE?: boolean;
INDEXEMPTY?: boolean;
}>;

type CreateSchemaNumericField = CreateSchemaCommonField<SchemaFieldTypes.NUMERIC>;
Expand All @@ -250,6 +248,7 @@ type CreateSchemaTagField = CreateSchemaCommonField<SchemaFieldTypes.TAG, {
SEPARATOR?: string;
CASESENSITIVE?: true;
WITHSUFFIXTRIE?: boolean;
INDEXEMPTY?: boolean;
}>;

export enum VectorAlgorithms {
Expand Down Expand Up @@ -297,7 +296,7 @@ export interface RediSearchSchema {
CreateSchemaTagField |
CreateSchemaFlatVectorField |
CreateSchemaHNSWVectorField |
CreateSchemaGeoShapeField
CreateSchemaGeoShapeField;
}

export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema) {
Expand Down Expand Up @@ -333,13 +332,17 @@ export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema
args.push('WITHSUFFIXTRIE');
}

pushMissingValues(args, fieldOptions.MISSING_VALUES);
pushCommonFieldArguments(args, fieldOptions);

if (fieldOptions.INDEXEMPTY) {
args.push('INDEXEMPTY');
}

break;

case SchemaFieldTypes.NUMERIC:
case SchemaFieldTypes.GEO:
pushMissingValues(args, fieldOptions.MISSING_VALUES);
pushCommonFieldArguments(args, fieldOptions);
break;

case SchemaFieldTypes.TAG:
Expand All @@ -355,7 +358,11 @@ export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema
args.push('WITHSUFFIXTRIE');
}

pushMissingValues(args, fieldOptions.MISSING_VALUES);
pushCommonFieldArguments(args, fieldOptions);

if (fieldOptions.INDEXEMPTY) {
args.push('INDEXEMPTY');
}

break;

Expand Down Expand Up @@ -398,30 +405,20 @@ export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema
}
});

pushMissingValues(args, fieldOptions.MISSING_VALUES);

continue; // vector fields do not contain SORTABLE and NOINDEX options
break;

case SchemaFieldTypes.GEOSHAPE:
if (fieldOptions.COORD_SYSTEM !== undefined) {
args.push('COORD_SYSTEM', fieldOptions.COORD_SYSTEM);
}

pushMissingValues(args, fieldOptions.MISSING_VALUES);

continue; // geo shape fields do not contain SORTABLE and NOINDEX options
}

if (fieldOptions.SORTABLE) {
args.push('SORTABLE');
pushCommonFieldArguments(args, fieldOptions);

if (fieldOptions.SORTABLE === 'UNF') {
args.push('UNF');
}
break;
}

if (fieldOptions.NOINDEX) {
args.push('NOINDEX');
if (fieldOptions.INDEXMISSING) {
args.push('INDEXMISSING');
}
}
}
Expand Down

0 comments on commit 840ec57

Please sign in to comment.