Skip to content

Commit b743fe0

Browse files
authored
Add FT.ALTER in OM JSON and Hash repo (#645)
* Add FT.ALTER in OM JSON and Hash repo Signed-off-by: Vatsal <[email protected]> * Add FT.ALTER in OM JSON and Hash repo Signed-off-by: Vatsal <[email protected]> * Add FT.ALTER in OM JSON and Hash repo Signed-off-by: Vatsal <[email protected]> * Add FT.ALTER in OM JSON and Hash repo Signed-off-by: Vatsal <[email protected]> * Add FT.ALTER in OM JSON and Hash repo Signed-off-by: Vatsal <[email protected]>
1 parent 25821d0 commit b743fe0

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

om/hash.go

+6
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func (r *HashRepository[T]) Remove(ctx context.Context, id string) error {
134134
return r.client.Do(ctx, r.client.B().Del().Key(key(r.prefix, id)).Build()).Error()
135135
}
136136

137+
// AlterIndex uses FT.ALTER from the RediSearch module to alter index under the name `hashidx:{prefix}`
138+
// You can use the cmdFn parameter to mutate the index alter command.
139+
func (r *HashRepository[T]) AlterIndex(ctx context.Context, cmdFn func(alter FtAlterIndex) rueidis.Completed) error {
140+
return r.client.Do(ctx, cmdFn(r.client.B().FtAlter().Index(r.idx))).Error()
141+
}
142+
137143
// CreateIndex uses FT.CREATE from the RediSearch module to create inverted index under the name `hashidx:{prefix}`
138144
// You can use the cmdFn parameter to mutate the index construction command.
139145
func (r *HashRepository[T]) CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error {

om/hash_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package om
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"math/rand"
78
"reflect"
89
"strings"
@@ -240,6 +241,63 @@ func TestNewHashRepository(t *testing.T) {
240241
t.Fatalf("should not be found, but got %v", e)
241242
}
242243
})
244+
245+
t.Run("Alter Index", func(t *testing.T) {
246+
err := repo.CreateIndex(ctx, func(schema FtCreateSchema) rueidis.Completed {
247+
return schema.FieldName("Val").Text().Build()
248+
})
249+
if err != nil {
250+
t.Fatal(err)
251+
}
252+
time.Sleep(time.Second)
253+
var entities []*HashTestStruct
254+
for i := 3; i >= 1; i-- {
255+
e := repo.NewEntity()
256+
e.Val = []byte("any")
257+
e.Vec32 = []float32{3, 2, 1}
258+
e.Vec64 = []float64{1, 2, 3}
259+
e.JSON = []byte(fmt.Sprintf("[%d]", i))
260+
err = repo.Save(ctx, e)
261+
if err != nil {
262+
t.Fatal(err)
263+
}
264+
entities = append(entities, e)
265+
}
266+
time.Sleep(time.Second)
267+
_, _, err = repo.Search(ctx, func(search FtSearchIndex) rueidis.Completed {
268+
return search.Query("*").Sortby("JSON").Build()
269+
})
270+
if err == nil {
271+
t.Fatalf("search by property not loaded nor in schema")
272+
}
273+
err = repo.AlterIndex(ctx, func(alter FtAlterIndex) rueidis.Completed {
274+
return alter.
275+
Schema().Add().Field("JSON").Options("TEXT").
276+
Build()
277+
})
278+
if err != nil {
279+
t.Fatal(err)
280+
}
281+
time.Sleep(time.Second)
282+
n, records, err := repo.Search(ctx, func(search FtSearchIndex) rueidis.Completed {
283+
return search.Query("*").Sortby("JSON").Build()
284+
})
285+
if err != nil {
286+
t.Fatal(err)
287+
}
288+
if n != 3 {
289+
t.Fatalf("unexpected total count %v", n)
290+
}
291+
if len(records) != 3 {
292+
t.Fatalf("unexpected return count %v", n)
293+
}
294+
if !reflect.DeepEqual(entities[2], records[0]) {
295+
t.Fatalf("entities[0] should be the same as records[2]")
296+
}
297+
if err = repo.DropIndex(ctx); err != nil {
298+
t.Fatal(err)
299+
}
300+
})
243301
})
244302

