You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
WIP
This commit is contained in:
@@ -1,509 +1,515 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { AggregateGroupByReducers, AggregateSteps, transformArguments } from './AGGREGATE';
|
||||
import { SchemaFieldTypes } from '.';
|
||||
import AGGREGATE from './AGGREGATE';
|
||||
// import { SchemaFieldTypes } from '.';
|
||||
|
||||
describe('AGGREGATE', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*'),
|
||||
['FT.AGGREGATE', 'index', '*']
|
||||
);
|
||||
});
|
||||
|
||||
it('with VERBATIM', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
VERBATIM: true
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'VERBATIM']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with LOAD', () => {
|
||||
describe('single', () => {
|
||||
describe('without alias', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*'),
|
||||
['FT.AGGREGATE', 'index', '*']
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
LOAD: '@property'
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('{ identifier: string }', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
LOAD: {
|
||||
identifier: '@property'
|
||||
}
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '1', '@property']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with VERBATIM', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', { VERBATIM: true }),
|
||||
['FT.AGGREGATE', 'index', '*', 'VERBATIM']
|
||||
);
|
||||
it('with alias', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
LOAD: {
|
||||
identifier: '@property',
|
||||
AS: 'alias'
|
||||
}
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '3', '@property', 'AS', 'alias']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with LOAD', () => {
|
||||
describe('single', () => {
|
||||
describe('without alias', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', { LOAD: '@property' }),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '1', '@property']
|
||||
);
|
||||
});
|
||||
it('multiple', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
LOAD: ['@1', '@2']
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('{ identifier: string }', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
LOAD: {
|
||||
identifier: '@property'
|
||||
}
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '1', '@property']
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('with STEPS', () => {
|
||||
describe('GROUPBY', () => {
|
||||
describe('COUNT', () => {
|
||||
describe('without properties', () => {
|
||||
it('without alias', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'COUNT'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT', '0']
|
||||
);
|
||||
});
|
||||
|
||||
it('with alias', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
LOAD: {
|
||||
identifier: '@property',
|
||||
AS: 'alias'
|
||||
}
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '3', '@property', 'AS', 'alias']
|
||||
);
|
||||
});
|
||||
it('with alias', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'COUNT',
|
||||
AS: 'count'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT', '0', 'AS', 'count']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with properties', () => {
|
||||
it('single', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
properties: '@property',
|
||||
REDUCE: {
|
||||
type: 'COUNT'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '1', '@property', 'REDUCE', 'COUNT', '0']
|
||||
);
|
||||
});
|
||||
|
||||
it('multiple', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', { LOAD: ['@1', '@2'] }),
|
||||
['FT.AGGREGATE', 'index', '*', 'LOAD', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with STEPS', () => {
|
||||
describe('GROUPBY', () => {
|
||||
describe('COUNT', () => {
|
||||
describe('without properties', () => {
|
||||
it('without alias', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.COUNT
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT', '0']
|
||||
);
|
||||
});
|
||||
|
||||
it('with alias', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.COUNT,
|
||||
AS: 'count'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT', '0', 'AS', 'count']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with properties', () => {
|
||||
it('single', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
properties: '@property',
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.COUNT
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '1', '@property', 'REDUCE', 'COUNT', '0']
|
||||
);
|
||||
});
|
||||
|
||||
it('multiple', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
properties: ['@1', '@2'],
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.COUNT
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '2', '@1', '@2', 'REDUCE', 'COUNT', '0']
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('COUNT_DISTINCT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.COUNT_DISTINCT,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT_DISTINCT', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('COUNT_DISTINCTISH', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.COUNT_DISTINCTISH,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT_DISTINCTISH', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('SUM', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.SUM,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'SUM', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('MIN', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.MIN,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'MIN', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('MAX', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.MAX,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'MAX', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('AVG', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.AVG,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'AVG', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('STDDEV', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.STDDEV,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'STDDEV', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('QUANTILE', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.QUANTILE,
|
||||
property: '@property',
|
||||
quantile: 0.5
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'QUANTILE', '2', '@property', '0.5']
|
||||
);
|
||||
});
|
||||
|
||||
it('TO_LIST', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.TO_LIST,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'TOLIST', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
describe('FIRST_VALUE', () => {
|
||||
it('simple', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.FIRST_VALUE,
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with BY', () => {
|
||||
describe('without direction', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.FIRST_VALUE,
|
||||
property: '@property',
|
||||
BY: '@by'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '3', '@property', 'BY', '@by']
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
it('{ property: string }', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.FIRST_VALUE,
|
||||
property: '@property',
|
||||
BY: {
|
||||
property: '@by'
|
||||
}
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '3', '@property', 'BY', '@by']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with direction', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.FIRST_VALUE,
|
||||
property: '@property',
|
||||
BY: {
|
||||
property: '@by',
|
||||
direction: 'ASC'
|
||||
}
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '4', '@property', 'BY', '@by', 'ASC']
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('RANDOM_SAMPLE', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: {
|
||||
type: AggregateGroupByReducers.RANDOM_SAMPLE,
|
||||
property: '@property',
|
||||
sampleSize: 1
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'RANDOM_SAMPLE', '2', '@property', '1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SORTBY', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.SORTBY,
|
||||
BY: '@by'
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'SORTBY', '1', '@by']
|
||||
);
|
||||
});
|
||||
|
||||
it('Array', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.SORTBY,
|
||||
BY: ['@1', '@2']
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'SORTBY', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
|
||||
it('with MAX', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.SORTBY,
|
||||
BY: '@by',
|
||||
MAX: 1
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'SORTBY', '1', '@by', 'MAX', '1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('APPLY', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.APPLY,
|
||||
expression: '@field + 1',
|
||||
AS: 'as'
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'APPLY', '@field + 1', 'AS', 'as']
|
||||
);
|
||||
});
|
||||
|
||||
describe('LIMIT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.LIMIT,
|
||||
from: 0,
|
||||
size: 1
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LIMIT', '0', '1']
|
||||
);
|
||||
});
|
||||
|
||||
describe('FILTER', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.FILTER,
|
||||
expression: '@field != ""'
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'FILTER', '@field != ""']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with PARAMS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
PARAMS: {
|
||||
param: 'value'
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
properties: ['@1', '@2'],
|
||||
REDUCE: {
|
||||
type: 'COUNT'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'PARAMS', '2', 'param', 'value']
|
||||
);
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '2', '@1', '@2', 'REDUCE', 'COUNT', '0']
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('with DIALECT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', {
|
||||
DIALECT: 1
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'DIALECT', '1']
|
||||
);
|
||||
it('COUNT_DISTINCT', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'COUNT_DISTINCT',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT_DISTINCT', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('with TIMEOUT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', { TIMEOUT: 10 }),
|
||||
['FT.AGGREGATE', 'index', '*', 'TIMEOUT', '10']
|
||||
);
|
||||
it('COUNT_DISTINCTISH', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'COUNT_DISTINCTISH',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT_DISTINCTISH', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('SUM', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'SUM',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'SUM', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('MIN', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'MIN',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'MIN', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('MAX', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'MAX',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'MAX', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('AVG', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'AVG',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'AVG', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('STDDEV', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'STDDEV',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'STDDEV', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
it('QUANTILE', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'QUANTILE',
|
||||
property: '@property',
|
||||
quantile: 0.5
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'QUANTILE', '2', '@property', '0.5']
|
||||
);
|
||||
});
|
||||
|
||||
it('TOLIST', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'TOLIST',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'TOLIST', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
describe('FIRST_VALUE', () => {
|
||||
it('simple', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'FIRST_VALUE',
|
||||
property: '@property'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '1', '@property']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with BY', () => {
|
||||
describe('without direction', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'FIRST_VALUE',
|
||||
property: '@property',
|
||||
BY: '@by'
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '3', '@property', 'BY', '@by']
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
it('{ property: string }', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'FIRST_VALUE',
|
||||
property: '@property',
|
||||
BY: {
|
||||
property: '@by'
|
||||
}
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '3', '@property', 'BY', '@by']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with direction', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'FIRST_VALUE',
|
||||
property: '@property',
|
||||
BY: {
|
||||
property: '@by',
|
||||
direction: 'ASC'
|
||||
}
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '4', '@property', 'BY', '@by', 'ASC']
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('RANDOM_SAMPLE', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: {
|
||||
type: 'RANDOM_SAMPLE',
|
||||
property: '@property',
|
||||
sampleSize: 1
|
||||
}
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'RANDOM_SAMPLE', '2', '@property', '1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SORTBY', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'SORTBY',
|
||||
BY: '@by'
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'SORTBY', '1', '@by']
|
||||
);
|
||||
});
|
||||
|
||||
it('Array', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'SORTBY',
|
||||
BY: ['@1', '@2']
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'SORTBY', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
|
||||
it('with MAX', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'SORTBY',
|
||||
BY: '@by',
|
||||
MAX: 1
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'SORTBY', '1', '@by', 'MAX', '1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('APPLY', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'APPLY',
|
||||
expression: '@field + 1',
|
||||
AS: 'as'
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'APPLY', '@field + 1', 'AS', 'as']
|
||||
);
|
||||
});
|
||||
|
||||
describe('LIMIT', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'LIMIT',
|
||||
from: 0,
|
||||
size: 1
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'LIMIT', '0', '1']
|
||||
);
|
||||
});
|
||||
|
||||
describe('FILTER', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'FILTER',
|
||||
expression: '@field != ""'
|
||||
}]
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'FILTER', '@field != ""']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.ft.aggregate', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: SchemaFieldTypes.NUMERIC
|
||||
}),
|
||||
client.hSet('1', 'field', '1'),
|
||||
client.hSet('2', 'field', '2')
|
||||
]);
|
||||
it('with PARAMS', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
PARAMS: {
|
||||
param: 'value'
|
||||
}
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'PARAMS', '2', 'param', 'value']
|
||||
);
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.aggregate('index', '*', {
|
||||
STEPS: [{
|
||||
type: AggregateSteps.GROUPBY,
|
||||
REDUCE: [{
|
||||
type: AggregateGroupByReducers.SUM,
|
||||
property: '@field',
|
||||
AS: 'sum'
|
||||
}, {
|
||||
type: AggregateGroupByReducers.AVG,
|
||||
property: '@field',
|
||||
AS: 'avg'
|
||||
}]
|
||||
}]
|
||||
}),
|
||||
{
|
||||
total: 1,
|
||||
results: [
|
||||
Object.create(null, {
|
||||
sum: {
|
||||
value: '3',
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
},
|
||||
avg: {
|
||||
value: '1.5',
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
}
|
||||
})
|
||||
]
|
||||
it('with DIALECT', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', {
|
||||
DIALECT: 1
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'DIALECT', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with TIMEOUT', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE.transformArguments('index', '*', { TIMEOUT: 10 }),
|
||||
['FT.AGGREGATE', 'index', '*', 'TIMEOUT', '10']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.ft.aggregate', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: 'NUMERIC'
|
||||
}),
|
||||
client.hSet('1', 'field', '1'),
|
||||
client.hSet('2', 'field', '2')
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.aggregate('index', '*', {
|
||||
STEPS: [{
|
||||
type: 'GROUPBY',
|
||||
REDUCE: [{
|
||||
type: 'SUM',
|
||||
property: '@field',
|
||||
AS: 'sum'
|
||||
}, {
|
||||
type: 'AVG',
|
||||
property: '@field',
|
||||
AS: 'avg'
|
||||
}]
|
||||
}]
|
||||
}),
|
||||
{
|
||||
total: 1,
|
||||
results: [
|
||||
Object.create(null, {
|
||||
sum: {
|
||||
value: '3',
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
},
|
||||
avg: {
|
||||
value: '1.5',
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
})
|
||||
]
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
@@ -1,311 +1,294 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
|
||||
import { pushVariadicArgument, transformTuplesReply } from '@redis/client/dist/lib/commands/generic-transformers';
|
||||
import { Params, PropertyName, pushArgumentsWithLength, pushParamsArgs, pushSortByArguments, SortByProperty } from '.';
|
||||
import { Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
|
||||
import { RediSearchProperty } from './CREATE';
|
||||
import { FtSearchParams, pushParamsArgument } from './SEARCH';
|
||||
import { pushVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
|
||||
|
||||
export enum AggregateSteps {
|
||||
GROUPBY = 'GROUPBY',
|
||||
SORTBY = 'SORTBY',
|
||||
APPLY = 'APPLY',
|
||||
LIMIT = 'LIMIT',
|
||||
FILTER = 'FILTER'
|
||||
type LoadField = RediSearchProperty | {
|
||||
identifier: RediSearchProperty;
|
||||
AS?: RedisArgument;
|
||||
}
|
||||
|
||||
interface AggregateStep<T extends AggregateSteps> {
|
||||
type: T;
|
||||
export const FT_AGGREGATE_STEPS = {
|
||||
GROUPBY: 'GROUPBY',
|
||||
SORTBY: 'SORTBY',
|
||||
APPLY: 'APPLY',
|
||||
LIMIT: 'LIMIT',
|
||||
FILTER: 'FILTER'
|
||||
} as const;
|
||||
|
||||
type FT_AGGREGATE_STEPS = typeof FT_AGGREGATE_STEPS;
|
||||
|
||||
export type FtAggregateStep = FT_AGGREGATE_STEPS[keyof FT_AGGREGATE_STEPS];
|
||||
|
||||
interface AggregateStep<T extends FtAggregateStep> {
|
||||
type: T;
|
||||
}
|
||||
|
||||
export enum AggregateGroupByReducers {
|
||||
COUNT = 'COUNT',
|
||||
COUNT_DISTINCT = 'COUNT_DISTINCT',
|
||||
COUNT_DISTINCTISH = 'COUNT_DISTINCTISH',
|
||||
SUM = 'SUM',
|
||||
MIN = 'MIN',
|
||||
MAX = 'MAX',
|
||||
AVG = 'AVG',
|
||||
STDDEV = 'STDDEV',
|
||||
QUANTILE = 'QUANTILE',
|
||||
TOLIST = 'TOLIST',
|
||||
TO_LIST = 'TOLIST',
|
||||
FIRST_VALUE = 'FIRST_VALUE',
|
||||
RANDOM_SAMPLE = 'RANDOM_SAMPLE'
|
||||
export const FT_AGGREGATE_GROUP_BY_REDUCERS = {
|
||||
COUNT: 'COUNT',
|
||||
COUNT_DISTINCT: 'COUNT_DISTINCT',
|
||||
COUNT_DISTINCTISH: 'COUNT_DISTINCTISH',
|
||||
SUM: 'SUM',
|
||||
MIN: 'MIN',
|
||||
MAX: 'MAX',
|
||||
AVG: 'AVG',
|
||||
STDDEV: 'STDDEV',
|
||||
QUANTILE: 'QUANTILE',
|
||||
TOLIST: 'TOLIST',
|
||||
FIRST_VALUE: 'FIRST_VALUE',
|
||||
RANDOM_SAMPLE: 'RANDOM_SAMPLE'
|
||||
} as const;
|
||||
|
||||
type FT_AGGREGATE_GROUP_BY_REDUCERS = typeof FT_AGGREGATE_GROUP_BY_REDUCERS;
|
||||
|
||||
export type FtAggregateGroupByReducer = FT_AGGREGATE_GROUP_BY_REDUCERS[keyof FT_AGGREGATE_GROUP_BY_REDUCERS];
|
||||
|
||||
interface GroupByReducer<T extends FtAggregateGroupByReducer> {
|
||||
type: T;
|
||||
AS?: RedisArgument;
|
||||
}
|
||||
|
||||
interface GroupByReducer<T extends AggregateGroupByReducers> {
|
||||
type: T;
|
||||
AS?: string;
|
||||
interface GroupByReducerWithProperty<T extends FtAggregateGroupByReducer> extends GroupByReducer<T> {
|
||||
property: RediSearchProperty;
|
||||
}
|
||||
|
||||
type CountReducer = GroupByReducer<AggregateGroupByReducers.COUNT>;
|
||||
type CountReducer = GroupByReducer<FT_AGGREGATE_GROUP_BY_REDUCERS['COUNT']>;
|
||||
|
||||
interface CountDistinctReducer extends GroupByReducer<AggregateGroupByReducers.COUNT_DISTINCT> {
|
||||
property: PropertyName;
|
||||
type CountDistinctReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['COUNT_DISTINCT']>;
|
||||
|
||||
type CountDistinctishReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['COUNT_DISTINCTISH']>;
|
||||
|
||||
type SumReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['SUM']>;
|
||||
|
||||
type MinReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['MIN']>;
|
||||
|
||||
type MaxReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['MAX']>;
|
||||
|
||||
type AvgReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['AVG']>;
|
||||
|
||||
type StdDevReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['STDDEV']>;
|
||||
|
||||
interface QuantileReducer extends GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['QUANTILE']> {
|
||||
quantile: number;
|
||||
}
|
||||
|
||||
interface CountDistinctishReducer extends GroupByReducer<AggregateGroupByReducers.COUNT_DISTINCTISH> {
|
||||
property: PropertyName;
|
||||
type ToListReducer = GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['TOLIST']>;
|
||||
|
||||
interface FirstValueReducer extends GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['FIRST_VALUE']> {
|
||||
BY?: RediSearchProperty | {
|
||||
property: RediSearchProperty;
|
||||
direction?: 'ASC' | 'DESC';
|
||||
};
|
||||
}
|
||||
|
||||
interface SumReducer extends GroupByReducer<AggregateGroupByReducers.SUM> {
|
||||
property: PropertyName;
|
||||
}
|
||||
|
||||
interface MinReducer extends GroupByReducer<AggregateGroupByReducers.MIN> {
|
||||
property: PropertyName;
|
||||
}
|
||||
|
||||
interface MaxReducer extends GroupByReducer<AggregateGroupByReducers.MAX> {
|
||||
property: PropertyName;
|
||||
}
|
||||
|
||||
interface AvgReducer extends GroupByReducer<AggregateGroupByReducers.AVG> {
|
||||
property: PropertyName;
|
||||
}
|
||||
|
||||
interface StdDevReducer extends GroupByReducer<AggregateGroupByReducers.STDDEV> {
|
||||
property: PropertyName;
|
||||
}
|
||||
|
||||
interface QuantileReducer extends GroupByReducer<AggregateGroupByReducers.QUANTILE> {
|
||||
property: PropertyName;
|
||||
quantile: number;
|
||||
}
|
||||
|
||||
interface ToListReducer extends GroupByReducer<AggregateGroupByReducers.TOLIST> {
|
||||
property: PropertyName;
|
||||
}
|
||||
|
||||
interface FirstValueReducer extends GroupByReducer<AggregateGroupByReducers.FIRST_VALUE> {
|
||||
property: PropertyName;
|
||||
BY?: PropertyName | {
|
||||
property: PropertyName;
|
||||
direction?: 'ASC' | 'DESC';
|
||||
};
|
||||
}
|
||||
|
||||
interface RandomSampleReducer extends GroupByReducer<AggregateGroupByReducers.RANDOM_SAMPLE> {
|
||||
property: PropertyName;
|
||||
sampleSize: number;
|
||||
interface RandomSampleReducer extends GroupByReducerWithProperty<FT_AGGREGATE_GROUP_BY_REDUCERS['RANDOM_SAMPLE']> {
|
||||
sampleSize: number;
|
||||
}
|
||||
|
||||
type GroupByReducers = CountReducer | CountDistinctReducer | CountDistinctishReducer | SumReducer | MinReducer | MaxReducer | AvgReducer | StdDevReducer | QuantileReducer | ToListReducer | FirstValueReducer | RandomSampleReducer;
|
||||
|
||||
interface GroupByStep extends AggregateStep<AggregateSteps.GROUPBY> {
|
||||
properties?: PropertyName | Array<PropertyName>;
|
||||
REDUCE: GroupByReducers | Array<GroupByReducers>;
|
||||
interface GroupByStep extends AggregateStep<FT_AGGREGATE_STEPS['GROUPBY']> {
|
||||
properties?: RediSearchProperty | Array<RediSearchProperty>;
|
||||
REDUCE: GroupByReducers | Array<GroupByReducers>;
|
||||
}
|
||||
|
||||
interface SortStep extends AggregateStep<AggregateSteps.SORTBY> {
|
||||
BY: SortByProperty | Array<SortByProperty>;
|
||||
MAX?: number;
|
||||
type SortByProperty = RedisArgument | {
|
||||
BY: RediSearchProperty;
|
||||
DIRECTION?: 'ASC' | 'DESC';
|
||||
};
|
||||
|
||||
interface SortStep extends AggregateStep<FT_AGGREGATE_STEPS['SORTBY']> {
|
||||
BY: SortByProperty | Array<SortByProperty>;
|
||||
MAX?: number;
|
||||
}
|
||||
|
||||
interface ApplyStep extends AggregateStep<AggregateSteps.APPLY> {
|
||||
expression: string;
|
||||
AS: string;
|
||||
interface ApplyStep extends AggregateStep<FT_AGGREGATE_STEPS['APPLY']> {
|
||||
expression: RedisArgument;
|
||||
AS: RedisArgument;
|
||||
}
|
||||
|
||||
interface LimitStep extends AggregateStep<AggregateSteps.LIMIT> {
|
||||
from: number;
|
||||
size: number;
|
||||
interface LimitStep extends AggregateStep<FT_AGGREGATE_STEPS['LIMIT']> {
|
||||
from: number;
|
||||
size: number;
|
||||
}
|
||||
|
||||
interface FilterStep extends AggregateStep<AggregateSteps.FILTER> {
|
||||
expression: string;
|
||||
interface FilterStep extends AggregateStep<FT_AGGREGATE_STEPS['FILTER']> {
|
||||
expression: RedisArgument;
|
||||
}
|
||||
|
||||
type LoadField = PropertyName | {
|
||||
identifier: PropertyName;
|
||||
AS?: string;
|
||||
export interface FtAggregateOptions {
|
||||
VERBATIM?: boolean;
|
||||
LOAD?: LoadField | Array<LoadField>;
|
||||
TIMEOUT?: number;
|
||||
STEPS?: Array<GroupByStep | SortStep | ApplyStep | LimitStep | FilterStep>;
|
||||
PARAMS?: FtSearchParams;
|
||||
DIALECT?: number;
|
||||
}
|
||||
|
||||
export interface AggregateOptions {
|
||||
VERBATIM?: true;
|
||||
LOAD?: LoadField | Array<LoadField>;
|
||||
STEPS?: Array<GroupByStep | SortStep | ApplyStep | LimitStep | FilterStep>;
|
||||
PARAMS?: Params;
|
||||
DIALECT?: number;
|
||||
TIMEOUT?: number;
|
||||
}
|
||||
export default {
|
||||
FIRST_KEY_INDEX: undefined,
|
||||
IS_READ_ONLY: false,
|
||||
transformArguments(index: RedisArgument, query: RedisArgument, options?: FtAggregateOptions) {
|
||||
const args = ['FT.AGGREGATE', index, query];
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(
|
||||
index: string,
|
||||
query: string,
|
||||
options?: AggregateOptions
|
||||
): RedisCommandArguments {
|
||||
return pushAggregatehOptions(
|
||||
['FT.AGGREGATE', index, query],
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
export function pushAggregatehOptions(
|
||||
args: RedisCommandArguments,
|
||||
options?: AggregateOptions
|
||||
): RedisCommandArguments {
|
||||
if (options?.VERBATIM) {
|
||||
args.push('VERBATIM');
|
||||
args.push('VERBATIM');
|
||||
}
|
||||
|
||||
if (options?.LOAD) {
|
||||
args.push('LOAD');
|
||||
pushArgumentsWithLength(args, () => {
|
||||
if (Array.isArray(options.LOAD)) {
|
||||
for (const load of options.LOAD) {
|
||||
pushLoadField(args, load);
|
||||
}
|
||||
} else {
|
||||
pushLoadField(args, options.LOAD!);
|
||||
}
|
||||
});
|
||||
}
|
||||
const length = args.push('LOAD', '');
|
||||
|
||||
if (options?.STEPS) {
|
||||
for (const step of options.STEPS) {
|
||||
switch (step.type) {
|
||||
case AggregateSteps.GROUPBY:
|
||||
args.push('GROUPBY');
|
||||
if (!step.properties) {
|
||||
args.push('0');
|
||||
} else {
|
||||
pushVariadicArgument(args, step.properties);
|
||||
}
|
||||
|
||||
if (Array.isArray(step.REDUCE)) {
|
||||
for (const reducer of step.REDUCE) {
|
||||
pushGroupByReducer(args, reducer);
|
||||
}
|
||||
} else {
|
||||
pushGroupByReducer(args, step.REDUCE);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case AggregateSteps.SORTBY:
|
||||
pushSortByArguments(args, 'SORTBY', step.BY);
|
||||
|
||||
if (step.MAX) {
|
||||
args.push('MAX', step.MAX.toString());
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case AggregateSteps.APPLY:
|
||||
args.push('APPLY', step.expression, 'AS', step.AS);
|
||||
break;
|
||||
|
||||
case AggregateSteps.LIMIT:
|
||||
args.push('LIMIT', step.from.toString(), step.size.toString());
|
||||
break;
|
||||
|
||||
case AggregateSteps.FILTER:
|
||||
args.push('FILTER', step.expression);
|
||||
break;
|
||||
}
|
||||
if (Array.isArray(options.LOAD)) {
|
||||
for (const load of options.LOAD) {
|
||||
pushLoadField(args, load);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pushLoadField(args, options.LOAD);
|
||||
}
|
||||
|
||||
pushParamsArgs(args, options?.PARAMS);
|
||||
|
||||
if (options?.DIALECT) {
|
||||
args.push('DIALECT', options.DIALECT.toString());
|
||||
args[length - 1] = (args.length - length).toString();
|
||||
}
|
||||
|
||||
if (options?.TIMEOUT !== undefined) {
|
||||
args.push('TIMEOUT', options.TIMEOUT.toString());
|
||||
args.push('TIMEOUT', options.TIMEOUT.toString());
|
||||
}
|
||||
|
||||
if (options?.STEPS) {
|
||||
for (const step of options.STEPS) {
|
||||
args.push(step.type);
|
||||
switch (step.type) {
|
||||
case FT_AGGREGATE_STEPS.GROUPBY:
|
||||
if (!step.properties) {
|
||||
args.push('0');
|
||||
} else {
|
||||
pushVariadicArgument(args, step.properties);
|
||||
}
|
||||
|
||||
if (Array.isArray(step.REDUCE)) {
|
||||
for (const reducer of step.REDUCE) {
|
||||
pushGroupByReducer(args, reducer);
|
||||
}
|
||||
} else {
|
||||
pushGroupByReducer(args, step.REDUCE);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case FT_AGGREGATE_STEPS.SORTBY:
|
||||
const length = args.push('');
|
||||
|
||||
if (Array.isArray(step.BY)) {
|
||||
for (const by of step.BY) {
|
||||
pushSortByProperty(args, by);
|
||||
}
|
||||
} else {
|
||||
pushSortByProperty(args, step.BY);
|
||||
}
|
||||
|
||||
if (step.MAX) {
|
||||
args.push('MAX', step.MAX.toString());
|
||||
}
|
||||
|
||||
args[length - 1] = (args.length - length).toString();
|
||||
|
||||
break;
|
||||
|
||||
case FT_AGGREGATE_STEPS.APPLY:
|
||||
args.push('APPLY', step.expression, 'AS', step.AS);
|
||||
break;
|
||||
|
||||
case FT_AGGREGATE_STEPS.LIMIT:
|
||||
args.push('LIMIT', step.from.toString(), step.size.toString());
|
||||
break;
|
||||
|
||||
case FT_AGGREGATE_STEPS.FILTER:
|
||||
args.push('FILTER', step.expression);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pushParamsArgument(args, options?.PARAMS);
|
||||
|
||||
if (options?.DIALECT !== undefined) {
|
||||
args.push('DIALECT', options.DIALECT.toString());
|
||||
}
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: undefined as unknown as () => any
|
||||
} as const satisfies Command;
|
||||
|
||||
function pushLoadField(args: Array<RedisArgument>, toLoad: LoadField) {
|
||||
if (typeof toLoad === 'string' || toLoad instanceof Buffer) {
|
||||
args.push(toLoad);
|
||||
} else {
|
||||
args.push(toLoad.identifier);
|
||||
|
||||
if (toLoad.AS) {
|
||||
args.push('AS', toLoad.AS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function pushLoadField(args: RedisCommandArguments, toLoad: LoadField): void {
|
||||
if (typeof toLoad === 'string') {
|
||||
args.push(toLoad);
|
||||
} else {
|
||||
args.push(toLoad.identifier);
|
||||
function pushGroupByReducer(args: Array<RedisArgument>, reducer: GroupByReducers) {
|
||||
args.push('REDUCE', reducer.type);
|
||||
|
||||
if (toLoad.AS) {
|
||||
args.push('AS', toLoad.AS);
|
||||
switch (reducer.type) {
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.COUNT:
|
||||
args.push('0');
|
||||
break;
|
||||
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.COUNT_DISTINCT:
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.COUNT_DISTINCTISH:
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.SUM:
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.MIN:
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.MAX:
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.AVG:
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.STDDEV:
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.TOLIST:
|
||||
args.push('1', reducer.property);
|
||||
break;
|
||||
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.QUANTILE:
|
||||
args.push('2', reducer.property, reducer.quantile.toString());
|
||||
break;
|
||||
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.FIRST_VALUE: {
|
||||
const length = args.push('', reducer.property) - 1;
|
||||
if (reducer.BY) {
|
||||
args.push('BY');
|
||||
if (typeof reducer.BY === 'string' || reducer.BY instanceof Buffer) {
|
||||
args.push(reducer.BY);
|
||||
} else {
|
||||
args.push(reducer.BY.property);
|
||||
if (reducer.BY.direction) {
|
||||
args.push(reducer.BY.direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
args[length - 1] = (args.length - length).toString();
|
||||
break;
|
||||
}
|
||||
|
||||
case FT_AGGREGATE_GROUP_BY_REDUCERS.RANDOM_SAMPLE:
|
||||
args.push('2', reducer.property, reducer.sampleSize.toString());
|
||||
break;
|
||||
}
|
||||
|
||||
if (reducer.AS) {
|
||||
args.push('AS', reducer.AS);
|
||||
}
|
||||
}
|
||||
|
||||
function pushGroupByReducer(args: RedisCommandArguments, reducer: GroupByReducers): void {
|
||||
args.push('REDUCE', reducer.type);
|
||||
|
||||
switch (reducer.type) {
|
||||
case AggregateGroupByReducers.COUNT:
|
||||
args.push('0');
|
||||
break;
|
||||
|
||||
case AggregateGroupByReducers.COUNT_DISTINCT:
|
||||
case AggregateGroupByReducers.COUNT_DISTINCTISH:
|
||||
case AggregateGroupByReducers.SUM:
|
||||
case AggregateGroupByReducers.MIN:
|
||||
case AggregateGroupByReducers.MAX:
|
||||
case AggregateGroupByReducers.AVG:
|
||||
case AggregateGroupByReducers.STDDEV:
|
||||
case AggregateGroupByReducers.TOLIST:
|
||||
args.push('1', reducer.property);
|
||||
break;
|
||||
|
||||
case AggregateGroupByReducers.QUANTILE:
|
||||
args.push('2', reducer.property, reducer.quantile.toString());
|
||||
break;
|
||||
|
||||
case AggregateGroupByReducers.FIRST_VALUE: {
|
||||
pushArgumentsWithLength(args, () => {
|
||||
args.push(reducer.property);
|
||||
|
||||
if (reducer.BY) {
|
||||
args.push('BY');
|
||||
if (typeof reducer.BY === 'string') {
|
||||
args.push(reducer.BY);
|
||||
} else {
|
||||
args.push(reducer.BY.property);
|
||||
|
||||
if (reducer.BY.direction) {
|
||||
args.push(reducer.BY.direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case AggregateGroupByReducers.RANDOM_SAMPLE:
|
||||
args.push('2', reducer.property, reducer.sampleSize.toString());
|
||||
break;
|
||||
}
|
||||
|
||||
if (reducer.AS) {
|
||||
args.push('AS', reducer.AS);
|
||||
function pushSortByProperty(args: Array<RedisArgument>, sortBy: SortByProperty) {
|
||||
if (typeof sortBy === 'string' || sortBy instanceof Buffer) {
|
||||
args.push(sortBy);
|
||||
} else {
|
||||
args.push(sortBy.BY);
|
||||
if (sortBy.DIRECTION) {
|
||||
args.push(sortBy.DIRECTION);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type AggregateRawReply = [
|
||||
total: number,
|
||||
...results: Array<Array<RedisCommandArgument>>
|
||||
];
|
||||
|
||||
export interface AggregateReply {
|
||||
total: number;
|
||||
results: Array<Record<string, RedisCommandArgument>>;
|
||||
}
|
||||
|
||||
export function transformReply(rawReply: AggregateRawReply): AggregateReply {
|
||||
const results: Array<Record<string, RedisCommandArgument>> = [];
|
||||
for (let i = 1; i < rawReply.length; i++) {
|
||||
results.push(
|
||||
transformTuplesReply(rawReply[i] as Array<RedisCommandArgument>)
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
total: rawReply[0],
|
||||
results
|
||||
};
|
||||
}
|
@@ -1,37 +1,47 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './AGGREGATE_WITHCURSOR';
|
||||
import { SchemaFieldTypes } from '.';
|
||||
import AGGREGATE_WITHCURSOR from './AGGREGATE_WITHCURSOR';
|
||||
|
||||
describe('AGGREGATE WITHCURSOR', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*'),
|
||||
['FT.AGGREGATE', 'index', '*', 'WITHCURSOR']
|
||||
);
|
||||
});
|
||||
|
||||
it('with COUNT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', '*', { COUNT: 1 }),
|
||||
['FT.AGGREGATE', 'index', '*', 'WITHCURSOR', 'COUNT', '1']
|
||||
);
|
||||
});
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE_WITHCURSOR.transformArguments('index', '*'),
|
||||
['FT.AGGREGATE', 'index', '*', 'WITHCURSOR']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.ft.aggregateWithCursor', async client => {
|
||||
await client.ft.create('index', {
|
||||
field: SchemaFieldTypes.NUMERIC
|
||||
});
|
||||
it('with COUNT', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE_WITHCURSOR.transformArguments('index', '*', {
|
||||
COUNT: 1
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'WITHCURSOR', 'COUNT', '1']
|
||||
);
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.aggregateWithCursor('index', '*'),
|
||||
{
|
||||
total: 0,
|
||||
results: [],
|
||||
cursor: 0
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
it('with MAXIDLE', () => {
|
||||
assert.deepEqual(
|
||||
AGGREGATE_WITHCURSOR.transformArguments('index', '*', {
|
||||
MAXIDLE: 1
|
||||
}),
|
||||
['FT.AGGREGATE', 'index', '*', 'WITHCURSOR', 'MAXIDLE', '1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.ft.aggregateWithCursor', async client => {
|
||||
await client.ft.create('index', {
|
||||
field: 'NUMERIC'
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.aggregateWithCursor('index', '*'),
|
||||
{
|
||||
total: 0,
|
||||
results: [],
|
||||
cursor: 0
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
@@ -1,44 +1,73 @@
|
||||
import {
|
||||
AggregateOptions,
|
||||
AggregateRawReply,
|
||||
AggregateReply,
|
||||
transformArguments as transformAggregateArguments,
|
||||
transformReply as transformAggregateReply
|
||||
} from './AGGREGATE';
|
||||
// import {
|
||||
// AggregateOptions,
|
||||
// AggregateRawReply,
|
||||
// AggregateReply,
|
||||
// transformArguments as transformAggregateArguments,
|
||||
// transformReply as transformAggregateReply
|
||||
// } from './AGGREGATE';
|
||||
|
||||
export { FIRST_KEY_INDEX, IS_READ_ONLY } from './AGGREGATE';
|
||||
// export { FIRST_KEY_INDEX, IS_READ_ONLY } from './AGGREGATE';
|
||||
|
||||
interface AggregateWithCursorOptions extends AggregateOptions {
|
||||
COUNT?: number;
|
||||
// interface AggregateWithCursorOptions extends AggregateOptions {
|
||||
// COUNT?: number;
|
||||
// }
|
||||
|
||||
// export function transformArguments(
|
||||
// index: string,
|
||||
// query: string,
|
||||
// options?: AggregateWithCursorOptions
|
||||
// ) {
|
||||
// const args = transformAggregateArguments(index, query, options);
|
||||
|
||||
// args.push('WITHCURSOR');
|
||||
// if (options?.COUNT) {
|
||||
// args.push('COUNT', options.COUNT.toString());
|
||||
// }
|
||||
|
||||
// return args;
|
||||
// }
|
||||
|
||||
// type AggregateWithCursorRawReply = [
|
||||
// result: AggregateRawReply,
|
||||
// cursor: number
|
||||
// ];
|
||||
|
||||
// interface AggregateWithCursorReply extends AggregateReply {
|
||||
// cursor: number;
|
||||
// }
|
||||
|
||||
// export function transformReply(reply: AggregateWithCursorRawReply): AggregateWithCursorReply {
|
||||
// return {
|
||||
// ...transformAggregateReply(reply[0]),
|
||||
// cursor: reply[1]
|
||||
// };
|
||||
// }
|
||||
|
||||
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import AGGREGATE, { FtAggregateOptions } from './AGGREGATE';
|
||||
|
||||
export interface FtAggregateWithCursorOptions extends FtAggregateOptions {
|
||||
COUNT?: number;
|
||||
MAXIDLE?: number;
|
||||
}
|
||||
|
||||
export function transformArguments(
|
||||
index: string,
|
||||
query: string,
|
||||
options?: AggregateWithCursorOptions
|
||||
) {
|
||||
const args = transformAggregateArguments(index, query, options);
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: AGGREGATE.FIRST_KEY_INDEX,
|
||||
IS_READ_ONLY: AGGREGATE.IS_READ_ONLY,
|
||||
transformArguments(index: RedisArgument, query: RedisArgument, options?: FtAggregateWithCursorOptions) {
|
||||
const args = AGGREGATE.transformArguments(index, query, options);
|
||||
args.push('WITHCURSOR');
|
||||
if (options?.COUNT) {
|
||||
args.push('COUNT', options.COUNT.toString());
|
||||
|
||||
if (options?.COUNT !== undefined) {
|
||||
args.push('COUNT', options.COUNT.toString());
|
||||
}
|
||||
|
||||
if(options?.MAXIDLE !== undefined) {
|
||||
args.push('MAXIDLE', options.MAXIDLE.toString());
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
},
|
||||
transformReply: undefined as unknown as () => any
|
||||
} as const satisfies Command;
|
||||
|
||||
type AggregateWithCursorRawReply = [
|
||||
result: AggregateRawReply,
|
||||
cursor: number
|
||||
];
|
||||
|
||||
interface AggregateWithCursorReply extends AggregateReply {
|
||||
cursor: number;
|
||||
}
|
||||
|
||||
export function transformReply(reply: AggregateWithCursorRawReply): AggregateWithCursorReply {
|
||||
return {
|
||||
...transformAggregateReply(reply[0]),
|
||||
cursor: reply[1]
|
||||
};
|
||||
}
|
||||
|
@@ -1,6 +1,5 @@
|
||||
import { RedisArgument, SimpleStringReply, Command, CommandArguments } from '@redis/client/dist/lib/RESP/types';
|
||||
import { RedisVariadicArgument, pushOptionalVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
|
||||
import { PropertyName } from '.';
|
||||
|
||||
export const SCHEMA_FIELD_TYPE = {
|
||||
TEXT: 'TEXT',
|
||||
@@ -257,14 +256,16 @@ export const REDISEARCH_LANGUAGE = {
|
||||
|
||||
export type RediSearchLanguage = typeof REDISEARCH_LANGUAGE[keyof typeof REDISEARCH_LANGUAGE];
|
||||
|
||||
export type RediSearchProperty = `${'@' | '$.'}${string}`;
|
||||
|
||||
export interface CreateOptions {
|
||||
ON?: 'HASH' | 'JSON';
|
||||
PREFIX?: RedisVariadicArgument;
|
||||
FILTER?: RedisArgument;
|
||||
LANGUAGE?: RediSearchLanguage;
|
||||
LANGUAGE_FIELD?: PropertyName;
|
||||
LANGUAGE_FIELD?: RediSearchProperty;
|
||||
SCORE?: number;
|
||||
SCORE_FIELD?: PropertyName;
|
||||
SCORE_FIELD?: RediSearchProperty;
|
||||
// PAYLOAD_FIELD?: string;
|
||||
MAXTEXTFIELDS?: boolean;
|
||||
TEMPORARY?: number;
|
||||
|
@@ -1,300 +1,327 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import { RedisSearchLanguages, SchemaFieldTypes } from '.';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './SEARCH';
|
||||
import SEARCH from './SEARCH';
|
||||
|
||||
describe('SEARCH', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query'),
|
||||
['FT.SEARCH', 'index', 'query']
|
||||
);
|
||||
});
|
||||
|
||||
it('with VERBATIM', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { VERBATIM: true }),
|
||||
['FT.SEARCH', 'index', 'query', 'VERBATIM']
|
||||
);
|
||||
});
|
||||
|
||||
it('with NOSTOPWORDS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { NOSTOPWORDS: true }),
|
||||
['FT.SEARCH', 'index', 'query', 'NOSTOPWORDS']
|
||||
);
|
||||
});
|
||||
|
||||
it('with INKEYS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { INKEYS: 'key' }),
|
||||
['FT.SEARCH', 'index', 'query', 'INKEYS', '1', 'key']
|
||||
);
|
||||
});
|
||||
|
||||
it('with INFIELDS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { INFIELDS: 'field' }),
|
||||
['FT.SEARCH', 'index', 'query', 'INFIELDS', '1', 'field']
|
||||
);
|
||||
});
|
||||
|
||||
it('with RETURN', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { RETURN: 'return' }),
|
||||
['FT.SEARCH', 'index', 'query', 'RETURN', '1', 'return']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with SUMMARIZE', () => {
|
||||
it('true', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { SUMMARIZE: true }),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with FIELDS', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
FIELDS: ['@field']
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FIELDS', '1', '@field']
|
||||
);
|
||||
});
|
||||
|
||||
it('Array', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
FIELDS: ['@1', '@2']
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FIELDS', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with FRAGS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
FRAGS: 1
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FRAGS', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with LEN', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
LEN: 1
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'LEN', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with SEPARATOR', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
SEPARATOR: 'separator'
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'SEPARATOR', 'separator']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with HIGHLIGHT', () => {
|
||||
it('true', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { HIGHLIGHT: true }),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with FIELDS', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
HIGHLIGHT: {
|
||||
FIELDS: ['@field']
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'FIELDS', '1', '@field']
|
||||
);
|
||||
});
|
||||
|
||||
it('Array', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
HIGHLIGHT: {
|
||||
FIELDS: ['@1', '@2']
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'FIELDS', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with TAGS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
HIGHLIGHT: {
|
||||
TAGS: {
|
||||
open: 'open',
|
||||
close: 'close'
|
||||
}
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'TAGS', 'open', 'close']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with SLOP', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { SLOP: 1 }),
|
||||
['FT.SEARCH', 'index', 'query', 'SLOP', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with INORDER', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { INORDER: true }),
|
||||
['FT.SEARCH', 'index', 'query', 'INORDER']
|
||||
);
|
||||
});
|
||||
|
||||
it('with LANGUAGE', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { LANGUAGE: RedisSearchLanguages.ARABIC }),
|
||||
['FT.SEARCH', 'index', 'query', 'LANGUAGE', RedisSearchLanguages.ARABIC]
|
||||
);
|
||||
});
|
||||
|
||||
it('with EXPANDER', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { EXPANDER: 'expender' }),
|
||||
['FT.SEARCH', 'index', 'query', 'EXPANDER', 'expender']
|
||||
);
|
||||
});
|
||||
|
||||
it('with SCORER', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { SCORER: 'scorer' }),
|
||||
['FT.SEARCH', 'index', 'query', 'SCORER', 'scorer']
|
||||
);
|
||||
});
|
||||
|
||||
it('with SORTBY', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', { SORTBY: '@by' }),
|
||||
['FT.SEARCH', 'index', 'query', 'SORTBY', '@by']
|
||||
);
|
||||
});
|
||||
|
||||
it('with LIMIT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
LIMIT: {
|
||||
from: 0,
|
||||
size: 1
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'LIMIT', '0', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with PARAMS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
PARAMS: {
|
||||
param: 'value'
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'PARAMS', '2', 'param', 'value']
|
||||
);
|
||||
});
|
||||
|
||||
it('with DIALECT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
DIALECT: 1
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'DIALECT', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with TIMEOUT', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query', {
|
||||
TIMEOUT: 5
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'TIMEOUT', '5']
|
||||
);
|
||||
});
|
||||
describe('FT.SEARCH', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query'),
|
||||
['FT.SEARCH', 'index', 'query']
|
||||
);
|
||||
});
|
||||
|
||||
describe('client.ft.search', () => {
|
||||
testUtils.testWithClient('without optional options', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: SchemaFieldTypes.NUMERIC
|
||||
}),
|
||||
client.hSet('1', 'field', '1')
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.search('index', '*'),
|
||||
{
|
||||
total: 1,
|
||||
documents: [{
|
||||
id: '1',
|
||||
value: Object.create(null, {
|
||||
field: {
|
||||
value: '1',
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
}
|
||||
})
|
||||
}]
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('RETURN []', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: SchemaFieldTypes.NUMERIC
|
||||
}),
|
||||
client.hSet('1', 'field', '1'),
|
||||
client.hSet('2', 'field', '2')
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.search('index', '*', {
|
||||
RETURN: []
|
||||
}),
|
||||
{
|
||||
total: 2,
|
||||
documents: [{
|
||||
id: '1',
|
||||
value: Object.create(null)
|
||||
}, {
|
||||
id: '2',
|
||||
value: Object.create(null)
|
||||
}]
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
it('with VERBATIM', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
VERBATIM: true
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'VERBATIM']
|
||||
);
|
||||
});
|
||||
|
||||
it('with NOSTOPWORDS', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
NOSTOPWORDS: true
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'NOSTOPWORDS']
|
||||
);
|
||||
});
|
||||
|
||||
it('with INKEYS', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
INKEYS: 'key'
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'INKEYS', '1', 'key']
|
||||
);
|
||||
});
|
||||
|
||||
it('with INFIELDS', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
INFIELDS: 'field'
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'INFIELDS', '1', 'field']
|
||||
);
|
||||
});
|
||||
|
||||
it('with RETURN', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
RETURN: 'return'
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'RETURN', '1', 'return']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with SUMMARIZE', () => {
|
||||
it('true', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SUMMARIZE: true
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with FIELDS', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
FIELDS: '@field'
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FIELDS', '1', '@field']
|
||||
);
|
||||
});
|
||||
|
||||
it('Array', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
FIELDS: ['@1', '@2']
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FIELDS', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with FRAGS', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
FRAGS: 1
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FRAGS', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with LEN', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
LEN: 1
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'LEN', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with SEPARATOR', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SUMMARIZE: {
|
||||
SEPARATOR: 'separator'
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'SEPARATOR', 'separator']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with HIGHLIGHT', () => {
|
||||
it('true', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
HIGHLIGHT: true
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT']
|
||||
);
|
||||
});
|
||||
|
||||
describe('with FIELDS', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
HIGHLIGHT: {
|
||||
FIELDS: ['@field']
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'FIELDS', '1', '@field']
|
||||
);
|
||||
});
|
||||
|
||||
it('Array', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
HIGHLIGHT: {
|
||||
FIELDS: ['@1', '@2']
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'FIELDS', '2', '@1', '@2']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with TAGS', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
HIGHLIGHT: {
|
||||
TAGS: {
|
||||
open: 'open',
|
||||
close: 'close'
|
||||
}
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'TAGS', 'open', 'close']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('with SLOP', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SLOP: 1
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SLOP', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with TIMEOUT', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
TIMEOUT: 1
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'TIMEOUT', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with INORDER', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
INORDER: true
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'INORDER']
|
||||
);
|
||||
});
|
||||
|
||||
it('with LANGUAGE', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
LANGUAGE: 'Arabic'
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'LANGUAGE', 'Arabic']
|
||||
);
|
||||
});
|
||||
|
||||
it('with EXPANDER', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
EXPANDER: 'expender'
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'EXPANDER', 'expender']
|
||||
);
|
||||
});
|
||||
|
||||
it('with SCORER', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SCORER: 'scorer'
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SCORER', 'scorer']
|
||||
);
|
||||
});
|
||||
|
||||
it('with SORTBY', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
SORTBY: '@by'
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'SORTBY', '@by']
|
||||
);
|
||||
});
|
||||
|
||||
it('with LIMIT', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
LIMIT: {
|
||||
from: 0,
|
||||
size: 1
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'LIMIT', '0', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with PARAMS', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
PARAMS: {
|
||||
string: 'string',
|
||||
buffer: Buffer.from('buffer'),
|
||||
number: 1
|
||||
}
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'PARAMS', '6', 'string', 'string', 'buffer', Buffer.from('buffer'), 'number', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('with DIALECT', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH.transformArguments('index', 'query', {
|
||||
DIALECT: 1
|
||||
}),
|
||||
['FT.SEARCH', 'index', 'query', 'DIALECT', '1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('client.ft.search', () => {
|
||||
testUtils.testWithClient('without optional options', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: 'TEXT'
|
||||
}),
|
||||
client.hSet('1', 'field', '1')
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.search('index', '*'),
|
||||
{
|
||||
total: 1,
|
||||
documents: [{
|
||||
id: '1',
|
||||
value: Object.create(null, {
|
||||
field: {
|
||||
value: '1',
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
}
|
||||
})
|
||||
}]
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('RETURN []', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: 'TEXT'
|
||||
}),
|
||||
client.hSet('1', 'field', '1'),
|
||||
client.hSet('2', 'field', '2')
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.search('index', '*', {
|
||||
RETURN: []
|
||||
}),
|
||||
{
|
||||
total: 2,
|
||||
documents: [{
|
||||
id: '1',
|
||||
value: Object.create(null)
|
||||
}, {
|
||||
id: '2',
|
||||
value: Object.create(null)
|
||||
}]
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
});
|
||||
|
@@ -1,109 +1,162 @@
|
||||
import { RedisCommandArguments } from '@redis/client/dist/lib/commands';
|
||||
import { pushSearchOptions, RedisSearchLanguages, Params, PropertyName, SortByProperty, SearchReply } from '.';
|
||||
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import { RedisVariadicArgument, pushOptionalVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
|
||||
import { RediSearchProperty, RediSearchLanguage } from './CREATE';
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
export type FtSearchParams = Record<string, RedisArgument | number>;
|
||||
|
||||
export const IS_READ_ONLY = true;
|
||||
export function pushParamsArgument(args: Array<RedisArgument>, params?: FtSearchParams) {
|
||||
if (params) {
|
||||
const length = args.push('PARAMS', '');
|
||||
for (const key in params) {
|
||||
if (!Object.hasOwn(params, key)) continue;
|
||||
|
||||
export interface SearchOptions {
|
||||
VERBATIM?: true;
|
||||
NOSTOPWORDS?: true;
|
||||
// WITHSCORES?: true;
|
||||
// WITHPAYLOADS?: true;
|
||||
WITHSORTKEYS?: true;
|
||||
// FILTER?: {
|
||||
// field: string;
|
||||
// min: number | string;
|
||||
// max: number | string;
|
||||
// };
|
||||
// GEOFILTER?: {
|
||||
// field: string;
|
||||
// lon: number;
|
||||
// lat: number;
|
||||
// radius: number;
|
||||
// unit: 'm' | 'km' | 'mi' | 'ft';
|
||||
// };
|
||||
INKEYS?: string | Array<string>;
|
||||
INFIELDS?: string | Array<string>;
|
||||
RETURN?: string | Array<string>;
|
||||
SUMMARIZE?: true | {
|
||||
FIELDS?: PropertyName | Array<PropertyName>;
|
||||
FRAGS?: number;
|
||||
LEN?: number;
|
||||
SEPARATOR?: string;
|
||||
};
|
||||
HIGHLIGHT?: true | {
|
||||
FIELDS?: PropertyName | Array<PropertyName>;
|
||||
TAGS?: {
|
||||
open: string;
|
||||
close: string;
|
||||
};
|
||||
};
|
||||
SLOP?: number;
|
||||
INORDER?: true;
|
||||
LANGUAGE?: RedisSearchLanguages;
|
||||
EXPANDER?: string;
|
||||
SCORER?: string;
|
||||
// EXPLAINSCORE?: true; // TODO: WITHSCORES
|
||||
// PAYLOAD?: ;
|
||||
SORTBY?: SortByProperty;
|
||||
// MSORTBY?: SortByProperty | Array<SortByProperty>;
|
||||
LIMIT?: {
|
||||
from: number | string;
|
||||
size: number | string;
|
||||
};
|
||||
PARAMS?: Params;
|
||||
DIALECT?: number;
|
||||
TIMEOUT?: number;
|
||||
}
|
||||
|
||||
export function transformArguments(
|
||||
index: string,
|
||||
query: string,
|
||||
options?: SearchOptions
|
||||
): RedisCommandArguments {
|
||||
return pushSearchOptions(
|
||||
['FT.SEARCH', index, query],
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
export type SearchRawReply = Array<any>;
|
||||
|
||||
export function transformReply(reply: SearchRawReply, withoutDocuments: boolean): SearchReply {
|
||||
const documents = [];
|
||||
let i = 1;
|
||||
while (i < reply.length) {
|
||||
documents.push({
|
||||
id: reply[i++],
|
||||
value: withoutDocuments ? Object.create(null) : documentValue(reply[i++])
|
||||
});
|
||||
const value = params[key];
|
||||
args.push(
|
||||
key,
|
||||
typeof value === 'number' ? value.toString() : value
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
total: reply[0],
|
||||
documents
|
||||
};
|
||||
args[length - 1] = (args.length - length).toString();
|
||||
}
|
||||
}
|
||||
|
||||
function documentValue(tuples: any) {
|
||||
const message = Object.create(null);
|
||||
export interface FtSearchOptions {
|
||||
VERBATIM?: boolean;
|
||||
NOSTOPWORDS?: boolean;
|
||||
INKEYS?: RedisVariadicArgument;
|
||||
INFIELDS?: RedisVariadicArgument;
|
||||
RETURN?: RedisVariadicArgument;
|
||||
SUMMARIZE?: boolean | {
|
||||
FIELDS?: RediSearchProperty | Array<RediSearchProperty>;
|
||||
FRAGS?: number;
|
||||
LEN?: number;
|
||||
SEPARATOR?: RedisArgument;
|
||||
};
|
||||
HIGHLIGHT?: boolean | {
|
||||
FIELDS?: RediSearchProperty | Array<RediSearchProperty>;
|
||||
TAGS?: {
|
||||
open: RedisArgument;
|
||||
close: RedisArgument;
|
||||
};
|
||||
};
|
||||
SLOP?: number;
|
||||
TIMEOUT?: number;
|
||||
INORDER?: boolean;
|
||||
LANGUAGE?: RediSearchLanguage;
|
||||
EXPANDER?: RedisArgument;
|
||||
SCORER?: RedisArgument;
|
||||
SORTBY?: RedisArgument | {
|
||||
BY: RediSearchProperty;
|
||||
DIRECTION?: 'ASC' | 'DESC';
|
||||
};
|
||||
LIMIT?: {
|
||||
from: number | RedisArgument;
|
||||
size: number | RedisArgument;
|
||||
};
|
||||
PARAMS?: FtSearchParams;
|
||||
DIALECT?: number;
|
||||
}
|
||||
|
||||
let i = 0;
|
||||
while (i < tuples.length) {
|
||||
const key = tuples[i++],
|
||||
value = tuples[i++];
|
||||
if (key === '$') { // might be a JSON reply
|
||||
try {
|
||||
Object.assign(message, JSON.parse(value));
|
||||
continue;
|
||||
} catch {
|
||||
// set as a regular property if not a valid JSON
|
||||
}
|
||||
export default {
|
||||
FIRST_KEY_INDEX: undefined,
|
||||
IS_READ_ONLY: true,
|
||||
transformArguments(index: RedisArgument, query: RedisArgument, options?: FtSearchOptions) {
|
||||
const args = ['FT.SEARCH', index, query];
|
||||
|
||||
if (options?.VERBATIM) {
|
||||
args.push('VERBATIM');
|
||||
}
|
||||
|
||||
if (options?.NOSTOPWORDS) {
|
||||
args.push('NOSTOPWORDS');
|
||||
}
|
||||
|
||||
pushOptionalVariadicArgument(args, 'INKEYS', options?.INKEYS);
|
||||
pushOptionalVariadicArgument(args, 'INFIELDS', options?.INFIELDS);
|
||||
pushOptionalVariadicArgument(args, 'RETURN', options?.RETURN);
|
||||
|
||||
if (options?.SUMMARIZE) {
|
||||
args.push('SUMMARIZE');
|
||||
|
||||
if (typeof options.SUMMARIZE === 'object') {
|
||||
pushOptionalVariadicArgument(args, 'FIELDS', options.SUMMARIZE.FIELDS);
|
||||
|
||||
if (options.SUMMARIZE.FRAGS !== undefined) {
|
||||
args.push('FRAGS', options.SUMMARIZE.FRAGS.toString());
|
||||
}
|
||||
|
||||
message[key] = value;
|
||||
if (options.SUMMARIZE.LEN !== undefined) {
|
||||
args.push('LEN', options.SUMMARIZE.LEN.toString());
|
||||
}
|
||||
|
||||
if (options.SUMMARIZE.SEPARATOR !== undefined) {
|
||||
args.push('SEPARATOR', options.SUMMARIZE.SEPARATOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
if (options?.HIGHLIGHT) {
|
||||
args.push('HIGHLIGHT');
|
||||
|
||||
if (typeof options.HIGHLIGHT === 'object') {
|
||||
pushOptionalVariadicArgument(args, 'FIELDS', options.HIGHLIGHT.FIELDS);
|
||||
|
||||
if (options.HIGHLIGHT.TAGS) {
|
||||
args.push('TAGS', options.HIGHLIGHT.TAGS.open, options.HIGHLIGHT.TAGS.close);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options?.SLOP !== undefined) {
|
||||
args.push('SLOP', options.SLOP.toString());
|
||||
}
|
||||
|
||||
if (options?.TIMEOUT !== undefined) {
|
||||
args.push('TIMEOUT', options.TIMEOUT.toString());
|
||||
}
|
||||
|
||||
if (options?.INORDER) {
|
||||
args.push('INORDER');
|
||||
}
|
||||
|
||||
if (options?.LANGUAGE) {
|
||||
args.push('LANGUAGE', options.LANGUAGE);
|
||||
}
|
||||
|
||||
if (options?.EXPANDER) {
|
||||
args.push('EXPANDER', options.EXPANDER);
|
||||
}
|
||||
|
||||
if (options?.SCORER) {
|
||||
args.push('SCORER', options.SCORER);
|
||||
}
|
||||
|
||||
if (options?.SORTBY) {
|
||||
args.push('SORTBY');
|
||||
|
||||
if (typeof options.SORTBY === 'string' || options.SORTBY instanceof Buffer) {
|
||||
args.push(options.SORTBY);
|
||||
} else {
|
||||
args.push(options.SORTBY.BY);
|
||||
|
||||
if (options.SORTBY.DIRECTION) {
|
||||
args.push(options.SORTBY.DIRECTION);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options?.LIMIT) {
|
||||
args.push('LIMIT', options.LIMIT.from.toString(), options.LIMIT.size.toString());
|
||||
}
|
||||
|
||||
pushParamsArgument(args, options?.PARAMS);
|
||||
|
||||
if (options?.DIALECT !== undefined) {
|
||||
args.push('DIALECT', options.DIALECT.toString());
|
||||
}
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: undefined as unknown as () => any
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,45 +1,34 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import { SchemaFieldTypes } from '.';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments, transformReply } from './SEARCH_NOCONTENT';
|
||||
import SEARCH_NOCONTENT from './SEARCH_NOCONTENT';
|
||||
|
||||
describe('SEARCH_NOCONTENT', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('index', 'query'),
|
||||
['FT.SEARCH', 'index', 'query', 'NOCONTENT']
|
||||
);
|
||||
});
|
||||
describe('FT.SEARCH NOCONTENT', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without options', () => {
|
||||
assert.deepEqual(
|
||||
SEARCH_NOCONTENT.transformArguments('index', 'query'),
|
||||
['FT.SEARCH', 'index', 'query', 'NOCONTENT']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transformReply', () => {
|
||||
it('returns total and keys', () => {
|
||||
assert.deepEqual(transformReply([3, '1', '2', '3']), {
|
||||
total: 3,
|
||||
documents: ['1', '2', '3']
|
||||
})
|
||||
});
|
||||
});
|
||||
describe('client.ft.searchNoContent', () => {
|
||||
testUtils.testWithClient('returns total and keys', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: 'TEXT'
|
||||
}),
|
||||
client.hSet('1', 'field', 'field1'),
|
||||
client.hSet('2', 'field', 'field2')
|
||||
]);
|
||||
|
||||
describe('client.ft.searchNoContent', () => {
|
||||
testUtils.testWithClient('returns total and keys', async client => {
|
||||
await Promise.all([
|
||||
client.ft.create('index', {
|
||||
field: SchemaFieldTypes.TEXT
|
||||
}),
|
||||
client.hSet('1', 'field', 'field1'),
|
||||
client.hSet('2', 'field', 'field2'),
|
||||
client.hSet('3', 'field', 'field3')
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
await client.ft.searchNoContent('index', '*'),
|
||||
{
|
||||
total: 3,
|
||||
documents: ['1','2','3']
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
assert.deepEqual(
|
||||
await client.ft.searchNoContent('index', '*'),
|
||||
{
|
||||
total: 2,
|
||||
documents: ['1', '2']
|
||||
}
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
});
|
||||
|
@@ -1,30 +1,13 @@
|
||||
import { RedisCommandArguments } from "@redis/client/dist/lib/commands";
|
||||
import { pushSearchOptions } from ".";
|
||||
import { SearchOptions, SearchRawReply } from "./SEARCH";
|
||||
import { Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import SEARCH from './SEARCH';
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(
|
||||
index: string,
|
||||
query: string,
|
||||
options?: SearchOptions
|
||||
): RedisCommandArguments {
|
||||
return pushSearchOptions(
|
||||
['FT.SEARCH', index, query, 'NOCONTENT'],
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
export interface SearchNoContentReply {
|
||||
total: number;
|
||||
documents: Array<string>;
|
||||
};
|
||||
|
||||
export function transformReply(reply: SearchRawReply): SearchNoContentReply {
|
||||
return {
|
||||
total: reply[0],
|
||||
documents: reply.slice(1)
|
||||
};
|
||||
}
|
||||
export default {
|
||||
FIRST_KEY_INDEX: SEARCH.FIRST_KEY_INDEX,
|
||||
IS_READ_ONLY: SEARCH.IS_READ_ONLY,
|
||||
transformArguments(...args: Parameters<typeof SEARCH.transformArguments>) {
|
||||
const redisArgs = SEARCH.transformArguments(...args);
|
||||
redisArgs.push('NOCONTENT');
|
||||
return redisArgs;
|
||||
},
|
||||
transformReply: undefined as unknown as () => any
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import _LIST from './_LIST';
|
||||
import ALTER from './ALTER';
|
||||
// import AGGREGATE_WITHCURSOR from './AGGREGATE_WITHCURSOR';
|
||||
// import AGGREGATE from './AGGREGATE';
|
||||
import AGGREGATE_WITHCURSOR from './AGGREGATE_WITHCURSOR';
|
||||
import AGGREGATE from './AGGREGATE';
|
||||
import ALIASADD from './ALIASADD';
|
||||
import ALIASDEL from './ALIASDEL';
|
||||
import ALIASUPDATE from './ALIASUPDATE';
|
||||
@@ -19,7 +19,8 @@ import DROPINDEX from './DROPINDEX';
|
||||
// import INFO from './INFO';
|
||||
// import PROFILESEARCH from './PROFILE_SEARCH';
|
||||
// import PROFILEAGGREGATE from './PROFILE_AGGREGATE';
|
||||
// import SEARCH from './SEARCH';
|
||||
import SEARCH_NOCONTENT from './SEARCH_NOCONTENT';
|
||||
import SEARCH from './SEARCH';
|
||||
import SPELLCHECK from './SPELLCHECK';
|
||||
import SUGADD from './SUGADD';
|
||||
import SUGDEL from './SUGDEL';
|
||||
@@ -31,6 +32,7 @@ import SUGLEN from './SUGLEN';
|
||||
import SYNDUMP from './SYNDUMP';
|
||||
import SYNUPDATE from './SYNUPDATE';
|
||||
import TAGVALS from './TAGVALS';
|
||||
import { RedisArgument } from '@redis/client';
|
||||
// import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
|
||||
// import { pushOptionalVariadicArgument, pushVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
|
||||
// import { SearchOptions } from './SEARCH';
|
||||
@@ -41,10 +43,10 @@ export default {
|
||||
_list: _LIST,
|
||||
ALTER,
|
||||
alter: ALTER,
|
||||
// AGGREGATE_WITHCURSOR,
|
||||
// aggregateWithCursor: AGGREGATE_WITHCURSOR,
|
||||
// AGGREGATE,
|
||||
// aggregate: AGGREGATE,
|
||||
AGGREGATE_WITHCURSOR,
|
||||
aggregateWithCursor: AGGREGATE_WITHCURSOR,
|
||||
AGGREGATE,
|
||||
aggregate: AGGREGATE,
|
||||
ALIASADD,
|
||||
aliasAdd: ALIASADD,
|
||||
ALIASDEL,
|
||||
@@ -79,8 +81,10 @@ export default {
|
||||
// profileSearch: PROFILESEARCH,
|
||||
// PROFILEAGGREGATE,
|
||||
// profileAggregate: PROFILEAGGREGATE,
|
||||
// SEARCH,
|
||||
// search: SEARCH,
|
||||
SEARCH_NOCONTENT,
|
||||
searchNoContent: SEARCH_NOCONTENT,
|
||||
SEARCH,
|
||||
search: SEARCH,
|
||||
SPELLCHECK,
|
||||
spellCheck: SPELLCHECK,
|
||||
SUGADD,
|
||||
@@ -104,539 +108,3 @@ export default {
|
||||
TAGVALS,
|
||||
tagVals: TAGVALS
|
||||
};
|
||||
|
||||
// export enum RedisSearchLanguages {
|
||||
// ARABIC = 'Arabic',
|
||||
// BASQUE = 'Basque',
|
||||
// CATALANA = 'Catalan',
|
||||
// DANISH = 'Danish',
|
||||
// DUTCH = 'Dutch',
|
||||
// ENGLISH = 'English',
|
||||
// FINNISH = 'Finnish',
|
||||
// FRENCH = 'French',
|
||||
// GERMAN = 'German',
|
||||
// GREEK = 'Greek',
|
||||
// HUNGARIAN = 'Hungarian',
|
||||
// INDONESAIN = 'Indonesian',
|
||||
// IRISH = 'Irish',
|
||||
// ITALIAN = 'Italian',
|
||||
// LITHUANIAN = 'Lithuanian',
|
||||
// NEPALI = 'Nepali',
|
||||
// NORWEIGAN = 'Norwegian',
|
||||
// PORTUGUESE = 'Portuguese',
|
||||
// ROMANIAN = 'Romanian',
|
||||
// RUSSIAN = 'Russian',
|
||||
// SPANISH = 'Spanish',
|
||||
// SWEDISH = 'Swedish',
|
||||
// TAMIL = 'Tamil',
|
||||
// TURKISH = 'Turkish',
|
||||
// CHINESE = 'Chinese'
|
||||
// }
|
||||
|
||||
export type PropertyName = `${'@' | '$.'}${string}`;
|
||||
|
||||
// export type SortByProperty = string | {
|
||||
// BY: string;
|
||||
// DIRECTION?: 'ASC' | 'DESC';
|
||||
// };
|
||||
|
||||
// export function pushSortByProperty(args: RedisCommandArguments, sortBy: SortByProperty): void {
|
||||
// if (typeof sortBy === 'string') {
|
||||
// args.push(sortBy);
|
||||
// } else {
|
||||
// args.push(sortBy.BY);
|
||||
|
||||
// if (sortBy.DIRECTION) {
|
||||
// args.push(sortBy.DIRECTION);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// export function pushSortByArguments(args: RedisCommandArguments, name: string, sortBy: SortByProperty | Array<SortByProperty>): RedisCommandArguments {
|
||||
// const lengthBefore = args.push(
|
||||
// name,
|
||||
// '' // will be overwritten
|
||||
// );
|
||||
|
||||
// if (Array.isArray(sortBy)) {
|
||||
// for (const field of sortBy) {
|
||||
// pushSortByProperty(args, field);
|
||||
// }
|
||||
// } else {
|
||||
// pushSortByProperty(args, sortBy);
|
||||
// }
|
||||
|
||||
// args[lengthBefore - 1] = (args.length - lengthBefore).toString();
|
||||
|
||||
// return args;
|
||||
// }
|
||||
|
||||
// export function pushArgumentsWithLength(args: CommandArguments, fn: (args: CommandArguments) => void) {
|
||||
// const lengthIndex = args.push('') - 1;
|
||||
// fn(args);
|
||||
// args[lengthIndex] = (args.length - lengthIndex - 1).toString();
|
||||
// return args;
|
||||
// }
|
||||
|
||||
// export enum SchemaFieldTypes {
|
||||
// TEXT = 'TEXT',
|
||||
// NUMERIC = 'NUMERIC',
|
||||
// GEO = 'GEO',
|
||||
// TAG = 'TAG',
|
||||
// VECTOR = 'VECTOR'
|
||||
// }
|
||||
|
||||
// type CreateSchemaField<
|
||||
// T extends SchemaFieldTypes,
|
||||
// E = Record<PropertyKey, unknown>
|
||||
// > = T | ({
|
||||
// type: T;
|
||||
// AS?: string;
|
||||
// } & E);
|
||||
|
||||
// type CreateSchemaCommonField<
|
||||
// T extends SchemaFieldTypes,
|
||||
// E = Record<PropertyKey, unknown>
|
||||
// > = CreateSchemaField<
|
||||
// T,
|
||||
// ({
|
||||
// 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 = CreateSchemaCommonField<SchemaFieldTypes.TEXT, {
|
||||
// NOSTEM?: true;
|
||||
// WEIGHT?: number;
|
||||
// PHONETIC?: SchemaTextFieldPhonetics;
|
||||
// WITHSUFFIXTRIE?: boolean;
|
||||
// }>;
|
||||
|
||||
// type CreateSchemaNumericField = CreateSchemaCommonField<SchemaFieldTypes.NUMERIC>;
|
||||
|
||||
// type CreateSchemaGeoField = CreateSchemaCommonField<SchemaFieldTypes.GEO>;
|
||||
|
||||
// type CreateSchemaTagField = CreateSchemaCommonField<SchemaFieldTypes.TAG, {
|
||||
// SEPARATOR?: string;
|
||||
// CASESENSITIVE?: true;
|
||||
// WITHSUFFIXTRIE?: boolean;
|
||||
// }>;
|
||||
|
||||
// export enum VectorAlgorithms {
|
||||
// FLAT = 'FLAT',
|
||||
// HNSW = 'HNSW'
|
||||
// }
|
||||
|
||||
// type CreateSchemaVectorField<
|
||||
// T extends VectorAlgorithms,
|
||||
// A extends Record<string, unknown>
|
||||
// > = CreateSchemaField<SchemaFieldTypes.VECTOR, {
|
||||
// ALGORITHM: T;
|
||||
// TYPE: string;
|
||||
// DIM: number;
|
||||
// DISTANCE_METRIC: 'L2' | 'IP' | 'COSINE';
|
||||
// INITIAL_CAP?: number;
|
||||
// } & A>;
|
||||
|
||||
// type CreateSchemaFlatVectorField = CreateSchemaVectorField<VectorAlgorithms.FLAT, {
|
||||
// BLOCK_SIZE?: number;
|
||||
// }>;
|
||||
|
||||
// type CreateSchemaHNSWVectorField = CreateSchemaVectorField<VectorAlgorithms.HNSW, {
|
||||
// M?: number;
|
||||
// EF_CONSTRUCTION?: number;
|
||||
// EF_RUNTIME?: number;
|
||||
// }>;
|
||||
|
||||
// export interface RediSearchSchema {
|
||||
// [field: string]:
|
||||
// CreateSchemaTextField |
|
||||
// CreateSchemaNumericField |
|
||||
// CreateSchemaGeoField |
|
||||
// CreateSchemaTagField |
|
||||
// CreateSchemaFlatVectorField |
|
||||
// CreateSchemaHNSWVectorField;
|
||||
// }
|
||||
|
||||
// export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema) {
|
||||
// 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 SchemaFieldTypes.TEXT:
|
||||
// if (fieldOptions.NOSTEM) {
|
||||
// args.push('NOSTEM');
|
||||
// }
|
||||
|
||||
// if (fieldOptions.WEIGHT) {
|
||||
// args.push('WEIGHT', fieldOptions.WEIGHT.toString());
|
||||
// }
|
||||
|
||||
// if (fieldOptions.PHONETIC) {
|
||||
// args.push('PHONETIC', fieldOptions.PHONETIC);
|
||||
// }
|
||||
|
||||
// if (fieldOptions.WITHSUFFIXTRIE) {
|
||||
// args.push('WITHSUFFIXTRIE');
|
||||
// }
|
||||
|
||||
// break;
|
||||
|
||||
// // case SchemaFieldTypes.NUMERIC:
|
||||
// // case SchemaFieldTypes.GEO:
|
||||
// // break;
|
||||
|
||||
// case SchemaFieldTypes.TAG:
|
||||
// if (fieldOptions.SEPARATOR) {
|
||||
// args.push('SEPARATOR', fieldOptions.SEPARATOR);
|
||||
// }
|
||||
|
||||
// if (fieldOptions.CASESENSITIVE) {
|
||||
// args.push('CASESENSITIVE');
|
||||
// }
|
||||
|
||||
// if (fieldOptions.WITHSUFFIXTRIE) {
|
||||
// args.push('WITHSUFFIXTRIE');
|
||||
// }
|
||||
|
||||
// break;
|
||||
|
||||
// case SchemaFieldTypes.VECTOR:
|
||||
// args.push(fieldOptions.ALGORITHM);
|
||||
|
||||
// pushArgumentsWithLength(args, () => {
|
||||
// args.push(
|
||||
// 'TYPE', fieldOptions.TYPE,
|
||||
// 'DIM', fieldOptions.DIM.toString(),
|
||||
// 'DISTANCE_METRIC', fieldOptions.DISTANCE_METRIC
|
||||
// );
|
||||
|
||||
// if (fieldOptions.INITIAL_CAP) {
|
||||
// args.push('INITIAL_CAP', fieldOptions.INITIAL_CAP.toString());
|
||||
// }
|
||||
|
||||
// switch (fieldOptions.ALGORITHM) {
|
||||
// case VectorAlgorithms.FLAT:
|
||||
// if (fieldOptions.BLOCK_SIZE) {
|
||||
// args.push('BLOCK_SIZE', fieldOptions.BLOCK_SIZE.toString());
|
||||
// }
|
||||
|
||||
// break;
|
||||
|
||||
// case VectorAlgorithms.HNSW:
|
||||
// if (fieldOptions.M) {
|
||||
// args.push('M', fieldOptions.M.toString());
|
||||
// }
|
||||
|
||||
// if (fieldOptions.EF_CONSTRUCTION) {
|
||||
// args.push('EF_CONSTRUCTION', fieldOptions.EF_CONSTRUCTION.toString());
|
||||
// }
|
||||
|
||||
// if (fieldOptions.EF_RUNTIME) {
|
||||
// args.push('EF_RUNTIME', fieldOptions.EF_RUNTIME.toString());
|
||||
// }
|
||||
|
||||
// break;
|
||||
// }
|
||||
// });
|
||||
|
||||
// continue; // vector fields do not contain SORTABLE and NOINDEX options
|
||||
// }
|
||||
|
||||
// if (fieldOptions.SORTABLE) {
|
||||
// args.push('SORTABLE');
|
||||
|
||||
// if (fieldOptions.SORTABLE === 'UNF') {
|
||||
// args.push('UNF');
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (fieldOptions.NOINDEX) {
|
||||
// args.push('NOINDEX');
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// export type Params = Record<string, RedisCommandArgument | number>;
|
||||
|
||||
// export function pushParamsArgs(
|
||||
// args: RedisCommandArguments,
|
||||
// params?: Params
|
||||
// ): RedisCommandArguments {
|
||||
// if (params) {
|
||||
// const enrties = Object.entries(params);
|
||||
// args.push('PARAMS', (enrties.length * 2).toString());
|
||||
// for (const [key, value] of enrties) {
|
||||
// args.push(key, typeof value === 'number' ? value.toString() : value);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return args;
|
||||
// }
|
||||
|
||||
// export function pushSearchOptions(
|
||||
// args: RedisCommandArguments,
|
||||
// options?: SearchOptions
|
||||
// ): RedisCommandArguments {
|
||||
// if (options?.VERBATIM) {
|
||||
// args.push('VERBATIM');
|
||||
// }
|
||||
|
||||
// if (options?.NOSTOPWORDS) {
|
||||
// args.push('NOSTOPWORDS');
|
||||
// }
|
||||
|
||||
// // if (options?.WITHSCORES) {
|
||||
// // args.push('WITHSCORES');
|
||||
// // }
|
||||
|
||||
// // if (options?.WITHPAYLOADS) {
|
||||
// // args.push('WITHPAYLOADS');
|
||||
// // }
|
||||
|
||||
// pushOptionalVariadicArgument(args, 'INKEYS', options?.INKEYS);
|
||||
// pushOptionalVariadicArgument(args, 'INFIELDS', options?.INFIELDS);
|
||||
// pushOptionalVariadicArgument(args, 'RETURN', options?.RETURN);
|
||||
|
||||
// if (options?.SUMMARIZE) {
|
||||
// args.push('SUMMARIZE');
|
||||
|
||||
// if (typeof options.SUMMARIZE === 'object') {
|
||||
// if (options.SUMMARIZE.FIELDS) {
|
||||
// args.push('FIELDS');
|
||||
// pushVariadicArgument(args, options.SUMMARIZE.FIELDS);
|
||||
// }
|
||||
|
||||
// if (options.SUMMARIZE.FRAGS) {
|
||||
// args.push('FRAGS', options.SUMMARIZE.FRAGS.toString());
|
||||
// }
|
||||
|
||||
// if (options.SUMMARIZE.LEN) {
|
||||
// args.push('LEN', options.SUMMARIZE.LEN.toString());
|
||||
// }
|
||||
|
||||
// if (options.SUMMARIZE.SEPARATOR) {
|
||||
// args.push('SEPARATOR', options.SUMMARIZE.SEPARATOR);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (options?.HIGHLIGHT) {
|
||||
// args.push('HIGHLIGHT');
|
||||
|
||||
// if (typeof options.HIGHLIGHT === 'object') {
|
||||
// if (options.HIGHLIGHT.FIELDS) {
|
||||
// args.push('FIELDS');
|
||||
// pushVariadicArgument(args, options.HIGHLIGHT.FIELDS);
|
||||
// }
|
||||
|
||||
// if (options.HIGHLIGHT.TAGS) {
|
||||
// args.push('TAGS', options.HIGHLIGHT.TAGS.open, options.HIGHLIGHT.TAGS.close);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (options?.SLOP) {
|
||||
// args.push('SLOP', options.SLOP.toString());
|
||||
// }
|
||||
|
||||
// if (options?.INORDER) {
|
||||
// args.push('INORDER');
|
||||
// }
|
||||
|
||||
// if (options?.LANGUAGE) {
|
||||
// args.push('LANGUAGE', options.LANGUAGE);
|
||||
// }
|
||||
|
||||
// if (options?.EXPANDER) {
|
||||
// args.push('EXPANDER', options.EXPANDER);
|
||||
// }
|
||||
|
||||
// if (options?.SCORER) {
|
||||
// args.push('SCORER', options.SCORER);
|
||||
// }
|
||||
|
||||
// // if (options?.EXPLAINSCORE) {
|
||||
// // args.push('EXPLAINSCORE');
|
||||
// // }
|
||||
|
||||
// // if (options?.PAYLOAD) {
|
||||
// // args.push('PAYLOAD', options.PAYLOAD);
|
||||
// // }
|
||||
|
||||
// if (options?.SORTBY) {
|
||||
// args.push('SORTBY');
|
||||
// pushSortByProperty(args, options.SORTBY);
|
||||
// }
|
||||
|
||||
// // if (options?.MSORTBY) {
|
||||
// // pushSortByArguments(args, 'MSORTBY', options.MSORTBY);
|
||||
// // }
|
||||
|
||||
// if (options?.LIMIT) {
|
||||
// args.push(
|
||||
// 'LIMIT',
|
||||
// options.LIMIT.from.toString(),
|
||||
// options.LIMIT.size.toString()
|
||||
// );
|
||||
// }
|
||||
|
||||
// if (options?.PARAMS) {
|
||||
// pushParamsArgs(args, options.PARAMS);
|
||||
// }
|
||||
|
||||
// if (options?.DIALECT) {
|
||||
// args.push('DIALECT', options.DIALECT.toString());
|
||||
// }
|
||||
|
||||
// if (options?.RETURN?.length === 0) {
|
||||
// args.preserve = true;
|
||||
// }
|
||||
|
||||
// if (options?.TIMEOUT !== undefined) {
|
||||
// args.push('TIMEOUT', options.TIMEOUT.toString());
|
||||
// }
|
||||
|
||||
// return args;
|
||||
// }
|
||||
|
||||
// interface SearchDocumentValue {
|
||||
// [key: string]: string | number | null | Array<SearchDocumentValue> | SearchDocumentValue;
|
||||
// }
|
||||
|
||||
// export interface SearchReply {
|
||||
// total: number;
|
||||
// documents: Array<{
|
||||
// id: string;
|
||||
// value: SearchDocumentValue;
|
||||
// }>;
|
||||
// }
|
||||
|
||||
// export interface ProfileOptions {
|
||||
// LIMITED?: true;
|
||||
// }
|
||||
|
||||
// export type ProfileRawReply<T> = [
|
||||
// results: T,
|
||||
// profile: [
|
||||
// _: string,
|
||||
// TotalProfileTime: string,
|
||||
// _: string,
|
||||
// ParsingTime: string,
|
||||
// _: string,
|
||||
// PipelineCreationTime: string,
|
||||
// _: string,
|
||||
// IteratorsProfile: Array<any>
|
||||
// ]
|
||||
// ];
|
||||
|
||||
// export interface ProfileReply {
|
||||
// results: SearchReply | AGGREGATE.AggregateReply;
|
||||
// profile: ProfileData;
|
||||
// }
|
||||
|
||||
// interface ChildIterator {
|
||||
// type?: string,
|
||||
// counter?: number,
|
||||
// term?: string,
|
||||
// size?: number,
|
||||
// time?: string,
|
||||
// childIterators?: Array<ChildIterator>
|
||||
// }
|
||||
|
||||
// interface IteratorsProfile {
|
||||
// type?: string,
|
||||
// counter?: number,
|
||||
// queryType?: string,
|
||||
// time?: string,
|
||||
// childIterators?: Array<ChildIterator>
|
||||
// }
|
||||
|
||||
// interface ProfileData {
|
||||
// totalProfileTime: string,
|
||||
// parsingTime: string,
|
||||
// pipelineCreationTime: string,
|
||||
// iteratorsProfile: IteratorsProfile
|
||||
// }
|
||||
|
||||
// export function transformProfile(reply: Array<any>): ProfileData {
|
||||
// return {
|
||||
// totalProfileTime: reply[0][1],
|
||||
// parsingTime: reply[1][1],
|
||||
// pipelineCreationTime: reply[2][1],
|
||||
// iteratorsProfile: transformIterators(reply[3][1])
|
||||
// };
|
||||
// }
|
||||
|
||||
// function transformIterators(IteratorsProfile: Array<any>): IteratorsProfile {
|
||||
// var res: IteratorsProfile = {};
|
||||
// for (let i = 0; i < IteratorsProfile.length; i += 2) {
|
||||
// const value = IteratorsProfile[i + 1];
|
||||
// switch (IteratorsProfile[i]) {
|
||||
// case 'Type':
|
||||
// res.type = value;
|
||||
// break;
|
||||
// case 'Counter':
|
||||
// res.counter = value;
|
||||
// break;
|
||||
// case 'Time':
|
||||
// res.time = value;
|
||||
// break;
|
||||
// case 'Query type':
|
||||
// res.queryType = value;
|
||||
// break;
|
||||
// case 'Child iterators':
|
||||
// res.childIterators = value.map(transformChildIterators);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return res;
|
||||
// }
|
||||
|
||||
// function transformChildIterators(IteratorsProfile: Array<any>): ChildIterator {
|
||||
// var res: ChildIterator = {};
|
||||
// for (let i = 1; i < IteratorsProfile.length; i += 2) {
|
||||
// const value = IteratorsProfile[i + 1];
|
||||
// switch (IteratorsProfile[i]) {
|
||||
// case 'Type':
|
||||
// res.type = value;
|
||||
// break;
|
||||
// case 'Counter':
|
||||
// res.counter = value;
|
||||
// break;
|
||||
// case 'Time':
|
||||
// res.time = value;
|
||||
// break;
|
||||
// case 'Size':
|
||||
// res.size = value;
|
||||
// break;
|
||||
// case 'Term':
|
||||
// res.term = value;
|
||||
// break;
|
||||
// case 'Child iterators':
|
||||
// res.childIterators = value.map(transformChildIterators);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return res;
|
||||
// }
|
||||
|
@@ -9,7 +9,7 @@
|
||||
"!dist/tsconfig.tsbuildinfo"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "nyc -r text-summary -r lcov mocha -r source-map-support/register -r ts-node/register './lib/**/*.spec.ts'"
|
||||
"test": "nyc -r text-summary -r lcov mocha -r tsx './lib/**/*.spec.ts'"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@redis/client": "*"
|
||||
|
Reference in New Issue
Block a user