Skip to content

Commit 681d6ee

Browse files
committed
Merge branch 'master' of github.com:redis/node-redis
2 parents 7565ae3 + 01e66e7 commit 681d6ee

19 files changed

+581
-272
lines changed

packages/search/lib/commands/AGGREGATE.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { strict as assert } from 'assert';
22
import testUtils, { GLOBAL } from '../test-utils';
33
import { AggregateGroupByReducers, AggregateSteps, transformArguments } from './AGGREGATE';
4-
import { SchemaFieldTypes } from './CREATE';
4+
import { SchemaFieldTypes } from '.';
55

66
describe('AGGREGATE', () => {
77
describe('transformArguments', () => {

packages/search/lib/commands/AGGREGATE.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RedisCommandArguments } from '@node-redis/client/dist/lib/commands';
22
import { pushVerdictArgument, transformReplyTuples, TuplesObject } from '@node-redis/client/dist/lib/commands/generic-transformers';
3-
import { PropertyName, pushArgumentsWithLength, pushSortByArguments, SortByProperty } from '.';
3+
import { AggregateReply, PropertyName, pushArgumentsWithLength, pushSortByArguments, SortByProperty } from '.';
44

55
export enum AggregateSteps {
66
GROUPBY = 'GROUPBY',
@@ -118,14 +118,27 @@ type LoadField = PropertyName | {
118118
AS?: string;
119119
}
120120

121-
interface AggregateOptions {
121+
export interface AggregateOptions {
122122
VERBATIM?: true;
123123
LOAD?: LoadField | Array<LoadField>;
124124
STEPS?: Array<GroupByStep | SortStep | ApplyStep | LimitStep | FilterStep>;
125125
}
126126

127-
export function transformArguments(index: string, query: string, options?: AggregateOptions): RedisCommandArguments {
127+
export function transformArguments(
128+
index: string,
129+
query: string,
130+
options?: AggregateOptions
131+
): RedisCommandArguments {
132+
128133
const args = ['FT.AGGREGATE', index, query];
134+
pushAggregatehOptions(args, options);
135+
return args;
136+
}
137+
138+
export function pushAggregatehOptions(
139+
args: RedisCommandArguments,
140+
options?: AggregateOptions
141+
): RedisCommandArguments {
129142

130143
if (options?.VERBATIM) {
131144
args.push('VERBATIM');
@@ -258,16 +271,11 @@ function pushGroupByReducer(args: RedisCommandArguments, reducer: GroupByReducer
258271
}
259272
}
260273

261-
type AggregateRawReply = [
274+
export type AggregateRawReply = [
262275
total: number,
263276
...results: Array<Array<string>>
264277
];
265278

266-
interface AggregateReply {
267-
total: number;
268-
results: Array<TuplesObject>;
269-
}
270-
271279
export function transformReply(rawReply: AggregateRawReply): AggregateReply {
272280
const results: Array<TuplesObject> = [];
273281
for (let i = 1; i < rawReply.length; i++) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './ALTER';
4+
import { SchemaFieldTypes } from '.';
5+
6+
describe('ALTER', () => {
7+
describe('transformArguments', () => {
8+
it('with NOINDEX', () => {
9+
assert.deepEqual(
10+
transformArguments('index', {
11+
field: {
12+
type: SchemaFieldTypes.TEXT,
13+
NOINDEX: true,
14+
SORTABLE: 'UNF',
15+
AS: 'text'
16+
}
17+
}),
18+
['FT.ALTER', 'index', 'SCHEMA', 'ADD', 'field', 'AS', 'text', 'TEXT', 'SORTABLE', 'UNF', 'NOINDEX']
19+
);
20+
});
21+
});
22+
23+
testUtils.testWithClient('client.ft.create', async client => {
24+
await Promise.all([
25+
client.ft.create('index', {
26+
title: SchemaFieldTypes.TEXT
27+
}),
28+
]);
29+
30+
assert.equal(
31+
await client.ft.alter('index', {
32+
body: SchemaFieldTypes.TEXT
33+
}),
34+
'OK'
35+
);
36+
}, GLOBAL.SERVERS.OPEN);
37+
});

packages/search/lib/commands/ALTER.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { CreateSchema, pushSchema } from '.';
2+
3+
export function transformArguments(index: string, schema: CreateSchema): Array<string> {
4+
const args = ['FT.ALTER', index, 'SCHEMA', 'ADD'];
5+
pushSchema(args, schema);
6+
7+
return args;
8+
}
9+
10+
export declare function transformReply(): 'OK';

packages/search/lib/commands/CREATE.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { strict as assert } from 'assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3-
import { SchemaFieldTypes, SchemaTextFieldPhonetics, transformArguments } from './CREATE';
4-
import { RedisSearchLanguages } from '.';
3+
import { transformArguments } from './CREATE';
4+
import { SchemaFieldTypes, SchemaTextFieldPhonetics, RedisSearchLanguages } from '.';
55

66
describe('CREATE', () => {
77
describe('transformArguments', () => {

packages/search/lib/commands/CREATE.ts

Lines changed: 2 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,5 @@
11
import { pushOptionalVerdictArgument } from '@node-redis/client/dist/lib/commands/generic-transformers';
2-
import { RedisSearchLanguages, PropertyName } from '.';
3-
4-
export enum SchemaFieldTypes {
5-
TEXT = 'TEXT',
6-
NUMERIC = 'NUMERIC',
7-
GEO = 'GEO',
8-
TAG = 'TAG'
9-
}
10-
11-
type CreateSchemaField<T extends SchemaFieldTypes, E = Record<string, never>> = T | ({
12-
type: T;
13-
AS?: string;
14-
SORTABLE?: true | 'UNF';
15-
NOINDEX?: true;
16-
} & E);
17-
18-
export enum SchemaTextFieldPhonetics {
19-
DM_EN = 'dm:en',
20-
DM_FR = 'dm:fr',
21-
FM_PT = 'dm:pt',
22-
DM_ES = 'dm:es'
23-
}
24-
25-
type CreateSchemaTextField = CreateSchemaField<SchemaFieldTypes.TEXT, {
26-
NOSTEM?: true;
27-
WEIGHT?: number;
28-
PHONETIC?: SchemaTextFieldPhonetics;
29-
}>;
30-
31-
type CreateSchemaNumericField = CreateSchemaField<SchemaFieldTypes.NUMERIC>;
32-
33-
type CreateSchemaGeoField = CreateSchemaField<SchemaFieldTypes.GEO>;
34-
35-
type CreateSchemaTagField = CreateSchemaField<SchemaFieldTypes.TAG, {
36-
SEPERATOR?: string;
37-
CASESENSITIVE?: true;
38-
}>;
39-
40-
interface CreateSchema {
41-
[field: string]:
42-
CreateSchemaTextField |
43-
CreateSchemaNumericField |
44-
CreateSchemaGeoField |
45-
CreateSchemaTagField
46-
}
2+
import { RedisSearchLanguages, PropertyName, CreateSchema, pushSchema } from '.';
473

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

12884
pushOptionalVerdictArgument(args, 'STOPWORDS', options?.STOPWORDS);
129-
13085
args.push('SCHEMA');
131-
132-
for (const [field, fieldOptions] of Object.entries(schema)) {
133-
args.push(field);
134-
135-
if (typeof fieldOptions === 'string') {
136-
args.push(fieldOptions);
137-
continue;
138-
}
139-
140-
if (fieldOptions.AS) {
141-
args.push('AS', fieldOptions.AS);
142-
}
143-
144-
args.push(fieldOptions.type);
145-
146-
switch (fieldOptions.type) {
147-
case 'TEXT':
148-
if (fieldOptions.NOSTEM) {
149-
args.push('NOSTEM');
150-
}
151-
152-
if (fieldOptions.WEIGHT) {
153-
args.push('WEIGHT', fieldOptions.WEIGHT.toString());
154-
}
155-
156-
if (fieldOptions.PHONETIC) {
157-
args.push('PHONETIC', fieldOptions.PHONETIC);
158-
}
159-
160-
break;
161-
162-
// case 'NUMERIC':
163-
// case 'GEO':
164-
// break;
165-
166-
case 'TAG':
167-
if (fieldOptions.SEPERATOR) {
168-
args.push('SEPERATOR', fieldOptions.SEPERATOR);
169-
}
170-
171-
if (fieldOptions.CASESENSITIVE) {
172-
args.push('CASESENSITIVE');
173-
}
174-
175-
break;
176-
}
177-
178-
if (fieldOptions.SORTABLE) {
179-
args.push('SORTABLE');
180-
181-
if (fieldOptions.SORTABLE === 'UNF') {
182-
args.push('UNF');
183-
}
184-
}
185-
186-
if (fieldOptions.NOINDEX) {
187-
args.push('NOINDEX');
188-
}
189-
}
86+
pushSchema(args, schema);
19087

19188
return args;
19289
}

packages/search/lib/commands/DROPINDEX.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { strict as assert } from 'assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3-
import { SchemaFieldTypes } from './CREATE';
3+
import { SchemaFieldTypes } from '.';
44
import { transformArguments } from './DROPINDEX';
55

66
describe('DROPINDEX', () => {

packages/search/lib/commands/INFO.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { strict as assert } from 'assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3-
import { SchemaFieldTypes } from './CREATE';
43
import { transformArguments } from './INFO';
54

65
describe('INFO', () => {

packages/search/lib/commands/PROFILE.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { SchemaFieldTypes } from '.';
4+
import { transformArguments } from './PROFILE_AGGREGATE';
5+
import { AggregateSteps } from './AGGREGATE';
6+
7+
describe('PROFILE AGGREGATE', () => {
8+
describe('transformArguments', () => {
9+
it('without options', () => {
10+
assert.deepEqual(
11+
transformArguments('index', 'query'),
12+
['FT.PROFILE', 'index', 'AGGREGATE', 'QUERY', 'query']
13+
);
14+
});
15+
16+
it('with options', () => {
17+
assert.deepEqual(
18+
transformArguments('index', 'query', {
19+
LIMITED: true,
20+
VERBATIM: true,
21+
STEPS: [{
22+
type: AggregateSteps.SORTBY,
23+
BY: '@by'
24+
}]
25+
}),
26+
['FT.PROFILE', 'index', 'AGGREGATE', 'LIMITED', 'QUERY', 'query',
27+
'VERBATIM', 'SORTBY', '1', '@by']
28+
);
29+
});
30+
});
31+
32+
testUtils.testWithClient('client.ft.search', async client => {
33+
await Promise.all([
34+
client.ft.create('index', {
35+
field: SchemaFieldTypes.NUMERIC
36+
}),
37+
client.hSet('1', 'field', '1'),
38+
client.hSet('2', 'field', '2')
39+
]);
40+
41+
const res = await client.ft.profileAggregate('index', '*');
42+
assert.ok(typeof res.profile.iteratorsProfile.counter === 'number');
43+
assert.ok(typeof res.profile.parsingTime === 'string');
44+
assert.ok(res.results.total == 1);
45+
}, GLOBAL.SERVERS.OPEN);
46+
});

0 commit comments

Comments
 (0)