245303
t.Run("SaveMulti", func(t *testing.T) {

om/json.go

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ func (r *JSONRepository[T]) Remove(ctx context.Context, id string) error {
132132
return r.client.Do(ctx, r.client.B().Del().Key(key(r.prefix, id)).Build()).Error()
133133
}
134134

135+
// AlterIndex uses FT.ALTER from the RediSearch module to alter index under the name `jsonidx:{prefix}`
136+
// You can use the cmdFn parameter to mutate the index alter command.
137+
func (r *JSONRepository[T]) AlterIndex(ctx context.Context, cmdFn func(alter FtAlterIndex) rueidis.Completed) error {
138+
return r.client.Do(ctx, cmdFn(r.client.B().FtAlter().Index(r.idx))).Error()
139+
}
140+
135141
// CreateIndex uses FT.CREATE from the RediSearch module to create inverted index under the name `jsonidx:{prefix}`
136142
// You can use the cmdFn parameter to mutate the index construction command,
137143
// and note that the field name should be specified with JSON path syntax, otherwise the index may not work as expected.

om/json_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package om
22

33
import (
44
"context"
5+
"fmt"
56
"reflect"
67
"testing"
78
"time"
@@ -204,6 +205,65 @@ func TestNewJSONRepository(t *testing.T) {
204205
t.Fatalf("should not be found, but got %v", e)
205206
}
206207
})
208+
209+
t.Run("Alter Index", func(t *testing.T) {
210+
err := repo.CreateIndex(ctx, func(schema FtCreateSchema) rueidis.Completed {
211+
return schema.FieldName("$.Val").Text().Build()
212+
})
213+
if err != nil {
214+
t.Fatal(err)
215+
}
216+
time.Sleep(time.Second)
217+
var entities []*JSONTestStruct
218+
for i := 3; i >= 1; i-- {
219+
e := repo.NewEntity()
220+
e.Val = []byte("any")
221+
e.Nested = struct {
222+
F1 string
223+
}{
224+
F1: fmt.Sprintf("%d", i),
225+
}
226+
err = repo.Save(ctx, e)
227+
if err != nil {
228+
t.Fatal(err)
229+
}
230+
entities = append(entities, e)
231+
}
232+
time.Sleep(time.Second)
233+
n, records, err := repo.Search(ctx, func(search FtSearchIndex) rueidis.Completed {
234+
return search.Query("*").Sortby("$.Nested.F1").Build()
235+
})
236+
if err == nil {
237+
t.Fatalf("search by property not loaded nor in schema")
238+
}
239+
err = repo.AlterIndex(ctx, func(alter FtAlterIndex) rueidis.Completed {
240+
return alter.
241+
Schema().Add().Field("$.Nested.F1").Options("TEXT").
242+
Build()
243+
})
244+
if err != nil {
245+
t.Fatal(err)
246+
}
247+
time.Sleep(time.Second)
248+
n, records, err = repo.Search(ctx, func(search FtSearchIndex) rueidis.Completed {
249+
return search.Query("*").Sortby("$.Nested.F1").Build()
250+
})
251+
if err != nil {
252+
t.Fatal(err)
253+
}
254+
if n != 3 {
255+
t.Fatalf("unexpected total count %v", n)
256+
}
257+
if len(records) != 3 {
258+
t.Fatalf("unexpected return count %v", n)
259+
}
260+
if !reflect.DeepEqual(entities[2], records[0]) {
261+
t.Fatalf("entities[0] should be the same as records[2]")
262+
}
263+
if err = repo.DropIndex(ctx); err != nil {
264+
t.Fatal(err)
265+
}
266+
})
207267
})
208268

209269
t.Run("SaveMulti", func(t *testing.T) {

om/repo.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type (
1616
FtSearchIndex = cmds.FtSearchIndex
1717
// FtAggregateIndex is the FT.AGGREGATE command builder
1818
FtAggregateIndex = cmds.FtAggregateIndex
19+
// FtAlterSchema is the FT.ALTERINDEX command builder
20+
FtAlterIndex = cmds.FtAlterIndex
1921
// Arbitrary is alias to cmds.Arbitrary. This allows user build arbitrary command in Repository.CreateIndex
2022
Arbitrary = cmds.Arbitrary
2123
)
@@ -43,6 +45,7 @@ type Repository[T any] interface {
4345
SaveMulti(ctx context.Context, entity ...*T) (errs []error)
4446
Remove(ctx context.Context, id string) error
4547
CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error
48+
AlterIndex(ctx context.Context, cmdFn func(alter FtAlterIndex) rueidis.Completed) error
4649
DropIndex(ctx context.Context) error
4750
IndexName() string
4851
}

0 commit comments

Comments
 (0)