Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
Redisearch: Some return type fixes for redisearch methods (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhanveli authored May 30, 2021
1 parent 7cefed0 commit 8780c90
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
lib
lib
yarn.lock
package-lock.json
28 changes: 14 additions & 14 deletions modules/redisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class Redisearch extends Module {
* Creating an index with a given spec
* @param parameters The additional parameters of the spec
* @param schemaFields The filter set after the 'SCHEMA' argument
* @returns 'OK'
* @returns 'OK' or error
*/
async create(index: string, schemaFields: FTSchemaField[], parameters?: FTCreateParameters): Promise<'OK'> {
async create(index: string, schemaFields: FTSchemaField[], parameters?: FTCreateParameters): Promise<'OK' | string> {
let args: string[] = [index]
args = args.concat(['ON', 'HASH']);
if(parameters !== undefined) {
Expand Down Expand Up @@ -80,7 +80,7 @@ export class Redisearch extends Module {
* @param parameters The additional optional parameter
* @returns Array reply, where the first element is the total number of results, and then pairs of document id, and a nested array of field/value.
*/
async search(index: string, query: string, parameters?: FTSearchParameters): Promise<number> {
async search(index: string, query: string, parameters?: FTSearchParameters): Promise<[number, ...Array<string | string[]>]> {
let args: string[] = [index, query];
if(parameters !== undefined) {
if(parameters.noContent === true)
Expand Down Expand Up @@ -172,7 +172,7 @@ export class Redisearch extends Module {
* @param parameters The additional optional parameters
* @returns Array Response. Each row is an array and represents a single aggregate result
*/
async aggregate(index: string, query: string, parameters?: FTAggregateParameters): Promise<number> {
async aggregate(index: string, query: string, parameters?: FTAggregateParameters): Promise<[number, ...Array<string[]>]> {
let args: string[] = [index, query];
if(parameters !== undefined) {
if(parameters.load !== undefined) {
Expand Down Expand Up @@ -258,9 +258,9 @@ export class Redisearch extends Module {
* @param field The field name
* @param fieldType The field type
* @param options The additional optional parameters
* @returns 'OK'
* @returns 'OK' or error
*/
async alter(index: string, field: string, fieldType: FTFieldType, options?: FTFieldOptions): Promise<'OK'> {
async alter(index: string, field: string, fieldType: FTFieldType, options?: FTFieldOptions): Promise<'OK' | string> {
let args = [index, 'SCHEMA', 'ADD', field, fieldType]
if(options !== undefined) {
if(options.sortable !== undefined) args.push('SORTABLE');
Expand All @@ -278,9 +278,9 @@ export class Redisearch extends Module {
* Deleting the index
* @param index The index
* @param deleteHash If set, the drop operation will delete the actual document hashes.
* @returns 'OK'
* @returns 'OK' or error
*/
async dropindex(index: string, deleteHash = false): Promise<'OK'> {
async dropindex(index: string, deleteHash = false): Promise<'OK' | string> {
const args = [index];
if(deleteHash === true) args.push('DD')
const response = await this.sendCommand('FT.DROPINDEX', args);
Expand All @@ -291,9 +291,9 @@ export class Redisearch extends Module {
* Adding alias fron an index
* @param name The alias name
* @param index The alias index
* @returns 'OK'
* @returns 'OK' or error
*/
async aliasadd(name: string, index: string): Promise<'OK'> {
async aliasadd(name: string, index: string): Promise<'OK' | string> {
const response = await this.sendCommand('FT.ALIASADD', [name, index]);
return this.handleResponse(response);
}
Expand All @@ -302,19 +302,19 @@ export class Redisearch extends Module {
* Updating alias index
* @param name The alias name
* @param index The alias index
* @returns 'OK'
* @returns 'OK' or error
*/
async aliasupdate(name: string, index: string): Promise<'OK'> {
async aliasupdate(name: string, index: string): Promise<'OK' | string> {
const response = await this.sendCommand('FT.ALIASUPDATE', [name, index]);
return this.handleResponse(response);
}

/**
* Deleting alias fron an index
* @param name The alias name
* @returns 'OK'
* @returns 'OK' or error
*/
async aliasdel(name: string): Promise<'OK'> {
async aliasdel(name: string): Promise<'OK' | string> {
const response = await this.sendCommand('FT.ALIASDEL', [name]);
return this.handleResponse(response);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"redis-ai-module-tests": "npm run test tests/redis-ai.ts -- -- --host=127.0.0.1 --port=6379",
"ris-module-tests": "npm run test tests/ris.ts -- -- --host=127.0.0.1 --port=6379",
"redis-module-base-tests": "npm run test tests/module-base.ts",
"docker": "docker run --rm --name redis-mod-sdk -p 6379:6379 redislabs/redismod --port 6379 --loadmodule /usr/lib/redis/modules/redisai.so --loadmodule /usr/lib/redis/modules/redisbloom.so --loadmodule /usr/lib/redis/modules/redistimeseries.so --loadmodule /usr/lib/redis/modules/redisearch.so --loadmodule /usr/lib/redis/modules/redisgraph.so --loadmodule /usr/lib/redis/modules/rejson.so",
"tests": "npm run redisbloom-tdigest-filter-tests && npm run rejson-module-tests && npm run rts-module-tests && npm run redisearch-module-tests && npm run redisgraph-module-tests && npm run redisgears-module-tests && npm run redisbloom-module-tests && npm run redis-ai-tests",
"pre-deploy": "npm run build",
"deploy": "npm-deploy redis-modules-sdk"
Expand Down
74 changes: 64 additions & 10 deletions tests/redisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('RediSearch Module testing', async function() {
await client.disconnect();
await redis.disconnect();
})

it('create function', async () => {
let response = await client.create(index, [{
name: 'name',
Expand All @@ -55,9 +54,61 @@ describe('RediSearch Module testing', async function() {
const response = await client.search(index, query)
expect(response).to.equal(0, 'The response of the FT.SEARCH command')
});
it('search function response test', async () => {
await client.create(`${index}-searchtest`, [{
name: 'name',
type: 'TEXT'
}], {
prefix: [
{
count: 1,
name: 'doc'
}
]
})
await client.redis.hset('doc:1', { name: 'John Doe' });
await client.redis.hset('doc:2', { name: 'Jane Doe' });
await client.redis.hset('doc:3', { name: 'Sarah Brown' });
const [count, ...result] = await client.search(`${index}-searchtest`, '@name:Doe');
await client.dropindex(`${index}-searchtest`);
expect(count).to.equal(2, 'Total number of returining document of FT.SEARCH command')
expect(result[0].indexOf('doc')).to.equal(0, 'first document key')
});
it('aggregate function', async () => {
const response = await client.aggregate(index, query)
expect(response).to.equal(0, 'The response of the FT.SEARCH command')
expect(response).to.equal(0, 'The response of the FT.AGGREGATE command')
});
it('aggregate function response', async () => {
await client.create(`${index}-aggreagtetest`, [{
name: 'name',
type: 'TEXT'
},
{
name: 'city',
type: 'TEXT'
},
{
name: 'gender',
type: 'TAG'
}
], {
prefix: [
{
count: 1,
name: 'person'
}
]
})
await client.redis.hset('person:1', { name: 'John Doe', city: 'London', gender: 'male' });
await client.redis.hset('person:2', { name: 'Jane Doe', city: 'London', gender: 'female' });
await client.redis.hset('person:3', { name: 'Sarah Brown', city: 'New York', gender: 'female' });
await client.redis.hset('person:3', { name: 'Michael Doe', city: 'New York', gender: 'male' });
const [count, ...result] = await client.aggregate(`${index}-aggreagtetest`, 'Doe', {
groupby: { property: '@city', nargs: '1' }
});
await client.dropindex(`${index}-aggreagtetest`);
expect(count).to.equal(2, 'Total number of the FT.AGGREGATE command result');
expect(result[0][0]).to.equal('city', 'Aggreagated prop of the FT.AGGREGATE command result');
});
it('explain function', async () => {
const response = await client.explain(index, query)
Expand All @@ -71,7 +122,6 @@ describe('RediSearch Module testing', async function() {
const response = await client.alter(index, 'tags', 'TAG')
expect(response).to.equal('OK', 'The response of the FT.ALTER command');
});

it('aliasadd function', async () => {
const response = await client.aliasadd(alias, index)
expect(response).to.equal('OK', 'The response of the FT.ALIASADD command');
Expand All @@ -84,7 +134,6 @@ describe('RediSearch Module testing', async function() {
const response = await client.aliasdel(alias)
expect(response).to.equal('OK', 'The response of the FT.ALIASDEL command');
});

it('sugadd function', async () => {
const response = await client.sugadd(sug.key, sug.string, sug.score);
expect(response).to.equal(1, 'The response of the FT.SUGADD command');
Expand Down Expand Up @@ -118,18 +167,19 @@ describe('RediSearch Module testing', async function() {
expect(response.length).to.be.greaterThan(0, 'The response of the FT.SPELLCHECK command')
});
it('dictadd function', async () => {
let response = await client.dictadd(dict.name, [dict.term])
expect(response).to.equal(1, 'The response of the FT.DICTADD command');
response = await client.dictadd(`${dict.name}1`, [dict.term+'1'])
const response = await client.dictadd(dict.name, [dict.term]);
expect(response).to.equal(1, 'The response of the FT.DICTADD command');
});
it('dictdel function', async () => {
const response = await client.dictdel(dict.name, [dict.term])
await client.dictadd(dict.name, [dict.term]);
const response = await client.dictdel(dict.name, [dict.term]);
expect(response).to.equal(1, 'The response of the FT.DICDEL command');
});
it('dictdump function', async () => {
const response = await client.dictdump(`${dict.name}1`)
await client.dictadd(`${dict.name}1`, [`${dict.term}1`]);
const response = await client.dictdump(`${dict.name}1`);
expect(response).to.equal('termY1', 'The response of the FT.DICTDUMP command');
await client.dictdel(`${dict.name}1`, [`${dict.term}1`]);
});
it('info function', async () => {
const response = await client.info(index)
Expand All @@ -140,7 +190,11 @@ describe('RediSearch Module testing', async function() {
expect(response.EXTLOAD).to.equal(null, 'The EXTLOAD value');
});
it('dropindex function', async () => {
const response = await client.dropindex(index)
await client.create(`${index}-droptest`, [{
name: 'name',
type: 'TEXT'
}])
const response = await client.dropindex(`${index}-droptest`)
expect(response).to.equal('OK', 'The response of the FT.DROPINDEX command');
});
});

0 comments on commit 8780c90

Please sign in to comment.