Skip to content

Commit

Permalink
add missing fields support to schema (v4) (#2789)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjpotter authored Jul 17, 2024
1 parent 64fca37 commit 0e7e2e6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/search/lib/commands/CREATE.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,36 @@ 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
35 changes: 32 additions & 3 deletions packages/search/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,32 @@ export enum SchemaFieldTypes {
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;
} & E);

type CreateSchemaCommonField<
Expand Down Expand Up @@ -313,11 +333,14 @@ export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema
args.push('WITHSUFFIXTRIE');
}

pushMissingValues(args, fieldOptions.MISSING_VALUES);

break;

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

case SchemaFieldTypes.TAG:
if (fieldOptions.SEPARATOR) {
Expand All @@ -332,6 +355,8 @@ export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema
args.push('WITHSUFFIXTRIE');
}

pushMissingValues(args, fieldOptions.MISSING_VALUES);

break;

case SchemaFieldTypes.VECTOR:
Expand Down Expand Up @@ -373,13 +398,17 @@ export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema
}
});

pushMissingValues(args, fieldOptions.MISSING_VALUES);

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);
}

pushMissingValues(args, fieldOptions.MISSING_VALUES);

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

Expand Down

0 comments on commit 0e7e2e6

Please sign in to comment.