-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
135 lines (116 loc) · 3.99 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package solr
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
)
// SingleClient implements the solr interface and is the basic connection
// to a solr server.
type SingleClient struct {
conn connection
BasePath string
}
// NewSingleClient returns a connection to the solr client provided by the given
// host and core.
func NewSingleClient(conn connection) (Client, error) {
bp := conn.formatBasePath()
return &SingleClient{conn: conn, BasePath: bp}, nil
}
// SetBasicAuth sets auth credentials if needed.
func (c *SingleClient) SetBasicAuth(username, password string) {
c.conn.setBasicAuth(username, password)
}
func (c *SingleClient) formatURL(path string, query string) string {
if query != "" {
return c.BasePath + path + "?" + query
}
return c.BasePath + path
}
// Ping ...
func (c *SingleClient) Ping(ctx context.Context) error {
url := c.formatURL("/admin/ping", "")
res, err := c.conn.request(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
if res.Status != nil && *res.Status != "OK" {
return fmt.Errorf("error pinging solr, status: %s", *res.Status)
}
return nil
}
// Search ...
func (c *SingleClient) Search(ctx context.Context, q *Query) (*Response, error) {
url := c.formatURL("/select", q.String())
return read(ctx, c.conn, url)
}
// Get ...
func (c *SingleClient) Get(ctx context.Context, id, filter string) (*Response, error) {
vals := make(url.Values)
vals.Set("id", id)
if filter != "" {
vals.Set("fq", filter)
}
url := c.formatURL("/get", vals.Encode())
return read(ctx, c.conn, url)
}
// BatchGet ...
func (c *SingleClient) BatchGet(ctx context.Context, ids []string, filter string) (*Response, error) {
vals := make(url.Values)
vals.Set("ids", strings.Join(ids, ","))
if filter != "" {
vals.Set("fq", filter)
}
url := c.formatURL("/get", vals.Encode())
return read(ctx, c.conn, url)
}
// Create ...
func (c *SingleClient) Create(ctx context.Context, item interface{}, opts *WriteOptions) (*Response, error) {
url := c.formatURL("/update/json/docs", opts.formatQueryFromOpts().Encode())
return create(ctx, c.conn, url, item)
}
// BatchCreate ...
func (c *SingleClient) BatchCreate(ctx context.Context, items interface{}, opts *WriteOptions) (*Response, error) {
url := c.formatURL("/update", opts.formatQueryFromOpts().Encode())
return batchCreate(ctx, c.conn, url, items)
}
// Update ...
func (c *SingleClient) Update(ctx context.Context, item *UpdatedFields, opts *WriteOptions) (*Response, error) {
url := c.formatURL("/update", opts.formatQueryFromOpts().Encode())
return update(ctx, c.conn, url, item)
}
// Commit ...
func (c *SingleClient) Commit(ctx context.Context, opts *CommitOptions) (*Response, error) {
url := c.BasePath + "/update"
return commit(ctx, c.conn, url, opts)
}
// Rollback ...
func (c *SingleClient) Rollback(ctx context.Context) (*Response, error) {
url := c.BasePath + "/update?commit=true"
return rollback(ctx, c.conn, url)
}
// Optimize ...
func (c *SingleClient) Optimize(ctx context.Context, opts *OptimizeOptions) (*Response, error) {
url := c.formatURL("/update", "")
return optimize(ctx, c.conn, url, opts)
}
// DeleteByID ...
func (c *SingleClient) DeleteByID(ctx context.Context, id string, opts *WriteOptions) (*Response, error) {
url := c.formatURL("/update", opts.formatQueryFromOpts().Encode())
return delete(ctx, c.conn, url, formatDeleteByID(id))
}
// DeleteByQuery ...
func (c *SingleClient) DeleteByQuery(ctx context.Context, query string, opts *WriteOptions) (*Response, error) {
url := c.formatURL("/update", opts.formatQueryFromOpts().Encode())
return delete(ctx, c.conn, url, formatDeleteByQuery(query))
}
// Clear ...
func (c *SingleClient) Clear(ctx context.Context) (*Response, error) {
return c.DeleteByQuery(ctx, "*:*", &WriteOptions{Commit: true})
}
// CustomUpdate ...
func (c *SingleClient) CustomUpdate(ctx context.Context, item *UpdateBuilder, opts *WriteOptions) (*Response, error) {
url := c.formatURL("/update", opts.formatQueryFromOpts().Encode())
return customUpdate(ctx, c.conn, url, item)
}