forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
220 lines (193 loc) · 5.58 KB
/
search.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
// Copyright 2013 Matthew Baird
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package search
import (
"encoding/json"
"fmt"
u "github.com/araddon/gou"
"github.com/zhaocloud/elastigo/api"
"github.com/zhaocloud/elastigo/core"
"log"
"strconv"
"strings"
)
var (
_ = u.DEBUG
)
// Search is the entry point to the SearchDsl, it is a chainable set of utilities
// to create searches.
//
// params
// @index = elasticsearch index to search
//
// out, err := Search("github").Type("Issues").Pretty().Query(
// Query().Range(
// Range().Field("created_at").From("2012-12-10T15:00:00-08:00").To("2012-12-10T15:10:00-08:00"),
// ).Search("add"),
// ).Result()
func Search(index string) *SearchDsl {
return &SearchDsl{Index: index, args: map[string]interface{}{}}
}
type SearchDsl struct {
args map[string]interface{}
types []string
FromVal int `json:"from,omitempty"`
SizeVal int `json:"size,omitempty"`
Index string `json:"-"`
FacetVal *FacetDsl `json:"facets,omitempty"`
QueryVal *QueryDsl `json:"query,omitempty"`
SortBody []*SortDsl `json:"sort,omitempty"`
HighlightVal *Highlighting `json:"highlight,omitempty"`
FilterVal *FilterWrap `json:"filter,omitempty"`
AggregatesVal map[string]*AggregateDsl `json:"aggregations,omitempty"`
}
func (s *SearchDsl) Bytes() ([]byte, error) {
return api.DoCommand("POST", s.url(), s.args, s)
}
func (s *SearchDsl) Result() (*core.SearchResult, error) {
var retval core.SearchResult
if core.DebugRequests {
sb, _ := json.MarshalIndent(s, " ", " ")
log.Println(s.url())
log.Println(string(sb))
}
body, err := s.Bytes()
if err != nil {
u.Errorf("%v", err)
return nil, err
}
jsonErr := json.Unmarshal(body, &retval)
if jsonErr != nil {
u.Errorf("%v \n\t%s", jsonErr, string(body))
}
//Debug(string(body))
return &retval, jsonErr
}
func (s *SearchDsl) url() string {
url := fmt.Sprintf("/%s%s/_search", s.Index, s.getType())
return url
}
func (s *SearchDsl) Pretty() *SearchDsl {
s.args["pretty"] = "1"
return s
}
// Type is the elasticsearch *Type* within a specific index
func (s *SearchDsl) Type(indexType string) *SearchDsl {
if len(s.types) == 0 {
s.types = make([]string, 0)
}
s.types = append(s.types, indexType)
return s
}
func (s *SearchDsl) getType() string {
if len(s.types) > 0 {
return "/" + strings.Join(s.types, ",")
}
return ""
}
func (s *SearchDsl) From(from string) *SearchDsl {
s.args["from"] = from
return s
}
// Search is a simple interface to search, doesn't have the power of query
// but uses a simple query_string search
func (s *SearchDsl) Search(srch string) *SearchDsl {
s.QueryVal = Query().Search(srch)
return s
}
func (s *SearchDsl) Size(size string) *SearchDsl {
s.args["size"] = size
return s
}
func (s *SearchDsl) Fields(fields ...string) *SearchDsl {
s.args["fields"] = strings.Join(fields, ",")
return s
}
func (s *SearchDsl) Source(returnSource bool) *SearchDsl {
s.args["_source"] = strconv.FormatBool(returnSource)
return s
}
func (s *SearchDsl) SearchType(searchType string) *SearchDsl {
s.args["search_type"] = searchType
return s
}
// Facet passes a Query expression to this search
//
// qry := Search("github").Size("0").Facet(
// Facet().Regex("repository.name", "no.*").Size("8"),
// )
//
// qry := Search("github").Pretty().Facet(
// Facet().Fields("type").Size("25"),
// )
func (s *SearchDsl) Facet(f *FacetDsl) *SearchDsl {
s.FacetVal = f
return s
}
func (s *SearchDsl) Aggregates(aggs ...*AggregateDsl) *SearchDsl {
if len(aggs) < 1 {
return s
}
if len(s.AggregatesVal) == 0 {
s.AggregatesVal = make(map[string]*AggregateDsl)
}
for _, agg := range aggs {
s.AggregatesVal[agg.Name] = agg
}
return s
}
func (s *SearchDsl) Query(q *QueryDsl) *SearchDsl {
s.QueryVal = q
return s
}
// Filter adds a Filter Clause with optional Boolean Clause. This accepts n number of
// filter clauses. If more than one, and missing Boolean Clause it assumes "and"
//
// qry := Search("github").Filter(
// Filter().Exists("repository.name"),
// )
//
// qry := Search("github").Filter(
// "or",
// Filter().Exists("repository.name"),
// Filter().Terms("actor_attributes.location", "portland"),
// )
//
// qry := Search("github").Filter(
// Filter().Exists("repository.name"),
// Filter().Terms("repository.has_wiki", true)
// )
func (s *SearchDsl) Filters(fl ...interface{}) *SearchDsl {
if s.FilterVal == nil {
s.FilterVal = NewFilterWrap()
}
s.FilterVal.AddFilters(fl)
return s
}
func (s *SearchDsl) Filter(f *FilterWrap) *SearchDsl {
s.FilterVal = f
return s
}
func (s *SearchDsl) Highlight(h *Highlighting) *SearchDsl {
s.HighlightVal = h
return s
}
func (s *SearchDsl) Sort(sort ...*SortDsl) *SearchDsl {
if s.SortBody == nil {
s.SortBody = make([]*SortDsl, 0)
}
s.SortBody = append(s.SortBody, sort...)
return s
}
func (s *SearchDsl) Scroll(duration string) *SearchDsl {
s.args["scroll"] = duration
return s
}