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, docs []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, docs, 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) Commit(ctx context.Context, collection string) (*Response, error) { |
70
|
|
|
|
71
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/json", collection) |
72
|
|
|
|
73
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, nil, Parameters{ |
74
|
|
|
Commit: true, |
75
|
|
|
}, nil) |
76
|
|
|
if err != nil { |
77
|
|
|
return nil, err |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
response, err := d.client.Do(ctx, req) |
81
|
|
|
if err != nil { |
82
|
|
|
return nil, err |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return response, err |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
func (d *DocumentAPI) Delete(ctx context.Context, collection string, delete Delete, params *Parameters) (*Response, error) { |
89
|
|
|
|
90
|
|
|
path := fmt.Sprintf("/solr/%s/update", collection) |
91
|
|
|
|
92
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, map[string]interface{}{ |
93
|
|
|
"delete": delete, |
94
|
|
|
}, params, nil) |
95
|
|
|
if err != nil { |
96
|
|
|
return nil, err |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
response, err := d.client.Do(ctx, req) |
100
|
|
|
if err != nil { |
101
|
|
|
return nil, err |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return response, err |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// EXTRACT: Uploading Data with Solr Cell using Apache Tika |
108
|
|
|
func (d *DocumentAPI) Extract(ctx context.Context, collection string, filename string, params *Parameters) (*Response, error) { |
109
|
|
|
file, err := os.Open(filename) |
110
|
|
|
if err != nil { |
111
|
|
|
log.Fatal(err) |
112
|
|
|
} |
113
|
|
|
defer file.Close() |
114
|
|
|
|
115
|
|
|
body := &bytes.Buffer{} |
116
|
|
|
writer := multipart.NewWriter(body) |
117
|
|
|
part, err := writer.CreateFormFile("document", filepath.Base(file.Name())) |
118
|
|
|
if err != nil { |
119
|
|
|
log.Fatal(err) |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
_, err = io.Copy(part, file) |
123
|
|
|
if err != nil { |
124
|
|
|
return nil, err |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
err = writer.Close() |
128
|
|
|
if err != nil { |
129
|
|
|
return nil, err |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
path := fmt.Sprintf("/api/collections/%s/update/extract", collection) |
133
|
|
|
|
134
|
|
|
req, err := d.client.NewRequest(ctx, http.MethodPost, path, body, params, &map[string]string{ |
135
|
|
|
"Content-Type": "application/octet-stream", |
136
|
|
|
}) |
137
|
|
|
if err != nil { |
138
|
|
|
return nil, err |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
response, err := d.client.Do(ctx, req) |
142
|
|
|
if err != nil { |
143
|
|
|
return nil, err |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return response, err |
147
|
|
|
} |