-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
60 lines (53 loc) · 1.82 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
package zincapi
import (
"fmt"
"github.com/guonaihong/gout"
jsoniter "github.com/json-iterator/go"
"os"
)
type Search struct {
scope *Index // 文档归属索引
}
type SearchResponse struct {
Took int64 `json:"took"`
TimedOut bool `json:"timed_out"`
MaxScore float64 `json:"max_score"`
Hits Hits `json:"hits"`
Buckets interface{} `json:"buckets"`
Error string `json:"error"`
}
type SearchType string
const (
SearchTypeMatch SearchType = "match"
SearchTypeMatchAll SearchType = "matchall"
SearchTypeMatchPhrase SearchType = "matchphrase"
SearchTypeTerm SearchType = "term"
SearchTypeQueryString SearchType = "querystring"
SearchTypePrefix SearchType = "prefix"
SearchTypeWildcard SearchType = "wildcard"
SearchTypeFuzzy SearchType = "fuzzy"
SearchTypeDateRange SearchType = "daterange"
)
type QueryParam struct {
SearchType SearchType `json:"search_type"`
Query map[string]interface{} `json:"query"`
SortFields []string `json:"sort_fields,omitempty"`
From int64 `json:"from"`
MaxResults int64 `json:"max_results"`
Source []string `json:"_source,omitempty"`
Aggregate map[string]interface{} `json:"aggs,omitempty"`
Highlight map[string]interface{} `json:"highlight,omitempty"`
}
func (s *Search) Search(filter QueryParam) (*SearchResponse, error) {
b, err := jsoniter.Marshal(filter)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "zinc search: %v\n", err)
return nil, err
}
var resp = new(SearchResponse)
if err := gout.POST(getURI(fmt.Sprintf("/api/%s/_search", s.scope.Name))).Debug(true).SetHeader(getHeader()).SetBody(b).BindJSON(resp).Do(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "zinc search: %v\n", err)
return nil, err
}
return resp, nil
}