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 missing fields support to schema (v4) #2789

Merged
merged 3 commits into from
Jul 17, 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
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
Loading