From e24bd5c0dd63cd213f4635c341be5f5c0ec7de2b Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 3 Aug 2021 13:14:27 +0300 Subject: [PATCH 1/8] some documentations --- redisearch/aggregate.go | 27 +++++++++++++++++++------ redisearch/autocomplete.go | 8 ++++---- redisearch/client.go | 41 +++++++++++++++++++------------------- redisearch/document.go | 8 ++++++-- redisearch/index.go | 1 + redisearch/query.go | 4 ++-- redisearch/spellcheck.go | 2 ++ 7 files changed, 56 insertions(+), 35 deletions(-) diff --git a/redisearch/aggregate.go b/redisearch/aggregate.go index 191be4e..9d085d4 100644 --- a/redisearch/aggregate.go +++ b/redisearch/aggregate.go @@ -7,7 +7,12 @@ import ( "reflect" ) -// Projection +// Projection - Apply a 1-to-1 transformation on one or more properties, +// and either store the result as a new property down the pipeline, or +// replace any property using this transformation. Expression is an expression +// that can be used to perform arithmetic operations on numeric properties, +// or functions that can be applied on properties depending on their types, +// or any combination thereof. type Projection struct { Expression string Alias string @@ -66,13 +71,16 @@ func (c Cursor) Serialize() redis.Args { return args } -// GroupBy +//GroupBy groups the results in the pipeline based on one or more properties. +//Each group should have at least one reducer, a function that handles the group +//entries, either counting them, or performing multiple aggregate operations. type GroupBy struct { Fields []string Reducers []Reducer Paging *Paging } +// NewGroupBy creates a new GroupBy object func NewGroupBy() *GroupBy { return &GroupBy{ Fields: make([]string, 0), @@ -81,6 +89,7 @@ func NewGroupBy() *GroupBy { } } +// AddFields to the group. Can be single string or list of strings. func (g *GroupBy) AddFields(fields interface{}) *GroupBy { switch fields.(type) { case string: @@ -93,11 +102,13 @@ func (g *GroupBy) AddFields(fields interface{}) *GroupBy { return g } +// Reduce adds reducer to the group's list func (g *GroupBy) Reduce(reducer Reducer) *GroupBy { g.Reducers = append(g.Reducers, reducer) return g } +// Limit adds Paging to the GroupBy object func (g *GroupBy) Limit(offset int, num int) *GroupBy { g.Paging = NewPaging(offset, num) return g @@ -140,6 +151,7 @@ func NewAggregateQuery() *AggregateQuery { } } +// SetQuery sets the query to the AggregateQuery func (a *AggregateQuery) SetQuery(query *Query) *AggregateQuery { a.Query = query return a @@ -150,11 +162,14 @@ func (a *AggregateQuery) SetWithSchema(value bool) *AggregateQuery { return a } +// SetVerbatim - If set, we do not try to use stemming for query expansion but search +// the query terms verbatim. func (a *AggregateQuery) SetVerbatim(value bool) *AggregateQuery { a.Verbatim = value return a } +// SetMax is used to optimized sorting, by sorting only for the n-largest elements func (a *AggregateQuery) SetMax(value int) *AggregateQuery { a.Max = value return a @@ -174,13 +189,13 @@ func (a *AggregateQuery) CursorHasResults() (res bool) { return } -//Adds a APPLY clause to the aggregate plan +//Apply a 1-to-1 transformation on some property func (a *AggregateQuery) Apply(expression Projection) *AggregateQuery { a.AggregatePlan = a.AggregatePlan.AddFlat(expression.Serialize()) return a } -//Sets the limit for the initial pool of results from the query. +//Limit the number of results to return just num results starting at index offset (zero-based). func (a *AggregateQuery) Limit(offset int, num int) *AggregateQuery { a.Paging = NewPaging(offset, num) return a @@ -219,7 +234,8 @@ func (a *AggregateQuery) SortBy(SortByProperties []SortingKey) *AggregateQuery { return a } -//Specify filters to filter the results using predicates relating to values in the result set. +//Filter the results using predicate expressions relating to values in each result. +//They are is applied post-query and relate to the current state of the pipeline. func (a *AggregateQuery) Filter(expression string) *AggregateQuery { a.AggregatePlan = a.AggregatePlan.Add("FILTER", expression) //a.Filters = append(a.Filters, expression) @@ -241,7 +257,6 @@ func (q AggregateQuery) Serialize() redis.Args { if q.Verbatim { args = args.Add("VERBATIM") } - // WITHCURSOR if q.WithCursor { args = args.AddFlat(q.Cursor.Serialize()) diff --git a/redisearch/autocomplete.go b/redisearch/autocomplete.go index 88f1e96..3df7063 100644 --- a/redisearch/autocomplete.go +++ b/redisearch/autocomplete.go @@ -68,7 +68,7 @@ func (a *Autocompleter) AddTerms(terms ...Suggestion) error { return nil } -// AddTerms pushes new term suggestions to the index +// DeleteTerms deletes term suggestions from the index func (a *Autocompleter) DeleteTerms(terms ...Suggestion) error { conn := a.pool.Get() defer conn.Close() @@ -94,7 +94,7 @@ func (a *Autocompleter) DeleteTerms(terms ...Suggestion) error { return nil } -// AddTerms pushes new term suggestions to the index +// Length gets the size of the suggestion func (a *Autocompleter) Length() (len int64, err error) { conn := a.pool.Get() defer conn.Close() @@ -128,8 +128,8 @@ func (a *Autocompleter) Suggest(prefix string, num int, fuzzy bool) (ret []Sugge // SuggestOpts gets completion suggestions from the Autocompleter dictionary to the given prefix. // SuggestOptions are passed allowing you specify if the returned values contain a payload, and scores. -// If SuggestOptions.Fuzzy is set, we also complete for prefixes that are in 1 Levenshtein distance from the -// given prefix +// If SuggestOptions.Fuzzy is set, we also complete for prefixes that are in 1 Levenshtein distance +// from the given prefix func (a *Autocompleter) SuggestOpts(prefix string, opts SuggestOptions) (ret []Suggestion, err error) { conn := a.pool.Get() defer conn.Close() diff --git a/redisearch/client.go b/redisearch/client.go index 8e3cd46..980d652 100644 --- a/redisearch/client.go +++ b/redisearch/client.go @@ -126,12 +126,10 @@ func (i *Client) Search(q *Query) (docs []Document, total int, err error) { payloadIdx = skip skip++ } - if q.Flags&QueryNoContent == 0 { fieldsIdx = skip skip++ } - if len(res) > skip { for i := 1; i < len(res); i += skip { @@ -145,7 +143,8 @@ func (i *Client) Search(q *Query) (docs []Document, total int, err error) { return } -// Adds an alias to an index. +// AliasAdd adds an alias to an index. +// Indexes can have more than one alias, though an alias cannot refer to another alias. func (i *Client) AliasAdd(name string) (err error) { conn := i.pool.Get() defer conn.Close() @@ -154,7 +153,7 @@ func (i *Client) AliasAdd(name string) (err error) { return } -// Deletes an alias to an index. +// AliasDel deletes an alias from index. func (i *Client) AliasDel(name string) (err error) { conn := i.pool.Get() defer conn.Close() @@ -163,7 +162,9 @@ func (i *Client) AliasDel(name string) (err error) { return } -// Deletes an alias to an index. +// AliasUpdate differs from the AliasAdd in that it will remove the alias association with +// a previous index, if any. AliasAdd will fail, on the other hand, if the alias is already +// associated with another index. func (i *Client) AliasUpdate(name string) (err error) { conn := i.pool.Get() defer conn.Close() @@ -172,7 +173,7 @@ func (i *Client) AliasUpdate(name string) (err error) { return } -// Adds terms to a dictionary. +// DictAdd adds terms to a dictionary. func (i *Client) DictAdd(dictionaryName string, terms []string) (newTerms int, err error) { conn := i.pool.Get() defer conn.Close() @@ -182,7 +183,7 @@ func (i *Client) DictAdd(dictionaryName string, terms []string) (newTerms int, e return } -// Deletes terms from a dictionary +// DictDel deletes terms from a dictionary func (i *Client) DictDel(dictionaryName string, terms []string) (deletedTerms int, err error) { conn := i.pool.Get() defer conn.Close() @@ -192,7 +193,7 @@ func (i *Client) DictDel(dictionaryName string, terms []string) (deletedTerms in return } -// Dumps all terms in the given dictionary. +// DictDump dumps all terms in the given dictionary. func (i *Client) DictDump(dictionaryName string) (terms []string, err error) { conn := i.pool.Get() defer conn.Close() @@ -336,14 +337,12 @@ func (i *Client) MultiGet(documentIds []string) (docs []*Document, err error) { } else { docs[i] = nil } - } - } return } -// Explain Return a textual string explaining the query +// Explain Return a textual string explaining the query (execution plan) func (i *Client) Explain(q *Query) (string, error) { conn := i.pool.Get() defer conn.Close() @@ -354,22 +353,21 @@ func (i *Client) Explain(q *Query) (string, error) { return redis.String(conn.Do("FT.EXPLAIN", args...)) } -// Deletes the index and all the keys associated with it. +// Drop deletes the index and all the keys associated with it. func (i *Client) Drop() error { conn := i.pool.Get() defer conn.Close() _, err := conn.Do("FT.DROP", i.name) return err - } // Deletes the secondary index and optionally the associated hashes // // Available since RediSearch 2.0. // -// By default, DropIndex() which is a wrapper for RediSearch FT.DROPINDEX does not delete the document hashes associated with the index. -// Setting the argument deleteDocuments to true deletes the hashes as well. +// By default, DropIndex() which is a wrapper for RediSearch FT.DROPINDEX does not delete the document +// hashes associated with the index. Setting the argument deleteDocuments to true deletes the hashes as well. func (i *Client) DropIndex(deleteDocuments bool) error { conn := i.pool.Get() defer conn.Close() @@ -389,7 +387,7 @@ func (i *Client) Delete(docId string, deleteDocument bool) (err error) { return i.delDoc(docId, deleteDocument) } -// Delete the document from the index and also delete the HASH key in which the document is stored +// DeleteDocument delete the document from the index and also delete the HASH key in which the document is stored func (i *Client) DeleteDocument(docId string) (err error) { return i.delDoc(docId, true) } @@ -406,6 +404,7 @@ func (i *Client) delDoc(docId string, deleteDocument bool) (err error) { return } +// Internal method to be used by Info() func (info *IndexInfo) setTarget(key string, value interface{}) error { v := reflect.ValueOf(info).Elem() for i := 0; i < v.NumField(); i++ { @@ -602,8 +601,8 @@ func (i *Client) GetTagVals(index string, filedName string) ([]string, error) { return redis.Strings(conn.Do("FT.TAGVALS", args...)) } -// Adds a synonym group. -// Deprecated: This function is not longer supported on RediSearch 2.0 and above, use SynUpdate instead +// SynAdd adds a synonym group. +// Deprecated: This function is not longer supported on RediSearch 2.0 and above, use SynUpdate instead func (i *Client) SynAdd(indexName string, terms []string) (int64, error) { conn := i.pool.Get() defer conn.Close() @@ -612,7 +611,7 @@ func (i *Client) SynAdd(indexName string, terms []string) (int64, error) { return redis.Int64(conn.Do("FT.SYNADD", args...)) } -// Updates a synonym group, with additional terms. +// SynUpdate updates a synonym group, with additional terms. func (i *Client) SynUpdate(indexName string, synonymGroupId int64, terms []string) (string, error) { conn := i.pool.Get() defer conn.Close() @@ -621,7 +620,7 @@ func (i *Client) SynUpdate(indexName string, synonymGroupId int64, terms []strin return redis.String(conn.Do("FT.SYNUPDATE", args...)) } -// Dumps the contents of a synonym group. +// SynDump dumps the contents of a synonym group. func (i *Client) SynDump(indexName string) (map[string][]int64, error) { conn := i.pool.Get() defer conn.Close() @@ -650,7 +649,7 @@ func (i *Client) SynDump(indexName string) (map[string][]int64, error) { } // Adds a document to the index from an existing HASH key in Redis. -// Deprecated: This function is not longer supported on RediSearch 2.0 and above, use HSET instead +// Deprecated: This function is not longer supported on RediSearch 2.0 and above, use HSET instead // See the example ExampleClient_CreateIndexWithIndexDefinition for a deeper understanding on how to move towards using hashes on your application func (i *Client) AddHash(docId string, score float32, language string, replace bool) (string, error) { conn := i.pool.Get() diff --git a/redisearch/document.go b/redisearch/document.go index 1801571..98c8ee4 100644 --- a/redisearch/document.go +++ b/redisearch/document.go @@ -86,7 +86,8 @@ func EscapeTextFileString(value string) string { return value } -// convert the result from a redis query to a proper Document object +// internal function +// loadDocument convert the result from a redis query to a proper Document object func loadDocument(arr []interface{}, idIdx, scoreIdx, payloadIdx, fieldsIdx int) (Document, error) { var score float64 = 1 @@ -111,7 +112,8 @@ func loadDocument(arr []interface{}, idIdx, scoreIdx, payloadIdx, fieldsIdx int) return doc, nil } -// SetPayload Sets the document payload +// internal function used by loadDocument() +// loadFields loads the fields of the document func (d *Document) loadFields(lst []interface{}) *Document { for i := 0; i < len(lst); i += 2 { var prop string @@ -137,7 +139,9 @@ func (d *Document) loadFields(lst []interface{}) *Document { // DocumentList is used to sort documents by descending score type DocumentList []Document +// Len returns the length of the DocumentList func (l DocumentList) Len() int { return len(l) } +// Swap two documents in the list by their index func (l DocumentList) Swap(i, j int) { l[i], l[j] = l[j], l[i] } func (l DocumentList) Less(i, j int) bool { return l[i].Score > l[j].Score } //reverse sorting diff --git a/redisearch/index.go b/redisearch/index.go index 9aab4d7..f6f6571 100644 --- a/redisearch/index.go +++ b/redisearch/index.go @@ -12,6 +12,7 @@ const ( JSON ) +// Parse IndexType enum to string func (it IndexType) String() string { return [...]string{"HASH", "JSON"}[it] } diff --git a/redisearch/query.go b/redisearch/query.go index d65a82e..48641b5 100644 --- a/redisearch/query.go +++ b/redisearch/query.go @@ -73,7 +73,7 @@ type SummaryOptions struct { Separator string // default "..." } -// Query is a single search query and all its parameters and predicates +// Query is a single search query containing all its parameters and predicates type Query struct { Raw string @@ -243,7 +243,7 @@ func appendNumArgs(num float64, exclude bool, args redis.Args) redis.Args { return append(args, num) } -// AddFilter adds a filter to the query +// AddFilter adds a filter to the query. func (q *Query) AddFilter(f Filter) *Query { if q.Filters == nil { q.Filters = []Filter{} diff --git a/redisearch/spellcheck.go b/redisearch/spellcheck.go index 560f3dd..203c0d3 100644 --- a/redisearch/spellcheck.go +++ b/redisearch/spellcheck.go @@ -93,7 +93,9 @@ func NewMisspelledTerm(term string) MisspelledTerm { } } +// Len returns the length of the MisspelledSuggestionList func (l MisspelledTerm) Len() int { return len(l.MisspelledSuggestionList) } +// Swap two suggestions in the list func (l MisspelledTerm) Swap(i, j int) { maxLen := len(l.MisspelledSuggestionList) if i < maxLen && j < maxLen { From e8c5bd47417d5347194605ba90c27ee01539e474 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Thu, 5 Aug 2021 12:37:49 +0300 Subject: [PATCH 2/8] support NOHL and SKIPINITIALSCAN tags on FT.CREATE --- redisearch/schema.go | 29 ++++++++++++++++++++++++++++- redisearch/schema_test.go | 8 ++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/redisearch/schema.go b/redisearch/schema.go index a6fb7a6..0da7e72 100644 --- a/redisearch/schema.go +++ b/redisearch/schema.go @@ -28,7 +28,7 @@ type Options struct { // This is an option that is applied and index level. NoFrequencies bool - // If set, , we avoid saving the term offsets for documents. + // If set, we avoid saving the term offsets for documents. // This saves memory but does not allow exact searches or highlighting. Implies NOHL // This is an option that is applied and index level. NoOffsetVectors bool @@ -49,6 +49,13 @@ type Options struct { // If set to true This option forces RediSearch to encode indexes as if there were more than 32 text fields, // which allows you to add additional fields (beyond 32). MaxTextFieldsFlag bool + + // If set to true, conserves storage space and memory by disabling highlighting support. + // Also implied by NoOffsetVectors + NoHighlights bool + + // If set to true, we do not scan and index. + SkipInitialScan bool } func NewOptions() *Options { @@ -89,6 +96,18 @@ func (options *Options) SetMaxTextFieldsFlag(flag bool) *Options { return options } +// SetNoHighlight conserves storage space and memory by disabling highlighting support. +func (options *Options) SetNoHighlight(flag bool) *Options { + options.NoHighlights = flag + return options +} + +// SetSkipInitialScan determines if scan the index on creation +func (options *Options) SetSkipInitialScan(flag bool) *Options { + options.SkipInitialScan = flag + return options +} + // DefaultOptions represents the default options var DefaultOptions = Options{ NoSave: false, @@ -99,6 +118,8 @@ var DefaultOptions = Options{ Temporary: false, TemporaryPeriod: 0, MaxTextFieldsFlag: false, + NoHighlights: false, + SkipInitialScan: false, } // Field Types @@ -282,12 +303,18 @@ func SerializeSchema(s *Schema, args redis.Args) (argsOut redis.Args, err error) if s.Options.Temporary { argsOut = append(argsOut, "TEMPORARY", s.Options.TemporaryPeriod) } + if s.Options.NoHighlights { + argsOut = append(argsOut, "NOHL") + } if s.Options.NoFieldFlags { argsOut = append(argsOut, "NOFIELDS") } if s.Options.NoFrequencies { argsOut = append(argsOut, "NOFREQS") } + if s.Options.SkipInitialScan { + argsOut = append(argsOut, "SKIPINITIALSCAN") + } if s.Options.Stopwords != nil { argsOut = argsOut.Add("STOPWORDS", len(s.Options.Stopwords)) diff --git a/redisearch/schema_test.go b/redisearch/schema_test.go index 0d70e81..f3e9757 100644 --- a/redisearch/schema_test.go +++ b/redisearch/schema_test.go @@ -22,6 +22,10 @@ func TestNewSchema(t *testing.T) { Options: Options{Stopwords: []string{"custom"}}}}, {"no-frequencies", args{Options{NoFrequencies: true}}, &Schema{Fields: []Field{}, Options: Options{NoFrequencies: true}}}, + {"no-highlights", args{Options{NoHighlights: true}}, &Schema{Fields: []Field{}, + Options: Options{NoHighlights: true}}}, + {"skip-initial-scan", args{Options{SkipInitialScan: true}}, &Schema{Fields: []Field{}, + Options: Options{SkipInitialScan: true}}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -50,6 +54,10 @@ func TestSerializeSchema(t *testing.T) { {"default-args-with-different-constructor", args{NewSchema(*NewOptions()), redis.Args{}}, redis.Args{"SCHEMA"}, false}, {"temporary", args{NewSchema(*NewOptions().SetTemporaryPeriod(60)), redis.Args{}}, redis.Args{"TEMPORARY", 60, "SCHEMA"}, false}, {"no-frequencies", args{NewSchema(Options{NoFrequencies: true}), redis.Args{}}, redis.Args{"NOFREQS", "SCHEMA"}, false}, + {"no-hithlights", args{NewSchema(Options{NoHighlights: true}), redis.Args{}}, redis.Args{"NOHL", "SCHEMA"}, false}, + {"no-hithlights-with-different-consturctor", args{NewSchema(*NewOptions().SetNoHighlight(true)), redis.Args{}}, redis.Args{"NOHL", "SCHEMA"}, false}, + {"skip-inital-scan", args{NewSchema(Options{SkipInitialScan: true}), redis.Args{}}, redis.Args{"SKIPINITIALSCAN", "SCHEMA"}, false}, + {"skipinitalscan-with-different-consturctor", args{NewSchema(*NewOptions().SetSkipInitialScan(true)), redis.Args{}}, redis.Args{"SKIPINITIALSCAN", "SCHEMA"}, false}, {"no-fields", args{NewSchema(Options{NoFieldFlags: true}), redis.Args{}}, redis.Args{"NOFIELDS", "SCHEMA"}, false}, {"custom-stopwords", args{NewSchema(Options{Stopwords: []string{"custom"}}), redis.Args{}}, redis.Args{"STOPWORDS", 1, "custom", "SCHEMA"}, false}, {"custom-stopwords-with-different-constructor", args{NewSchema(*NewOptions().SetStopWords([]string{"custom"})), redis.Args{}}, redis.Args{"STOPWORDS", 1, "custom", "SCHEMA"}, false}, From 9f756a092e50aea61e2c98630c33fdd84d90b1af Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Thu, 5 Aug 2021 12:45:32 +0300 Subject: [PATCH 3/8] fix --- redisearch/spellcheck.go | 1 + 1 file changed, 1 insertion(+) diff --git a/redisearch/spellcheck.go b/redisearch/spellcheck.go index 203c0d3..1000a9c 100644 --- a/redisearch/spellcheck.go +++ b/redisearch/spellcheck.go @@ -95,6 +95,7 @@ func NewMisspelledTerm(term string) MisspelledTerm { // Len returns the length of the MisspelledSuggestionList func (l MisspelledTerm) Len() int { return len(l.MisspelledSuggestionList) } + // Swap two suggestions in the list func (l MisspelledTerm) Swap(i, j int) { maxLen := len(l.MisspelledSuggestionList) From 989f1ed363056044635507f2339da3c119fab289 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Sun, 8 Aug 2021 10:52:16 +0300 Subject: [PATCH 4/8] add SKIP-INITIAL-SCAN test --- redisearch/schema_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/redisearch/schema_test.go b/redisearch/schema_test.go index f3e9757..07bf02e 100644 --- a/redisearch/schema_test.go +++ b/redisearch/schema_test.go @@ -120,3 +120,31 @@ func TestSchema_AddField(t *testing.T) { }) } } + +func TestSchema_SkipInitialScan(t *testing.T) { + c := createClient("skip-initial-scan-test") + flush(c) + + vanillaConnection := c.pool.Get() + _, err := vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25) + assert.Nil(t, err) + + q := NewQuery("@name:Jon") + schema1 := NewSchema(DefaultOptions).AddField(NewTextField("name")) + schema2 := NewSchema(Options{SkipInitialScan: true}).AddField(NewTextField("name")) + indexDefinition := NewIndexDefinition() + + c = createClient("skip-initial-scan-test-scan") + c.CreateIndexWithIndexDefinition(schema1, indexDefinition) + assert.Nil(t, err) + _, total, err := c.Search(q) + assert.Nil(t, err) + assert.Equal(t, 1, total) + + c = createClient("skip-initial-scan-test-skip-scan") + c.CreateIndexWithIndexDefinition(schema2, indexDefinition) + assert.Nil(t, err) + _, total, err = c.Search(q) + assert.Nil(t, err) + assert.Equal(t, 0, total) +} From e4d70ec03d07b8c1701b50b6b3f45a46ddd23f71 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Sun, 8 Aug 2021 11:10:31 +0300 Subject: [PATCH 5/8] add NO-HIGHLIGHT test --- redisearch/schema_test.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/redisearch/schema_test.go b/redisearch/schema_test.go index 07bf02e..80ce098 100644 --- a/redisearch/schema_test.go +++ b/redisearch/schema_test.go @@ -124,7 +124,7 @@ func TestSchema_AddField(t *testing.T) { func TestSchema_SkipInitialScan(t *testing.T) { c := createClient("skip-initial-scan-test") flush(c) - + vanillaConnection := c.pool.Get() _, err := vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25) assert.Nil(t, err) @@ -148,3 +148,23 @@ func TestSchema_SkipInitialScan(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 0, total) } + +func TestSchema_SummarizationDisabled(t *testing.T) { + doc := NewDocument("TestSchema-doc1", 1.0).Set("body", "foo bar") + + c := createClient("summarize-disable-no-term-offsets-test") + flush(c) + schema := NewSchema(Options{NoOffsetVectors: true}).AddField(NewTextField("body")) + c.CreateIndexWithIndexDefinition(schema, NewIndexDefinition()) + assert.Nil(t, c.IndexOptions(DefaultIndexingOptions, doc)) + _, _, err := c.Search(NewQuery("body").Summarize()) + assert.NotNil(t, err) + + c = createClient("summarize-disable-no-highlights-test") + flush(c) + schema = NewSchema(Options{NoHighlights: true}).AddField(NewTextField("body")) + c.CreateIndexWithIndexDefinition(schema, NewIndexDefinition()) + assert.Nil(t, c.IndexOptions(DefaultIndexingOptions, doc)) + _, _, err = c.Search(NewQuery("body").Summarize()) + assert.NotNil(t, err) +} From f84445120f3654ed50a1b3835fd3bfaf0449ec1b Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Tue, 10 Aug 2021 10:03:49 +0100 Subject: [PATCH 6/8] [fix] Running SKIPINITIALSCAN related tests on RediSearch >= v2.x --- redisearch/schema_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/redisearch/schema_test.go b/redisearch/schema_test.go index 80ce098..a09e54d 100644 --- a/redisearch/schema_test.go +++ b/redisearch/schema_test.go @@ -125,8 +125,16 @@ func TestSchema_SkipInitialScan(t *testing.T) { c := createClient("skip-initial-scan-test") flush(c) + // check RediSearch version + version, err := c.getRediSearchVersion() + assert.Nil(t, err) + // This feature is only available since RediSearch >= v2.0 + if version <= 10699 { + return + } + vanillaConnection := c.pool.Get() - _, err := vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25) + _, err = vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25) assert.Nil(t, err) q := NewQuery("@name:Jon") From 387843818b3f8695faa062cc21a199da39f972be Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Tue, 10 Aug 2021 10:09:20 +0100 Subject: [PATCH 7/8] [fix] Using CreateIndex on TestSchema_SummarizationDisabled for RediSearch v1.6 compatible testing --- redisearch/schema_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/redisearch/schema_test.go b/redisearch/schema_test.go index a09e54d..ceeb5d8 100644 --- a/redisearch/schema_test.go +++ b/redisearch/schema_test.go @@ -163,7 +163,8 @@ func TestSchema_SummarizationDisabled(t *testing.T) { c := createClient("summarize-disable-no-term-offsets-test") flush(c) schema := NewSchema(Options{NoOffsetVectors: true}).AddField(NewTextField("body")) - c.CreateIndexWithIndexDefinition(schema, NewIndexDefinition()) + + c.CreateIndex(schema) assert.Nil(t, c.IndexOptions(DefaultIndexingOptions, doc)) _, _, err := c.Search(NewQuery("body").Summarize()) assert.NotNil(t, err) @@ -171,7 +172,7 @@ func TestSchema_SummarizationDisabled(t *testing.T) { c = createClient("summarize-disable-no-highlights-test") flush(c) schema = NewSchema(Options{NoHighlights: true}).AddField(NewTextField("body")) - c.CreateIndexWithIndexDefinition(schema, NewIndexDefinition()) + c.CreateIndex(schema) assert.Nil(t, c.IndexOptions(DefaultIndexingOptions, doc)) _, _, err = c.Search(NewQuery("body").Summarize()) assert.NotNil(t, err) From 10371bc018ac2fe149b76e5c2ff96fdfd8f0d939 Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Tue, 10 Aug 2021 10:21:21 +0100 Subject: [PATCH 8/8] [fix] Fix flakiness of TestSchema_SkipInitialScan by waiting for all docs to be indexed before querying --- redisearch/schema_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/redisearch/schema_test.go b/redisearch/schema_test.go index ceeb5d8..07ec217 100644 --- a/redisearch/schema_test.go +++ b/redisearch/schema_test.go @@ -5,6 +5,7 @@ import ( "github.com/stretchr/testify/assert" "reflect" "testing" + "time" ) func TestNewSchema(t *testing.T) { @@ -145,6 +146,15 @@ func TestSchema_SkipInitialScan(t *testing.T) { c = createClient("skip-initial-scan-test-scan") c.CreateIndexWithIndexDefinition(schema1, indexDefinition) assert.Nil(t, err) + + // Wait for all documents to be indexed + info, err := c.Info() + assert.Nil(t, err) + for info.IsIndexing { + time.Sleep(time.Second) + info, _ = c.Info() + } + _, total, err := c.Search(q) assert.Nil(t, err) assert.Equal(t, 1, total)