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
|
|
|
Query string `url:"q,omitempty"` |
19
|
|
|
Delete interface{} `url:"delete,omitempty"` |
20
|
|
|
LiteralId string `url:"literal.id,omitempty"` |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
type Delete struct { |
24
|
|
|
Id string `json:"id,omitempty"` |
25
|
|
|
Query string `json:"query,omitempty"` |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
type Document map[string]interface{} |
29
|
|
|
|
30
|
|
|
type DocumentAPI struct { |
31
|
|
|
client *Client |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func (d *DocumentAPI) Select(ctx context.Context, collection string, query string) (*Response, error) { |
35
|
|
|
path := fmt.Sprintf("/solr/%s/select", collection) |
36
|
|
|
|
37
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodGet, path, nil, &Parameters{ |
38
|
|
|
Query: query, |
39
|
|
|
}, nil) |
40
|
|
|
if err != nil { |
41
|
|
|
return nil, err |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
response, err := d.client.Do(ctx, req) |
45
|
|
|
if err != nil { |
46
|
|
|
return nil, err |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return response, err |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
func (d *DocumentAPI) Update(ctx context.Context, collection string, doc Document, params *Parameters) (*Response, error) { |
53
|
|
|
|
54
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/json", collection) |
55
|
|
|
|
56
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, doc, params, nil) |
57
|
|
|
if err != nil { |
58
|
|
|
return nil, err |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
response, err := d.client.Do(ctx, req) |
62
|
|
|
if err != nil { |
63
|
|
|
return nil, err |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return response, err |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
func (d *DocumentAPI) UpdateMany(ctx context.Context, collection string, docs []Document, params *Parameters) (*Response, error) { |
70
|
|
|
|
71
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/json", collection) |
72
|
|
|
|
73
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, docs, params, nil) |
74
|
|
|
if err != nil { |
75
|
|
|
return nil, err |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
response, err := d.client.Do(ctx, req) |
79
|
|
|
if err != nil { |
80
|
|
|
return nil, err |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return response, err |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
func (d *DocumentAPI) Commit(ctx context.Context, collection string) (*Response, error) { |
87
|
|
|
|
88
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/json", collection) |
89
|
|
|
|
90
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, nil, Parameters{ |
91
|
|
|
Commit: true, |
92
|
|
|
}, nil) |
93
|
|
|
if err != nil { |
94
|
|
|
return nil, err |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
response, err := d.client.Do(ctx, req) |
98
|
|
|
if err != nil { |
99
|
|
|
return nil, err |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return response, err |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
func (d *DocumentAPI) Delete(ctx context.Context, collection string, delete Delete, params *Parameters) (*Response, error) { |
106
|
|
|
|
107
|
|
|
path := fmt.Sprintf("/solr/%s/update", collection) |
108
|
|
|
|
109
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, map[string]interface{}{ |
110
|
|
|
"delete": delete, |
111
|
|
|
}, params, nil) |
112
|
|
|
if err != nil { |
113
|
|
|
return nil, err |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
response, err := d.client.Do(ctx, req) |
117
|
|
|
if err != nil { |
118
|
|
|
return nil, err |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return response, err |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// EXTRACT: Uploading Data with Solr Cell using Apache Tika |
125
|
|
|
func (d *DocumentAPI) Extract(ctx context.Context, collection string, filename string, params *Parameters) (*Response, error) { |
126
|
|
|
file, err := os.Open(filename) |
127
|
|
|
if err != nil { |
128
|
|
|
log.Fatal(err) |
129
|
|
|
} |
130
|
|
|
defer file.Close() |
131
|
|
|
|
132
|
|
|
body := &bytes.Buffer{} |
133
|
|
|
writer := multipart.NewWriter(body) |
134
|
|
|
part, err := writer.CreateFormFile("document", filepath.Base(file.Name())) |
135
|
|
|
if err != nil { |
136
|
|
|
log.Fatal(err) |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
_, err = io.Copy(part, file) |
140
|
|
|
if err != nil { |
141
|
|
|
return nil, err |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
err = writer.Close() |
145
|
|
|
if err != nil { |
146
|
|
|
return nil, err |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/extract", collection) |
150
|
|
|
|
151
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, body, params, &map[string]string{ |
152
|
|
|
"Content-Type": "application/octet-stream", |
153
|
|
|
}) |
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
|
|
|
} |