This repository has been archived by the owner on Jan 21, 2018. It is now read-only.
/
requests.go
147 lines (118 loc) · 2.9 KB
/
requests.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
package elasticsearch
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
// Helper function which turns a map of strings into url.Values, omitting empty
// values.
func values(v map[string]string) url.Values {
values := url.Values{}
for key, value := range v {
if value != "" {
values.Set(key, value)
}
}
return values
}
// Fireable defines anything which can be fired against the search cluster.
type Fireable interface {
Request(uri *url.URL) (*http.Request, error)
}
//
//
//
type SearchParams struct {
Indices []string `json:"index,omitempty"`
Types []string `json:"type,omitempty"`
Routing string `json:"routing,omitempty"`
Preference string `json:"preference,omitempty"`
SearchType string `json:"search_type,omitempty"`
}
func (p SearchParams) Values() url.Values {
return values(map[string]string{
"routing": p.Routing,
"preference": p.Preference,
"search_type": p.SearchType,
})
}
type SearchRequest struct {
Params SearchParams
Query SubQuery
}
func (r SearchRequest) EncodeMultiHeader(enc *json.Encoder) error {
return enc.Encode(r.Params)
}
func (r SearchRequest) EncodeQuery(enc *json.Encoder) error {
return enc.Encode(r.Query)
}
func (r SearchRequest) Request(uri *url.URL) (*http.Request, error) {
uri.Path = r.Path()
uri.RawQuery = r.Params.Values().Encode()
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
if err := r.EncodeQuery(enc); err != nil {
return nil, err
}
return http.NewRequest("GET", uri.String(), buf)
}
func (r SearchRequest) Path() string {
switch true {
case len(r.Params.Indices) == 0 && len(r.Params.Types) == 0:
return fmt.Sprintf(
"/_search", // all indices, all types
)
case len(r.Params.Indices) > 0 && len(r.Params.Types) == 0:
return fmt.Sprintf(
"/%s/_search",
strings.Join(r.Params.Indices, ","),
)
case len(r.Params.Indices) == 0 && len(r.Params.Types) > 0:
return fmt.Sprintf(
"/_all/%s/_search",
strings.Join(r.Params.Types, ","),
)
case len(r.Params.Indices) > 0 && len(r.Params.Types) > 0:
return fmt.Sprintf(
"/%s/%s/_search",
strings.Join(r.Params.Indices, ","),
strings.Join(r.Params.Types, ","),
)
}
panic("unreachable")
}
//
//
//
type MultiSearchParams struct {
Indices []string
Types []string
SearchType string
}
func (p MultiSearchParams) Values() url.Values {
return values(map[string]string{
"search_type": p.SearchType,
})
}
type MultiSearchRequest struct {
Params MultiSearchParams
Requests []SearchRequest
}
func (r MultiSearchRequest) Request(uri *url.URL) (*http.Request, error) {
uri.Path = "/_msearch"
uri.RawQuery = r.Params.Values().Encode()
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
for _, req := range r.Requests {
if err := req.EncodeMultiHeader(enc); err != nil {
return nil, err
}
if err := req.EncodeQuery(enc); err != nil {
return nil, err
}
}
return http.NewRequest("GET", uri.String(), buf)
}