Skip to content

Commit a217cc1

Browse files
brettburchleibale
andauthored
Add support for FT.SEARCH NOCONTENT (#2610)
* Add support for NOCONTENT in FT.SEARCH * Move support for NOCONTENT search option from client.search to client.searchNoContent * Add test for SEARCH_NOCONTENT#transformReply * Fix typo * Enable test * Update test field type --------- Co-authored-by: Leibale <[email protected]>
1 parent 1f97893 commit a217cc1

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

packages/search/lib/commands/SEARCH.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const FIRST_KEY_INDEX = 1;
66
export const IS_READ_ONLY = true;
77

88
export interface SearchOptions {
9-
// NOCONTENT?: true; TODO
109
VERBATIM?: true;
1110
NOSTOPWORDS?: true;
1211
// WITHSCORES?: true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { strict as assert } from 'assert';
2+
import { SchemaFieldTypes } from '.';
3+
import testUtils, { GLOBAL } from '../test-utils';
4+
import { transformArguments, transformReply } from './SEARCH_NOCONTENT';
5+
6+
describe('SEARCH_NOCONTENT', () => {
7+
describe('transformArguments', () => {
8+
it('without options', () => {
9+
assert.deepEqual(
10+
transformArguments('index', 'query'),
11+
['FT.SEARCH', 'index', 'query', 'NOCONTENT']
12+
);
13+
});
14+
});
15+
16+
describe('transformReply', () => {
17+
it('returns total and keys', () => {
18+
assert.deepEqual(transformReply([3, '1', '2', '3']), {
19+
total: 3,
20+
documents: ['1', '2', '3']
21+
})
22+
});
23+
});
24+
25+
describe('client.ft.searchNoContent', () => {
26+
testUtils.testWithClient('returns total and keys', async client => {
27+
await Promise.all([
28+
client.ft.create('index', {
29+
field: SchemaFieldTypes.TEXT
30+
}),
31+
client.hSet('1', 'field', 'field1'),
32+
client.hSet('2', 'field', 'field2'),
33+
client.hSet('3', 'field', 'field3')
34+
]);
35+
36+
assert.deepEqual(
37+
await client.ft.searchNoContent('index', '*'),
38+
{
39+
total: 3,
40+
documents: ['1','2','3']
41+
}
42+
);
43+
}, GLOBAL.SERVERS.OPEN);
44+
});
45+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { RedisCommandArguments } from "@redis/client/dist/lib/commands";
2+
import { pushSearchOptions } from ".";
3+
import { SearchOptions, SearchRawReply } from "./SEARCH";
4+
5+
export const FIRST_KEY_INDEX = 1;
6+
7+
export const IS_READ_ONLY = true;
8+
9+
export function transformArguments(
10+
index: string,
11+
query: string,
12+
options?: SearchOptions
13+
): RedisCommandArguments {
14+
return pushSearchOptions(
15+
['FT.SEARCH', index, query, 'NOCONTENT'],
16+
options
17+
);
18+
}
19+
20+
export interface SearchNoContentReply {
21+
total: number;
22+
documents: Array<string>;
23+
};
24+
25+
export function transformReply(reply: SearchRawReply): SearchNoContentReply {
26+
return {
27+
total: reply[0],
28+
documents: reply.slice(1)
29+
};
30+
}

packages/search/lib/commands/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as INFO from './INFO';
2020
import * as PROFILESEARCH from './PROFILE_SEARCH';
2121
import * as PROFILEAGGREGATE from './PROFILE_AGGREGATE';
2222
import * as SEARCH from './SEARCH';
23+
import * as SEARCH_NOCONTENT from './SEARCH_NOCONTENT';
2324
import * as SPELLCHECK from './SPELLCHECK';
2425
import * as SUGADD from './SUGADD';
2526
import * as SUGDEL from './SUGDEL';
@@ -80,6 +81,8 @@ export default {
8081
profileAggregate: PROFILEAGGREGATE,
8182
SEARCH,
8283
search: SEARCH,
84+
SEARCH_NOCONTENT,
85+
searchNoContent: SEARCH_NOCONTENT,
8386
SPELLCHECK,
8487
spellCheck: SPELLCHECK,
8588
SUGADD,

0 commit comments

Comments
 (0)