Skip to content

Commit 3faad10

Browse files
authored
feat(search): support indexing same field multiple times with different configurations (redis#3157)
Allow RediSearchSchema to accept an array of field definitions for a single field, enabling the same field to be indexed with different types and aliases. Example: ```typescript { sku: [ { type: SCHEMA_FIELD_TYPE.TEXT, AS: 'sku_text' }, { type: SCHEMA_FIELD_TYPE.TAG, AS: 'sku_tag', SORTABLE: true } ] } ``` - Add SchemaFieldDefinition type for reusable field type union - Update RediSearchSchema to accept SchemaFieldDefinition | SchemaFieldDefinition[] - Update parseSchema to normalize input and iterate over field definition arrays --- - fix typo in PT SCHEMA_TEXT_FIELD_PHONETIC - Add JSDoc and README documentation for the new array syntax that allows indexing the same field multiple times with different types or aliases.
1 parent 591e7f7 commit 3faad10

File tree

3 files changed

+195
-126
lines changed

3 files changed

+195
-126
lines changed

packages/search/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ await client.ft.create('idx:animals', {
3232

3333
See the [`FT.CREATE` documentation](https://redis.io/commands/ft.create/#description) for information about the different field types and additional options.
3434

35+
#### Indexing a Field Multiple Times
36+
37+
You can index the same field multiple times with different types or aliases by using an array:
38+
39+
```javascript
40+
await client.ft.create('idx:products', {
41+
sku: [
42+
{ type: SCHEMA_FIELD_TYPE.TEXT, AS: 'sku_text' },
43+
{ type: SCHEMA_FIELD_TYPE.TAG, AS: 'sku_tag', SORTABLE: true }
44+
]
45+
}, {
46+
ON: 'HASH',
47+
PREFIX: 'product:'
48+
});
49+
```
50+
51+
This allows querying the same field using different search strategies.
52+
3553
#### Querying the Index
3654

3755
Once we've created an index, and added some data to Redis hashes whose keys begin with the prefix `noderedis:animals`, we can start writing some search queries. RediSearch supports a rich query syntax for full-text search, faceted search, aggregation and more. Check out the [`FT.SEARCH` documentation](https://redis.io/commands/ft.search) and the [query syntax reference](https://redis.io/docs/interact/search-and-query/query) for more information.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,28 @@ describe('FT.CREATE', () => {
332332
['FT.CREATE', 'index', 'SCHEMA', 'field', 'TEXT', 'INDEXMISSING']
333333
);
334334
});
335+
336+
it('same field indexed twice with different aliases and types', () => {
337+
assert.deepEqual(
338+
parseArgs(CREATE, 'idx', {
339+
sku: [
340+
{
341+
type: SCHEMA_FIELD_TYPE.TEXT,
342+
AS: 'sku_text'
343+
},
344+
{
345+
type: SCHEMA_FIELD_TYPE.TAG,
346+
AS: 'sku_tag',
347+
SORTABLE: true
348+
}
349+
]
350+
}, {
351+
ON: 'HASH',
352+
PREFIX: 'blog:post:'
353+
}),
354+
['FT.CREATE', 'idx', 'ON', 'HASH', 'PREFIX', '1', 'blog:post:', 'SCHEMA', 'sku', 'AS', 'sku_text', 'TEXT', 'sku', 'AS', 'sku_tag', 'TAG', 'SORTABLE']
355+
);
356+
});
335357
});
336358

337359
it('with ON', () => {

0 commit comments

Comments
 (0)