Skip to content
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
2 changes: 1 addition & 1 deletion packages/search/lib/commands/AGGREGATE.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 { AggregateGroupByReducers, AggregateSteps, transformArguments } from './AGGREGATE';
import { SchemaFieldTypes } from './CREATE';
import { SchemaFieldTypes } from '.';

describe('AGGREGATE', () => {
describe('transformArguments', () => {
Expand Down
26 changes: 17 additions & 9 deletions packages/search/lib/commands/AGGREGATE.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RedisCommandArguments } from '@node-redis/client/dist/lib/commands';
import { pushVerdictArgument, transformReplyTuples, TuplesObject } from '@node-redis/client/dist/lib/commands/generic-transformers';
import { PropertyName, pushArgumentsWithLength, pushSortByArguments, SortByProperty } from '.';
import { AggregateReply, PropertyName, pushArgumentsWithLength, pushSortByArguments, SortByProperty } from '.';

export enum AggregateSteps {
GROUPBY = 'GROUPBY',
Expand Down Expand Up @@ -118,14 +118,27 @@ type LoadField = PropertyName | {
AS?: string;
}

interface AggregateOptions {
export interface AggregateOptions {
VERBATIM?: true;
LOAD?: LoadField | Array<LoadField>;
STEPS?: Array<GroupByStep | SortStep | ApplyStep | LimitStep | FilterStep>;
}

export function transformArguments(index: string, query: string, options?: AggregateOptions): RedisCommandArguments {
export function transformArguments(
index: string,
query: string,
options?: AggregateOptions
): RedisCommandArguments {

const args = ['FT.AGGREGATE', index, query];
pushAggregatehOptions(args, options);
return args;
}

export function pushAggregatehOptions(
args: RedisCommandArguments,
options?: AggregateOptions
): RedisCommandArguments {

if (options?.VERBATIM) {
args.push('VERBATIM');
Expand Down Expand Up @@ -258,16 +271,11 @@ function pushGroupByReducer(args: RedisCommandArguments, reducer: GroupByReducer
}
}

type AggregateRawReply = [
export type AggregateRawReply = [
total: number,
...results: Array<Array<string>>
];

interface AggregateReply {
total: number;
results: Array<TuplesObject>;
}

export function transformReply(rawReply: AggregateRawReply): AggregateReply {
const results: Array<TuplesObject> = [];
for (let i = 1; i < rawReply.length; i++) {
Expand Down
37 changes: 37 additions & 0 deletions packages/search/lib/commands/ALTER.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ALTER';
import { SchemaFieldTypes } from '.';

describe('ALTER', () => {
describe('transformArguments', () => {
it('with NOINDEX', () => {
assert.deepEqual(
transformArguments('index', {
field: {
type: SchemaFieldTypes.TEXT,
NOINDEX: true,
SORTABLE: 'UNF',
AS: 'text'
}
}),
['FT.ALTER', 'index', 'SCHEMA', 'ADD', 'field', 'AS', 'text', 'TEXT', 'SORTABLE', 'UNF', 'NOINDEX']
);
});
});

testUtils.testWithClient('client.ft.create', async client => {
await Promise.all([
client.ft.create('index', {
title: SchemaFieldTypes.TEXT
}),
]);

assert.equal(
await client.ft.alter('index', {
body: SchemaFieldTypes.TEXT
}),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});
10 changes: 10 additions & 0 deletions packages/search/lib/commands/ALTER.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CreateSchema, pushSchema } from '.';

export function transformArguments(index: string, schema: CreateSchema): Array<string> {
const args = ['FT.ALTER', index, 'SCHEMA', 'ADD'];
pushSchema(args, schema);

return args;
}

export declare function transformReply(): 'OK';
4 changes: 2 additions & 2 deletions 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 { SchemaFieldTypes, SchemaTextFieldPhonetics, transformArguments } from './CREATE';
import { RedisSearchLanguages } from '.';
import { transformArguments } from './CREATE';
import { SchemaFieldTypes, SchemaTextFieldPhonetics, RedisSearchLanguages } from '.';

describe('CREATE', () => {
describe('transformArguments', () => {
Expand Down
107 changes: 2 additions & 105 deletions packages/search/lib/commands/CREATE.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,5 @@
import { pushOptionalVerdictArgument } from '@node-redis/client/dist/lib/commands/generic-transformers';
import { RedisSearchLanguages, PropertyName } from '.';

export enum SchemaFieldTypes {
TEXT = 'TEXT',
NUMERIC = 'NUMERIC',
GEO = 'GEO',
TAG = 'TAG'
}

type CreateSchemaField<T extends SchemaFieldTypes, E = Record<string, never>> = T | ({
type: T;
AS?: string;
SORTABLE?: true | 'UNF';
NOINDEX?: true;
} & E);

export enum SchemaTextFieldPhonetics {
DM_EN = 'dm:en',
DM_FR = 'dm:fr',
FM_PT = 'dm:pt',
DM_ES = 'dm:es'
}

type CreateSchemaTextField = CreateSchemaField<SchemaFieldTypes.TEXT, {
NOSTEM?: true;
WEIGHT?: number;
PHONETIC?: SchemaTextFieldPhonetics;
}>;

type CreateSchemaNumericField = CreateSchemaField<SchemaFieldTypes.NUMERIC>;

type CreateSchemaGeoField = CreateSchemaField<SchemaFieldTypes.GEO>;

type CreateSchemaTagField = CreateSchemaField<SchemaFieldTypes.TAG, {
SEPERATOR?: string;
CASESENSITIVE?: true;
}>;

interface CreateSchema {
[field: string]:
CreateSchemaTextField |
CreateSchemaNumericField |
CreateSchemaGeoField |
CreateSchemaTagField
}
import { RedisSearchLanguages, PropertyName, CreateSchema, pushSchema } from '.';

interface CreateOptions {
ON?: 'HASH' | 'JSON';
Expand Down Expand Up @@ -126,67 +82,8 @@ export function transformArguments(index: string, schema: CreateSchema, options?
}

pushOptionalVerdictArgument(args, 'STOPWORDS', options?.STOPWORDS);

args.push('SCHEMA');

for (const [field, fieldOptions] of Object.entries(schema)) {
args.push(field);

if (typeof fieldOptions === 'string') {
args.push(fieldOptions);
continue;
}

if (fieldOptions.AS) {
args.push('AS', fieldOptions.AS);
}

args.push(fieldOptions.type);

switch (fieldOptions.type) {
case 'TEXT':
if (fieldOptions.NOSTEM) {
args.push('NOSTEM');
}

if (fieldOptions.WEIGHT) {
args.push('WEIGHT', fieldOptions.WEIGHT.toString());
}

if (fieldOptions.PHONETIC) {
args.push('PHONETIC', fieldOptions.PHONETIC);
}

break;

// case 'NUMERIC':
// case 'GEO':
// break;

case 'TAG':
if (fieldOptions.SEPERATOR) {
args.push('SEPERATOR', fieldOptions.SEPERATOR);
}

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

break;
}

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

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

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

return args;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/search/lib/commands/DROPINDEX.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { SchemaFieldTypes } from './CREATE';
import { SchemaFieldTypes } from '.';
import { transformArguments } from './DROPINDEX';

describe('DROPINDEX', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/search/lib/commands/INFO.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { SchemaFieldTypes } from './CREATE';
import { transformArguments } from './INFO';

describe('INFO', () => {
Expand Down
26 changes: 0 additions & 26 deletions packages/search/lib/commands/PROFILE.ts

This file was deleted.

46 changes: 46 additions & 0 deletions packages/search/lib/commands/PROFILE_AGGREGATE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { SchemaFieldTypes } from '.';
import { transformArguments } from './PROFILE_AGGREGATE';
import { AggregateSteps } from './AGGREGATE';

describe('PROFILE AGGREGATE', () => {
describe('transformArguments', () => {
it('without options', () => {
assert.deepEqual(
transformArguments('index', 'query'),
['FT.PROFILE', 'index', 'AGGREGATE', 'QUERY', 'query']
);
});

it('with options', () => {
assert.deepEqual(
transformArguments('index', 'query', {
LIMITED: true,
VERBATIM: true,
STEPS: [{
type: AggregateSteps.SORTBY,
BY: '@by'
}]
}),
['FT.PROFILE', 'index', 'AGGREGATE', 'LIMITED', 'QUERY', 'query',
'VERBATIM', 'SORTBY', '1', '@by']
);
});
});

testUtils.testWithClient('client.ft.search', async client => {
await Promise.all([
client.ft.create('index', {
field: SchemaFieldTypes.NUMERIC
}),
client.hSet('1', 'field', '1'),
client.hSet('2', 'field', '2')
]);

const res = await client.ft.profileAggregate('index', '*');
assert.ok(typeof res.profile.iteratorsProfile.counter === 'number');
assert.ok(typeof res.profile.parsingTime === 'string');
assert.ok(res.results.total == 1);
}, GLOBAL.SERVERS.OPEN);
});
29 changes: 29 additions & 0 deletions packages/search/lib/commands/PROFILE_AGGREGATE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { pushAggregatehOptions, AggregateOptions, transformReply as transformAggregateReply, AggregateRawReply } from './AGGREGATE';
import { ProfileOptions, ProfileRawReply, ProfileReply, transformProfile } from '.';

export const IS_READ_ONLY = true;

export function transformArguments(
index: string,
query: string,
options?: ProfileOptions & AggregateOptions
): Array<string> {
const args = ['FT.PROFILE', index, 'AGGREGATE'];

if (options?.LIMITED) {
args.push('LIMITED');
}

args.push('QUERY', query);
pushAggregatehOptions(args, options)
return args;
}

type ProfileAggeregateRawReply = ProfileRawReply<AggregateRawReply>;

export function transformReply(reply: ProfileAggeregateRawReply): ProfileReply {
return {
results: transformAggregateReply(reply[0]),
profile: transformProfile(reply[1])
};
}
Loading