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

Commit

Permalink
[RediSearch] Fix num parameter and spellcheck response object (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Legolaszstudio authored Sep 14, 2021
1 parent dd49da6 commit 33ea3e3
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 42 deletions.
2 changes: 2 additions & 0 deletions modules/redis-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { GearsCommander } from './redisgears/redisgears.commander';
import { GraphCommander } from './redisgraph/redisgraph.commander';
import { RejsonCommander } from './rejson/rejson.commander';
import { RedisTimeSeriesCommander } from './rts/rts.commander';
import { RedisearchHelpers } from './redisearch/redisearch.helpers';

export class RedisModules extends Module {
public bloomCommander = new BloomCommander()
Expand All @@ -41,6 +42,7 @@ export class RedisModules extends Module {
public rejsonCommander = new RejsonCommander()
public risCommander = new RedisIntervalSetsCommander()
public rtsCommander = new RedisTimeSeriesCommander()
public searchHelpers = new RedisearchHelpers();

/**
* Initializing the module object
Expand Down
87 changes: 52 additions & 35 deletions modules/redisearch/redisearch.commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export class SearchCommander {
if(parameters !== undefined) {
if(parameters.prefix !== undefined) {
args.push('PREFIX')
args.push(
parameters.prefix.num !== undefined ?
`${parameters.prefix.num}` :
`${parameters.prefix.prefixes.length}`
)
if(parameters.prefix.num !== undefined) {
args.push(`${parameters.prefix.num}`)
} else if(Array.isArray(parameters.prefix.prefixes)) {
args.push(`${parameters.prefix.prefixes.length}`)
} else {
args.push("1")
}
args = args.concat(parameters.prefix.prefixes);
}
if(parameters.filter !== undefined)
Expand Down Expand Up @@ -52,11 +54,13 @@ export class SearchCommander {
args.push('NOFREQS')
if(parameters.stopwords !== undefined) {
args.push('STOPWORDS')
args.push(
parameters.stopwords.num !== undefined ?
`${parameters.stopwords.num}` :
`${parameters.stopwords.stopwords.length}`
)
if(parameters.stopwords.num !== undefined) {
args.push(`${parameters.stopwords.num}`)
} else if(Array.isArray(parameters.stopwords.stopwords)) {
args.push(`${parameters.stopwords.stopwords.length}`)
} else {
args.push("1")
}
args = args.concat(parameters.stopwords.stopwords);
}
if(parameters.skipInitialScan === true)
Expand Down Expand Up @@ -119,20 +123,28 @@ export class SearchCommander {
`${parameters.geoFilter.radius}`,
parameters.geoFilter.measurement
])
if(parameters.inKeys !== undefined)
args = args.concat([
'INKEYS',
parameters.inKeys.num !== undefined ?
`${parameters.inKeys.num}` :
`${parameters.inKeys.keys.length}`,
]).concat(parameters.inKeys.keys);
if(parameters.inFields !== undefined)
args = args.concat([
'INFIELDS',
parameters.inFields.num !== undefined ?
`${parameters.inFields.num}` :
`${parameters.inFields.fields.length}`,
]).concat(parameters.inFields.fields);
if(parameters.inKeys !== undefined) {
args.push('INKEYS')
if(parameters.inKeys.num !== undefined) {
args.push(`${parameters.inKeys.num}`)
} else if(Array.isArray(parameters.inKeys.keys)) {
args.push(`${parameters.inKeys.keys.length}`)
} else {
args.push("1")
}
args = args.concat(parameters.inKeys.keys);
}
if(parameters.inFields !== undefined) {
args.push('INFIELDS');
if(parameters.inFields.num !== undefined) {
args.push(`${parameters.inFields.num}`)
} else if(Array.isArray(parameters.inFields.fields)) {
args.push(`${parameters.inFields.fields.length}`)
} else {
args.push("1")
}
args = args.concat(parameters.inFields.fields);
}
if(parameters.return !== undefined) {
args = args.concat([
'RETURN',
Expand All @@ -154,11 +166,13 @@ export class SearchCommander {
args.push('SUMMARIZE')
if(parameters.summarize.fields !== undefined) {
args.push('FIELDS')
args.push(
parameters.summarize.fields.num !== undefined ?
`${parameters.summarize.fields.num}` :
`${parameters.summarize.fields.fields.length}`
)
if(parameters.summarize.fields.num !== undefined) {
args.push(`${parameters.summarize.fields.num}`)
} else if(Array.isArray(parameters.summarize.fields.fields)) {
args.push(`${parameters.summarize.fields.fields.length}`)
} else {
args.push("1")
}
args = args.concat(parameters.summarize.fields.fields)
}
if(parameters.summarize.frags !== undefined)
Expand All @@ -171,12 +185,15 @@ export class SearchCommander {
if(parameters.highlight !== undefined) {
args.push('HIGHLIGHT')
if(parameters.highlight.fields !== undefined) {
args = args.concat([
'FIELDS',
parameters.highlight.fields.num !== undefined ?
`${parameters.highlight.fields.num}` :
`${parameters.highlight.fields.fields.length}`,
]).concat(parameters.highlight.fields.fields);
args.push('FIELDS');
if(parameters.highlight.fields.num !== undefined) {
args.push(`${parameters.highlight.fields.num}`)
} else if(Array.isArray(parameters.highlight.fields.fields)) {
args.push(`${parameters.highlight.fields.fields.length}`)
} else {
args.push("1")
}
args = args.concat(parameters.highlight.fields.fields);
}
if(parameters.highlight.tags !== undefined) {
args.push('TAGS')
Expand Down
25 changes: 25 additions & 0 deletions modules/redisearch/redisearch.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { FTSpellCheckResponse } from "./redisearch.types";

export class RedisearchHelpers {
/**
* Parses `spellcheck` response into a list of objects.
* @param response The response array from the spellcheck command
*/
handleSpellcheckResponse(response: any): FTSpellCheckResponse[] {
const output = [];
for(const term of response) {
output.push({
term: term[1],
suggestions: (term[2] as string[]).map(
function (suggestionArrayElem) {
return {
score: suggestionArrayElem[0],
suggestion: suggestionArrayElem[1]
}
}
)
});
}
return output;
}
}
9 changes: 6 additions & 3 deletions modules/redisearch/redisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { Module, RedisModuleOptions } from '../module.base';
import { SearchCommander } from './redisearch.commander';
import {
FTAggregateParameters, FTConfig, FTCreateParameters, FTFieldOptions, FTFieldType, FTIndexType, FTInfo, FTSchemaField,
FTSearchParameters, FTSpellCheck, FTSugAddParameters, FTSugGetParameters
FTSearchParameters, FTSpellCheck, FTSpellCheckResponse, FTSugAddParameters, FTSugGetParameters
} from './redisearch.types';
import { RedisearchHelpers } from './redisearch.helpers';

export class Redisearch extends Module {

private searchCommander = new SearchCommander();
private searchHelpers = new RedisearchHelpers();

/**
* Initializing the module object
* @param name The name of the module
Expand Down Expand Up @@ -251,10 +254,10 @@ export class Redisearch extends Module {
* @param options The additional optional parameters
* @returns An array, in which each element represents a misspelled term from the query
*/
async spellcheck(index: string, query: string, options?: FTSpellCheck): Promise<string[]> {
async spellcheck(index: string, query: string, options?: FTSpellCheck): Promise<FTSpellCheckResponse[]> {
const command = this.searchCommander.spellcheck(index, query, options);
const response = await this.sendCommand(command);
return this.handleResponse(response);
return this.searchHelpers.handleSpellcheckResponse(response);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions modules/redisearch/redisearch.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,27 @@ export interface FTInfo {
index_capacity?: number,
index_total?: number
}
}

/**
* The FT.SPELLCHECK response object
*/
export interface FTSpellCheckResponse {
/**
* The term that was spellchecked
*/
term: string,
/**
* Suggested corrections
*/
suggestions: {
/**
* Score of the suggestion
*/
score: string,
/**
* Score of the suggestion
*/
suggestion: string
}[],
}
17 changes: 13 additions & 4 deletions tests/redisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,19 @@ describe('RediSearch Module testing', async function () {
expect(response.term1).to.equal('0', 'The response of the FT.SYNDUMP command')
})
it('spellcheck function', async () => {
const response = await client.spellcheck(index, query, {
distance: 1
})
expect(response.length).to.be.greaterThan(0, 'The response of the FT.SPELLCHECK command')
await client.create(`${index}-spellcheck`, 'HASH', [{
name: "content",
type: "TEXT",
}], {
prefix: { prefixes: 'colors:' }
});
await client.redis.hset('colors:1', { content: 'red green blue yellow mellon' })

let response = await client.spellcheck(`${index}-spellcheck`, "redis")
expect(response[0].suggestions.length).to.equal(0, 'No suggestion should be found')

response = await client.spellcheck(`${index}-spellcheck`, "mellow blua")
expect(response.length).to.equal(2, 'Both word should be spellchecked')
})
it('dictadd function', async () => {
const response = await client.dictadd(dict.name, [dict.term])
Expand Down

0 comments on commit 33ea3e3

Please sign in to comment.