-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolr.go
276 lines (246 loc) · 7.52 KB
/
solr.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Package solr provides functions to connect to Solr and make request
// for getting individual documents, executing searches, updating, and
// deleteing documents.
//
// This package is geared towards supporting a web user interface that
// queries and filters Solr via facets. As such it provides functionality
// to handle the typical request-response workflow of a web application.
// For example SearchResponse provides URLs to re-execute a search and
// handle pagination, likewise the Facets returned in a SearchResponse
// include URLs to add or remove a filter for a given facet field/value.
//
// Most basic search usage:
//
// s := solr.New("http://localhost/solr/some-core", false)
// results := s.SearchText("hello")
// log.Printf("%v", results)
//
// Search with options:
// s := solr.New("http://localhost/solr/some-core", false)
// qs := url.Values{
// "q": []string{"title:\"one two\""},
// }
// options := map[string]interface{}{
// "defType": "edismax",
// }
// facets := map[string]string{
// "title_str" : "Title",
// }
// params := NewSearchParams(qs, options, facets)
// results := s.Search(params)
// log.Printf("%v", results)
//
// Search with options from a query string (req is *http.Request
// from a web handler)
//
// s := solr.New("http://localhost/solr/some-core", false)
// options := map[string]interface{}{}
// facets := map[string]string{}
// params := NewSearchParams(req.URL.Query(), options, facets)
// results := s.Search(params)
// log.Printf("%v", results)
//
package solr
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// The main class to drive interaction with Solr.
type Solr struct {
CoreUrl string
Verbose bool
}
// Creates a new instance of Solr.
// When verbose = true it will log.Printf() the HTTP requests to Solr.
func New(coreUrl string, verbose bool) Solr {
return Solr{CoreUrl: coreUrl, Verbose: verbose}
}
func (s Solr) Count() (int, error) {
options := map[string]string{"rows": "0", "defType": "edismax", "wt": "json"}
facets := map[string]string{}
params := NewSearchParams("*", options, facets)
r, err := s.Search(params)
return r.NumFound, err
}
// Get fetches a single document from Solr.
func (s Solr) Get(params GetParams) (Document, error) {
url := s.CoreUrl + "/select?" + params.toSolrQueryString()
raw, err := s.httpGet(url)
if err != nil {
return Document{}, err
}
count := len(raw.Data.Documents)
if count == 0 {
return Document{}, nil
} else if count > 1 {
msg := fmt.Sprintf("More than one document was found (Q=%s)", params.Q)
return Document{}, errors.New(msg)
}
return newDocumentFromSolrDoc(raw.Data.Documents[0]), err
}
// Issues a search with the values indicated in the paramers.
func (s Solr) Search(params SearchParams) (SearchResponse, error) {
url := s.CoreUrl + "/select?" + params.toSolrQueryString()
raw, err := s.httpGet(url)
if err != nil {
return SearchResponse{}, err
}
return newSearchResponse(params, raw), err
}
// Updates a single document in Solr with the data in the
// document provided.
func (s Solr) PostDoc(doc Document) error {
docs := []Document{doc}
return s.PostDocs(docs)
}
// Updates an array of documents in Solr.
func (s Solr) PostDocs(docs []Document) error {
// Extract the data from the documents
// (i.e. only the data, without the highlight properties)
data := []map[string]interface{}{}
for _, doc := range docs {
data = append(data, doc.Data)
}
return s.Post(data)
}
// Updates a single document in Solr. Uses plain Go map[string]interface{}
// object rather than a Document object. The map key is represents
// the field name and the map value the field value.
func (s Solr) PostOne(datum map[string]interface{}) error {
data := []map[string]interface{}{datum}
return s.Post(data)
}
// Post issues an HTTP POST to Solr with data data of the
// documents indicated in the data parameter.
//
// The parameter data is a plain Go map[string]interface{} object
// rather than an array of Document objects. The map key represents
// the field name and the map value the field value.
func (s Solr) Post(data []map[string]interface{}) error {
bytes, err := json.Marshal(data)
if err != nil {
return err
}
return s.PostString(string(bytes))
}
// PostString issues an HTTP POST the `/update` handler to update or
// add one or more documents to Solr. The string is assummed to be
// a valid representation of one or more documents.
func (s Solr) PostString(data string) error {
contentType := "application/json"
params := "wt=json&commit=true"
url := s.CoreUrl + "/update?" + params
r, err := s.httpPost(url, contentType, data)
if err != nil {
return err
}
var response responseRaw
err = json.Unmarshal([]byte(r), &response)
if err != nil {
return err
}
if response.Header.Status != 0 {
errorMsg := fmt.Sprintf("Solr returned status %d", response.Header.Status)
return errors.New(errorMsg)
}
return nil
}
// Deletes from Solr all the documents
func (s Solr) DeleteAll() error {
payload := "<delete><query>*:*</query></delete>"
return s.deletePayload(payload)
}
// Deletes from Solr the documents with the IDs indicated.
func (s Solr) Delete(ids []string) error {
payload := "<delete>\r\n"
for _, id := range ids {
payload += "\t<id>" + id + "</id>\r\n"
}
payload += "</delete>"
return s.deletePayload(payload)
}
func (s Solr) deletePayload(payload string) error {
// notice that the request body (contentType) is in XML
// but the response (wt) is in JSON
contentType := "text/xml"
params := "wt=json&commit=true"
url := s.CoreUrl + "/update?" + params
r, err := s.httpPost(url, contentType, payload)
if err != nil {
return err
}
var response responseRaw
err = json.Unmarshal([]byte(r), &response)
if err != nil {
return err
}
if response.Header.Status != 0 {
errorMsg := fmt.Sprintf("Solr returned status %d", response.Header.Status)
return errors.New(errorMsg)
}
return nil
}
// Issues a search for the text indicated. Uses the server's default
// values for all other Solr parameters.
func (s Solr) SearchText(text string) (SearchResponse, error) {
options := map[string]string{}
facets := map[string]string{}
params := NewSearchParams(text, options, facets)
return s.Search(params)
}
func (s Solr) httpGet(url string) (responseRaw, error) {
s.log("Solr HTTP GET", url)
r, err := http.Get(url)
if err != nil {
return responseRaw{}, err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return responseRaw{}, err
}
if r.StatusCode < 200 || r.StatusCode > 299 {
msg := fmt.Sprintf("HTTP Status: %s. ", r.Status)
if len(body) > 0 {
msg += fmt.Sprintf("Body: %s", body)
}
return responseRaw{}, errors.New(msg)
}
response, err := NewResponseRaw([]byte(body))
if err == nil {
// HTTP request was successful but Solr reported an error.
if response.Error.Trace != "" {
msg := fmt.Sprintf("Solr Error. %#v", response.Error)
err = errors.New(msg)
}
} else {
if len(r.Header["Content-Type"]) > 0 {
// Perhaps the response was not in JSON
// (e.g. if Solr returns XML by default)
msg := fmt.Sprintf("%s. Solr's response Content-Type: %s", err, r.Header["Content-Type"])
err = errors.New(msg)
}
}
return response, err
}
func (s Solr) httpPost(url, contentType, body string) (string, error) {
s.log("Solr HTTP POST", url)
payload := bytes.NewBufferString(body)
r, err := http.Post(url, contentType, payload)
if err != nil {
return "", err
}
defer r.Body.Close()
respStr, err := ioutil.ReadAll(r.Body)
return string(respStr), nil
}
func (s Solr) log(msg1, msg2 string) {
if s.Verbose {
log.Printf("%s: %s", msg1, msg2)
}
}