1
|
|
|
package solr |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"context" |
6
|
|
|
"fmt" |
7
|
|
|
"io" |
8
|
|
|
"log" |
9
|
|
|
"mime/multipart" |
10
|
|
|
"net/http" |
11
|
|
|
"os" |
12
|
|
|
"path/filepath" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
type Parameters struct { |
16
|
|
|
CommitWithin int `url:"commitWithin,omitempty"` |
17
|
|
|
Commit bool `url:"commit,omitempty"` |
18
|
|
|
Version bool `url:"version,omitempty"` |
19
|
|
|
Query string `url:"q,omitempty"` |
20
|
|
|
Delete interface{} `url:"delete,omitempty"` |
21
|
|
|
LiteralId string `url:"literal.id,omitempty"` |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
type Delete struct { |
25
|
|
|
Id string `json:"id,omitempty"` |
26
|
|
|
Query string `json:"query,omitempty"` |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
type Document map[string]interface{} |
30
|
|
|
|
31
|
|
|
type DocumentAPI struct { |
32
|
|
|
client *Client |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// SELECT: Select documents |
36
|
|
|
func (d *DocumentAPI) Select(ctx context.Context, collection string, query string) (*Response, error) { |
37
|
|
|
path := fmt.Sprintf("/solr/%s/select", collection) |
38
|
|
|
|
39
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodGet, path, nil, &Parameters{ |
40
|
|
|
Query: query, |
41
|
|
|
}, nil) |
42
|
|
|
if err != nil { |
43
|
|
|
return nil, err |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
response, err := d.client.Do(ctx, req) |
47
|
|
|
if err != nil { |
48
|
|
|
return nil, err |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return response, err |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// UPDATE: Update/Insert document |
55
|
|
|
func (d *DocumentAPI) Update(ctx context.Context, collection string, doc Document, params *Parameters) (*Response, error) { |
56
|
|
|
|
57
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/json", collection) |
58
|
|
|
|
59
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, doc, params, nil) |
60
|
|
|
if err != nil { |
61
|
|
|
return nil, err |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
response, err := d.client.Do(ctx, req) |
65
|
|
|
if err != nil { |
66
|
|
|
return nil, err |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return response, err |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// UPDATE MANY: Update/Insert documents |
73
|
|
|
func (d *DocumentAPI) UpdateMany(ctx context.Context, collection string, docs []Document, params *Parameters) (*Response, error) { |
74
|
|
|
|
75
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/json", collection) |
76
|
|
|
|
77
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, docs, params, nil) |
78
|
|
|
if err != nil { |
79
|
|
|
return nil, err |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
response, err := d.client.Do(ctx, req) |
83
|
|
|
if err != nil { |
84
|
|
|
return nil, err |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return response, err |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// ATOMIC UPDATE: Atomic Update/Insert document |
91
|
|
|
func (d *DocumentAPI) AtomicUpdate(ctx context.Context, collection string, doc Document, params *Parameters) (*Response, error) { |
92
|
|
|
|
93
|
|
|
path := fmt.Sprintf("/solr/%s/update", collection) |
94
|
|
|
|
95
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, doc, params, nil) |
96
|
|
|
if err != nil { |
97
|
|
|
return nil, err |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
response, err := d.client.Do(ctx, req) |
101
|
|
|
if err != nil { |
102
|
|
|
return nil, err |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return response, err |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// ATOMIC UPDATE MANY: Atomic Update/Insert documents |
109
|
|
|
func (d *DocumentAPI) AtomicUpdateMany(ctx context.Context, collection string, docs []Document, params *Parameters) (*Response, error) { |
110
|
|
|
|
111
|
|
|
path := fmt.Sprintf("/solr/%s/update", collection) |
112
|
|
|
|
113
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, docs, params, nil) |
114
|
|
|
if err != nil { |
115
|
|
|
return nil, err |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
response, err := d.client.Do(ctx, req) |
119
|
|
|
if err != nil { |
120
|
|
|
return nil, err |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return response, err |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// COMMIT: Commit documents |
127
|
|
|
func (d *DocumentAPI) Commit(ctx context.Context, collection string) (*Response, error) { |
128
|
|
|
|
129
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/json", collection) |
130
|
|
|
|
131
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, nil, Parameters{ |
132
|
|
|
Commit: true, |
133
|
|
|
}, nil) |
134
|
|
|
if err != nil { |
135
|
|
|
return nil, err |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
response, err := d.client.Do(ctx, req) |
139
|
|
|
if err != nil { |
140
|
|
|
return nil, err |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return response, err |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// DELETE: Delete document |
147
|
|
|
func (d *DocumentAPI) Delete(ctx context.Context, collection string, delete Delete, params *Parameters) (*Response, error) { |
148
|
|
|
|
149
|
|
|
path := fmt.Sprintf("/solr/%s/update", collection) |
150
|
|
|
|
151
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, map[string]interface{}{ |
152
|
|
|
"delete": delete, |
153
|
|
|
}, params, nil) |
154
|
|
|
if err != nil { |
155
|
|
|
return nil, err |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
response, err := d.client.Do(ctx, req) |
159
|
|
|
if err != nil { |
160
|
|
|
return nil, err |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return response, err |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// EXTRACT: Uploading Data with Solr Cell using Apache Tika |
167
|
|
|
func (d *DocumentAPI) Extract(ctx context.Context, collection string, filename string, params *Parameters) (*Response, error) { |
168
|
|
|
file, err := os.Open(filename) |
169
|
|
|
if err != nil { |
170
|
|
|
log.Fatal(err) |
171
|
|
|
} |
172
|
|
|
defer file.Close() |
173
|
|
|
|
174
|
|
|
body := &bytes.Buffer{} |
175
|
|
|
writer := multipart.NewWriter(body) |
176
|
|
|
part, err := writer.CreateFormFile("document", filepath.Base(file.Name())) |
177
|
|
|
if err != nil { |
178
|
|
|
log.Fatal(err) |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
_, err = io.Copy(part, file) |
182
|
|
|
if err != nil { |
183
|
|
|
return nil, err |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
err = writer.Close() |
187
|
|
|
if err != nil { |
188
|
|
|
return nil, err |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/extract", collection) |
192
|
|
|
|
193
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, body, params, &map[string]string{ |
194
|
|
|
"Content-Type": "application/octet-stream", |
195
|
|
|
}) |
196
|
|
|
if err != nil { |
197
|
|
|
return nil, err |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
response, err := d.client.Do(ctx, req) |
201
|
|
|
if err != nil { |
202
|
|
|
return nil, err |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return response, err |
206
|
|
|
} |
207
|
|
|
|