-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
74 lines (58 loc) · 1.08 KB
/
client.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package es
import (
"context"
"encoding/json"
"fmt"
"github.com/olivere/elastic/v7"
"github.com/vcaesar/go-gse-elastic/nlp"
)
type Conf struct {
URL string `toml:"url" yaml:"url"`
Sniff *bool `toml:"sniff" yaml:"sniff"`
SetGzip *bool `toml:"gzip" yaml:"gzip"`
}
type Client struct {
Client *elastic.Client
Conf *Conf
seg nlp.Segmenter
ctx context.Context
}
func (c *Conf) Init() error {
s := true
if c.Sniff == nil {
c.Sniff = &s
}
if c.SetGzip == nil {
c.SetGzip = &s
}
return nil
}
func (c *Conf) New(ctx1 context.Context) (*Client, error) {
err := c.Init()
if err != nil {
return nil, err
}
client, err := elastic.NewClient(
elastic.SetURL(c.URL),
elastic.SetSniff(*c.Sniff),
elastic.SetGzip(*c.SetGzip),
)
if err != nil {
return nil, err
}
return &Client{
Client: client,
Conf: c,
ctx: ctx1,
}, nil
}
func Source(q elastic.Query) (string, error) {
src, err := q.Source()
data, _ := json.Marshal(src)
return string(data), err
}
func Println(q elastic.Query) {
fmt.Println()
s, err := Source(q)
fmt.Println(s, err)
}