Skip to content

Commit 90b2295

Browse files
support NOHL and SKIPINITIALSCAN tags on FT.CREATE (#125)
* support NOHL and SKIPINITIALSCAN tags on FT.CREATE * add SKIP-INITIAL-SCAN test * add NO-HIGHLIGHT test * [fix] Running SKIPINITIALSCAN related tests on RediSearch >= v2.x * [fix] Using CreateIndex on TestSchema_SummarizationDisabled for RediSearch v1.6 compatible testing * [fix] Fix flakiness of TestSchema_SkipInitialScan by waiting for all docs to be indexed before querying Co-authored-by: filipecosta90 <[email protected]>
1 parent bd1fa60 commit 90b2295

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

redisearch/schema.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Options struct {
2828
// This is an option that is applied and index level.
2929
NoFrequencies bool
3030

31-
// If set, , we avoid saving the term offsets for documents.
31+
// If set, we avoid saving the term offsets for documents.
3232
// This saves memory but does not allow exact searches or highlighting. Implies NOHL
3333
// This is an option that is applied and index level.
3434
NoOffsetVectors bool
@@ -49,6 +49,13 @@ type Options struct {
4949
// If set to true This option forces RediSearch to encode indexes as if there were more than 32 text fields,
5050
// which allows you to add additional fields (beyond 32).
5151
MaxTextFieldsFlag bool
52+
53+
// If set to true, conserves storage space and memory by disabling highlighting support.
54+
// Also implied by NoOffsetVectors
55+
NoHighlights bool
56+
57+
// If set to true, we do not scan and index.
58+
SkipInitialScan bool
5259
}
5360

5461
func NewOptions() *Options {
@@ -89,6 +96,18 @@ func (options *Options) SetMaxTextFieldsFlag(flag bool) *Options {
8996
return options
9097
}
9198

99+
// SetNoHighlight conserves storage space and memory by disabling highlighting support.
100+
func (options *Options) SetNoHighlight(flag bool) *Options {
101+
options.NoHighlights = flag
102+
return options
103+
}
104+
105+
// SetSkipInitialScan determines if scan the index on creation
106+
func (options *Options) SetSkipInitialScan(flag bool) *Options {
107+
options.SkipInitialScan = flag
108+
return options
109+
}
110+
92111
// DefaultOptions represents the default options
93112
var DefaultOptions = Options{
94113
NoSave: false,
@@ -99,6 +118,8 @@ var DefaultOptions = Options{
99118
Temporary: false,
100119
TemporaryPeriod: 0,
101120
MaxTextFieldsFlag: false,
121+
NoHighlights: false,
122+
SkipInitialScan: false,
102123
}
103124

104125
// Field Types
@@ -282,12 +303,18 @@ func SerializeSchema(s *Schema, args redis.Args) (argsOut redis.Args, err error)
282303
if s.Options.Temporary {
283304
argsOut = append(argsOut, "TEMPORARY", s.Options.TemporaryPeriod)
284305
}
306+
if s.Options.NoHighlights {
307+
argsOut = append(argsOut, "NOHL")
308+
}
285309
if s.Options.NoFieldFlags {
286310
argsOut = append(argsOut, "NOFIELDS")
287311
}
288312
if s.Options.NoFrequencies {
289313
argsOut = append(argsOut, "NOFREQS")
290314
}
315+
if s.Options.SkipInitialScan {
316+
argsOut = append(argsOut, "SKIPINITIALSCAN")
317+
}
291318

292319
if s.Options.Stopwords != nil {
293320
argsOut = argsOut.Add("STOPWORDS", len(s.Options.Stopwords))

redisearch/schema_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/stretchr/testify/assert"
66
"reflect"
77
"testing"
8+
"time"
89
)
910

1011
func TestNewSchema(t *testing.T) {
@@ -22,6 +23,10 @@ func TestNewSchema(t *testing.T) {
2223
Options: Options{Stopwords: []string{"custom"}}}},
2324
{"no-frequencies", args{Options{NoFrequencies: true}}, &Schema{Fields: []Field{},
2425
Options: Options{NoFrequencies: true}}},
26+
{"no-highlights", args{Options{NoHighlights: true}}, &Schema{Fields: []Field{},
27+
Options: Options{NoHighlights: true}}},
28+
{"skip-initial-scan", args{Options{SkipInitialScan: true}}, &Schema{Fields: []Field{},
29+
Options: Options{SkipInitialScan: true}}},
2530
}
2631
for _, tt := range tests {
2732
t.Run(tt.name, func(t *testing.T) {
@@ -50,6 +55,10 @@ func TestSerializeSchema(t *testing.T) {
5055
{"default-args-with-different-constructor", args{NewSchema(*NewOptions()), redis.Args{}}, redis.Args{"SCHEMA"}, false},
5156
{"temporary", args{NewSchema(*NewOptions().SetTemporaryPeriod(60)), redis.Args{}}, redis.Args{"TEMPORARY", 60, "SCHEMA"}, false},
5257
{"no-frequencies", args{NewSchema(Options{NoFrequencies: true}), redis.Args{}}, redis.Args{"NOFREQS", "SCHEMA"}, false},
58+
{"no-hithlights", args{NewSchema(Options{NoHighlights: true}), redis.Args{}}, redis.Args{"NOHL", "SCHEMA"}, false},
59+
{"no-hithlights-with-different-consturctor", args{NewSchema(*NewOptions().SetNoHighlight(true)), redis.Args{}}, redis.Args{"NOHL", "SCHEMA"}, false},
60+
{"skip-inital-scan", args{NewSchema(Options{SkipInitialScan: true}), redis.Args{}}, redis.Args{"SKIPINITIALSCAN", "SCHEMA"}, false},
61+
{"skipinitalscan-with-different-consturctor", args{NewSchema(*NewOptions().SetSkipInitialScan(true)), redis.Args{}}, redis.Args{"SKIPINITIALSCAN", "SCHEMA"}, false},
5362
{"no-fields", args{NewSchema(Options{NoFieldFlags: true}), redis.Args{}}, redis.Args{"NOFIELDS", "SCHEMA"}, false},
5463
{"custom-stopwords", args{NewSchema(Options{Stopwords: []string{"custom"}}), redis.Args{}}, redis.Args{"STOPWORDS", 1, "custom", "SCHEMA"}, false},
5564
{"custom-stopwords-with-different-constructor", args{NewSchema(*NewOptions().SetStopWords([]string{"custom"})), redis.Args{}}, redis.Args{"STOPWORDS", 1, "custom", "SCHEMA"}, false},
@@ -112,3 +121,69 @@ func TestSchema_AddField(t *testing.T) {
112121
})
113122
}
114123
}
124+
125+
func TestSchema_SkipInitialScan(t *testing.T) {
126+
c := createClient("skip-initial-scan-test")
127+
flush(c)
128+
129+
// check RediSearch version
130+
version, err := c.getRediSearchVersion()
131+
assert.Nil(t, err)
132+
// This feature is only available since RediSearch >= v2.0
133+
if version <= 10699 {
134+
return
135+
}
136+
137+
vanillaConnection := c.pool.Get()
138+
_, err = vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25)
139+
assert.Nil(t, err)
140+
141+
q := NewQuery("@name:Jon")
142+
schema1 := NewSchema(DefaultOptions).AddField(NewTextField("name"))
143+
schema2 := NewSchema(Options{SkipInitialScan: true}).AddField(NewTextField("name"))
144+
indexDefinition := NewIndexDefinition()
145+
146+
c = createClient("skip-initial-scan-test-scan")
147+
c.CreateIndexWithIndexDefinition(schema1, indexDefinition)
148+
assert.Nil(t, err)
149+
150+
// Wait for all documents to be indexed
151+
info, err := c.Info()
152+
assert.Nil(t, err)
153+
for info.IsIndexing {
154+
time.Sleep(time.Second)
155+
info, _ = c.Info()
156+
}
157+
158+
_, total, err := c.Search(q)
159+
assert.Nil(t, err)
160+
assert.Equal(t, 1, total)
161+
162+
c = createClient("skip-initial-scan-test-skip-scan")
163+
c.CreateIndexWithIndexDefinition(schema2, indexDefinition)
164+
assert.Nil(t, err)
165+
_, total, err = c.Search(q)
166+
assert.Nil(t, err)
167+
assert.Equal(t, 0, total)
168+
}
169+
170+
func TestSchema_SummarizationDisabled(t *testing.T) {
171+
doc := NewDocument("TestSchema-doc1", 1.0).Set("body", "foo bar")
172+
173+
c := createClient("summarize-disable-no-term-offsets-test")
174+
flush(c)
175+
schema := NewSchema(Options{NoOffsetVectors: true}).AddField(NewTextField("body"))
176+
177+
c.CreateIndex(schema)
178+
assert.Nil(t, c.IndexOptions(DefaultIndexingOptions, doc))
179+
_, _, err := c.Search(NewQuery("body").Summarize())
180+
assert.NotNil(t, err)
181+
182+
c = createClient("summarize-disable-no-highlights-test")
183+
flush(c)
184+
schema = NewSchema(Options{NoHighlights: true}).AddField(NewTextField("body"))
185+
c.CreateIndex(schema)
186+
assert.Nil(t, c.IndexOptions(DefaultIndexingOptions, doc))
187+
_, _, err = c.Search(NewQuery("body").Summarize())
188+
assert.NotNil(t, err)
189+
}

0 commit comments

Comments
 (0)