-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
51 lines (42 loc) · 1.16 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package gonic
import (
"context"
"github.com/Haato3o/gonic/internal/driver"
"github.com/Haato3o/gonic/internal/search"
)
type SearchOptions struct {
Limit *int
Offset *int
Locale *Lang
}
type SuggestOptions struct {
Limit *int
}
type ListOptions struct {
Limit *int
Offset *int
}
type Searchable interface {
QueryWithContext(ctx context.Context, collection, bucket, query string, opts ...func(options *SearchOptions)) ([]string, error)
SuggestWithContext(ctx context.Context, collection, bucket, word string, opts ...func(options *SuggestOptions)) ([]string, error)
ListWithContext(ctx context.Context, collection, bucket string, opts ...func(options *ListOptions)) ([]string, error)
PingWithContext(ctx context.Context) error
}
func NewSearchableWithContext(ctx context.Context, options ...func(options *Options)) (Searchable, error) {
opts := &Options{
Port: 1491,
}
for _, fun := range options {
fun(opts)
}
dvr, err := driver.New(opts.Address, opts.Port, driver.ModeSearch)
if err != nil {
return nil, err
}
if err = dvr.ConnectWithContext(ctx, opts.Password); err != nil {
return nil, err
}
return &search.Engine{
Driver: dvr,
}, nil
}