-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
53 lines (43 loc) · 1.14 KB
/
util.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
package solr
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
// ErrInvalidConfig is returned when the hostname or corename are empty
var ErrInvalidConfig = errors.New("invalid configuration: no host or core provided")
func formatBasePath(host, core string) string {
if strings.HasSuffix(host, "/solr") {
return fmt.Sprintf("%s/%s", host, core)
}
return fmt.Sprintf("%s/solr/%s", host, core)
}
func formatDocEntry(doc Doc) map[string]interface{} {
return map[string]interface{}{"doc": doc}
}
func formatDeleteByID(id string) Doc {
return Doc{"id": id}
}
func formatDeleteByQuery(query string) Doc {
return Doc{"query": query}
}
func isJSON(input []byte) error {
var js map[string]interface{}
return json.Unmarshal(input, &js)
}
func isArrayOfJSON(input []byte) error {
var js []*map[string]interface{}
return json.Unmarshal(input, &js)
}
func interfaceToBytes(a interface{}) ([]byte, error) {
b, err := json.Marshal(a)
if err != nil {
return nil, err
}
return b, err
}
// BoostField is a helper function to properly format field boosting
func BoostField(field string, boost float64) string {
return fmt.Sprintf("%s^%f", field, boost)
}