forked from sunmi-OS/gocore
/
schema.go
187 lines (161 loc) · 4.94 KB
/
schema.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
package solr
import (
"fmt"
"net/url"
"strings"
)
type Schema struct {
url *url.URL
core string
username string
password string
}
// NewSchema will parse solrUrl and return a schema object, solrUrl must be a absolute url or path
func NewSchema(solrUrl, core string) (*Schema, error) {
u, err := url.ParseRequestURI(strings.TrimRight(solrUrl, "/"))
if err != nil {
return nil, err
}
return &Schema{url: u, core: core}, nil
}
// Set to a new core
func (s *Schema) SetCore(core string) {
s.core = core
}
func (s *Schema) SetBasicAuth(username, password string) {
s.username = username
s.password = password
}
// See Get requests in https://wiki.apache.org/solr/SchemaRESTAPI for detail
func (s *Schema) Get(path string, params *url.Values) (*SolrResponse, error) {
var (
r []byte
err error
)
if params == nil {
params = &url.Values{}
}
params.Set("wt", "json")
if path != "" {
path = fmt.Sprintf("/%s", strings.Trim(path, "/"))
}
if s.core != "" {
r, err = HTTPGet(fmt.Sprintf("%s/%s/schema%s?%s", s.url.String(), s.core, path, params.Encode()), nil, s.username, s.password)
} else {
r, err = HTTPGet(fmt.Sprintf("%s/schema%s?%s", s.url.String(), path, params.Encode()), nil, s.username, s.password)
}
if err != nil {
return nil, err
}
resp, err := bytes2json(&r)
if err != nil {
return nil, err
}
return &SolrResponse{Response: resp, Status: int(resp["responseHeader"].(map[string]interface{})["status"].(float64))}, nil
}
// Return entire schema, require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) All() (*SolrResponse, error) {
return s.Get("", nil)
}
// Require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) Uniquekey() (*SolrResponse, error) {
return s.Get("uniquekey", nil)
}
// Require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) Version() (*SolrResponse, error) {
return s.Get("version", nil)
}
// Return name of schema, require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) Name() (*SolrResponse, error) {
return s.Get("name", nil)
}
// see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) Fields(fl string, includeDynamic, showDefaults bool) (*SolrResponse, error) {
params := &url.Values{}
if includeDynamic {
params.Set("includeDynamic", "true")
}
if showDefaults {
params.Set("showDefaults", "true")
}
if fl != "" {
params.Set("fl", fl)
}
return s.Get("fields", params)
}
// see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) FieldsName(name string, includeDynamic, showDefaults bool) (*SolrResponse, error) {
params := &url.Values{}
if includeDynamic {
params.Set("includeDynamic", "true")
}
if showDefaults {
params.Set("showDefaults", "true")
}
return s.Get(fmt.Sprintf("fields/%s", name), params)
}
// see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) Fieldtypes(showDefaults bool) (*SolrResponse, error) {
params := &url.Values{}
if showDefaults {
params.Set("showDefaults", "true")
}
return s.Get("fieldtypes", params)
}
// see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) FieldtypesName(name string, showDefaults bool) (*SolrResponse, error) {
params := &url.Values{}
if showDefaults {
params.Set("showDefaults", "true")
}
return s.Get(fmt.Sprintf("fieldtypes/%s", name), params)
}
// see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) DynamicFields(fl string, showDefaults bool) (*SolrResponse, error) {
params := &url.Values{}
if showDefaults {
params.Set("showDefaults", "true")
}
if fl != "" {
params.Set("fl", fl)
}
return s.Get("dynamicfields", params)
}
// see https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) DynamicFieldsName(name string, showDefaults bool) (*SolrResponse, error) {
params := &url.Values{}
if showDefaults {
params.Set("showDefaults", "true")
}
return s.Get(fmt.Sprintf("dynamicfields/%s", name), params)
}
// For modify schema, require Solr4.4, currently one can add fields and copy fields.
// Example: s.Post("fields", data) for adding new fields.
// See https://wiki.apache.org/solr/SchemaRESTAPI
func (s *Schema) Post(path string, data interface{}) (*SolrUpdateResponse, error) {
var (
r []byte
err error
)
b, err := json2bytes(data)
if err != nil {
return nil, err
}
if s.core != "" {
r, err = HTTPPost(fmt.Sprintf("%s/%s/schema/%s?wt=json", s.url.String(), s.core, strings.Trim(path, "/")), b, [][]string{{"Content-Type", "application/json"}}, s.username, s.password)
} else {
r, err = HTTPPost(fmt.Sprintf("%s/schema/%s?wt=json", s.url.String(), strings.Trim(path, "/")), b, [][]string{{"Content-Type", "application/json"}}, s.username, s.password)
}
if err != nil {
return nil, err
}
resp, err := bytes2json(&r)
if err != nil {
return nil, err
}
// check error in resp
if !successStatus(resp) || hasError(resp) {
return &SolrUpdateResponse{Success: false, Result: resp}, nil
}
return &SolrUpdateResponse{Success: true, Result: resp}, nil
}