Skip to content

Commit beda6fa

Browse files
committed
Fixed multi error and added some tests
1 parent dbf0869 commit beda6fa

File tree

3 files changed

+98
-22
lines changed

3 files changed

+98
-22
lines changed

redisearch/client.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,6 @@ type IndexingOptions struct {
148148
Replace bool
149149
}
150150

151-
// MultiError Represents one or more errors
152-
type MultiError map[int]error
153-
154-
func (e MultiError) Error() string {
155-
for _, err := range e {
156-
return err.Error()
157-
}
158-
return ""
159-
}
160-
161151
// DefaultIndexingOptions are the default options for document indexing
162152
var DefaultIndexingOptions = IndexingOptions{
163153
Language: "",
@@ -166,13 +156,13 @@ var DefaultIndexingOptions = IndexingOptions{
166156
}
167157

168158
// Index indexes multiple documents on the index, with optional Options passed to options
169-
func (i *Client) IndexOptions(opts IndexingOptions, docs ...Document) (errors MultiError) {
159+
func (i *Client) IndexOptions(opts IndexingOptions, docs ...Document) error {
170160

171161
conn := i.pool.Get()
172162
defer conn.Close()
173163

174164
n := 0
175-
errors = make(map[int]error)
165+
var merr MultiError
176166

177167
for ii, doc := range docs {
178168
args := make(redis.Args, 0, 6+len(doc.Properties))
@@ -199,29 +189,35 @@ func (i *Client) IndexOptions(opts IndexingOptions, docs ...Document) (errors Mu
199189
}
200190

201191
if err := conn.Send("FT.ADD", args...); err != nil {
202-
errors[ii] = err
203-
return
192+
if merr == nil {
193+
merr = NewMultiError(len(docs))
194+
}
195+
merr[ii] = err
196+
197+
return merr
204198
}
205199
n++
206200
}
207201

208202
if err := conn.Flush(); err != nil {
209-
errors[-1] = err
210-
return
203+
return err
211204
}
212205

213206
for n > 0 {
214207
if _, err := conn.Receive(); err != nil {
215-
errors[n-1] = err
208+
if merr == nil {
209+
merr = NewMultiError(len(docs))
210+
}
211+
merr[n-1] = err
216212
}
217213
n--
218214
}
219215

220-
if len(errors) == 0 {
216+
if merr == nil {
221217
return nil
222218
}
223219

224-
return
220+
return merr
225221
}
226222

227223
// convert the result from a redis query to a proper Document object
@@ -260,7 +256,7 @@ func loadDocument(arr []interface{}, idIdx, scoreIdx, payloadIdx, fieldsIdx int)
260256
return doc, nil
261257
}
262258

263-
func (i *Client) Index(docs ...Document) map[int]error {
259+
func (i *Client) Index(docs ...Document) error {
264260
return i.IndexOptions(DefaultIndexingOptions, docs...)
265261
}
266262

redisearch/multi_error.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package redisearch
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// MultiError Represents one or more errors
8+
type MultiError []error
9+
10+
// NewMultiError initializes a multierror with the given len, and all sub-errors set to nil
11+
func NewMultiError(len int) MultiError {
12+
return make(MultiError, len)
13+
}
14+
15+
// Error returns a string representation of the error, in this case it just chains all the sub errors if they are not nil
16+
func (e MultiError) Error() string {
17+
18+
var ret string
19+
for i, err := range e {
20+
if err != nil {
21+
ret += fmt.Sprintf("[%d] %s\n", i, err.Error())
22+
}
23+
}
24+
return ret
25+
}

redisearch/redisearch_test.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/RedisLabs/redisearch-go/redisearch"
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
func createClient(indexName string) *redisearch.Client {
@@ -42,14 +43,68 @@ func TestClient(t *testing.T) {
4243
// Test it again
4344
if err := c.IndexOptions(redisearch.DefaultIndexingOptions, docs...); err == nil {
4445
t.Fatal("Expected error for duplicate document")
45-
} else if len(err) != 100 {
46-
t.Fatal("Not enough errors received")
46+
} else {
47+
if merr, ok := err.(redisearch.MultiError); !ok {
48+
t.Fatal("error not a multi error")
49+
} else {
50+
assert.Equal(t, 100, len(merr))
51+
assert.NotEmpty(t, merr)
52+
//fmt.Println("Got errors: ", merr)
53+
}
4754
}
4855

4956
docs, total, err := c.Search(redisearch.NewQuery("hello world"))
57+
assert.Nil(t, err)
58+
assert.Equal(t, 100, total)
59+
assert.Equal(t, 10, len(docs))
60+
5061
fmt.Println(docs, total, err)
5162
}
5263

64+
func TestNumeric(t *testing.T) {
65+
c := createClient("testung")
66+
67+
sc := redisearch.NewSchema(redisearch.DefaultOptions).
68+
AddField(redisearch.NewTextField("foo")).
69+
AddField(redisearch.NewSortableNumericField("bar"))
70+
c.Drop()
71+
assert.Nil(t, c.CreateIndex(sc))
72+
73+
docs := make([]redisearch.Document, 100)
74+
for i := 0; i < 100; i++ {
75+
docs[i] = redisearch.NewDocument(fmt.Sprintf("doc%d", i), 1).Set("foo", "hello world").Set("bar", i)
76+
}
77+
78+
assert.Nil(t, c.Index(docs...))
79+
80+
docs, total, err := c.Search(redisearch.NewQuery("hello world @bar:[50 100]").SetFlags(redisearch.QueryNoContent | redisearch.QueryWithScores))
81+
assert.Nil(t, err)
82+
assert.Equal(t, 10, len(docs))
83+
assert.Equal(t, 50, total)
84+
85+
docs, total, err = c.Search(redisearch.NewQuery("hello world @bar:[40 90]").SetSortBy("bar", false))
86+
assert.Nil(t, err)
87+
assert.Equal(t, 10, len(docs))
88+
assert.Equal(t, 51, total)
89+
assert.Equal(t, "doc90", docs[0].Id)
90+
assert.Equal(t, "doc89", docs[1].Id)
91+
assert.Equal(t, "doc81", docs[9].Id)
92+
93+
docs, total, err = c.Search(redisearch.NewQuery("hello world @bar:[40 90]").
94+
SetSortBy("bar", true).
95+
SetReturnFields("foo"))
96+
assert.Nil(t, err)
97+
assert.Equal(t, 10, len(docs))
98+
assert.Equal(t, 51, total)
99+
assert.Equal(t, "doc40", docs[0].Id)
100+
assert.Equal(t, "hello world", docs[0].Properties["foo"])
101+
assert.Nil(t, docs[0].Properties["bar"])
102+
assert.Equal(t, "doc41", docs[1].Id)
103+
assert.Equal(t, "doc49", docs[9].Id)
104+
fmt.Println(docs)
105+
106+
}
107+
53108
func ExampleClient() {
54109

55110
// Create a client. By default a client is schemaless

0 commit comments

Comments
 (0)