forked from meilisearch/meilisearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_search.go
129 lines (112 loc) · 3.23 KB
/
index_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
package meilisearch
import (
"encoding/json"
"net/http"
)
// This constant contains the default values assigned by Meilisearch to the limit in search parameters
//
// Documentation: https://docs.meilisearch.com/reference/features/search_parameters.html
const (
DefaultLimit int64 = 20
)
func (i Index) SearchRaw(query string, request *SearchRequest) (*json.RawMessage, error) {
resp := &json.RawMessage{}
if request.Limit == 0 {
request.Limit = DefaultLimit
}
searchPostRequestParams := searchPostRequestParams(query, request)
req := internalRequest{
endpoint: "/indexes/" + i.UID + "/search",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: searchPostRequestParams,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "SearchRaw",
}
if err := i.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func (i Index) Search(query string, request *SearchRequest) (*SearchResponse, error) {
resp := &SearchResponse{}
if request.Limit == 0 {
request.Limit = DefaultLimit
}
if request.IndexUID != "" {
request.IndexUID = ""
}
searchPostRequestParams := searchPostRequestParams(query, request)
req := internalRequest{
endpoint: "/indexes/" + i.UID + "/search",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: searchPostRequestParams,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "Search",
}
if err := i.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func searchPostRequestParams(query string, request *SearchRequest) map[string]interface{} {
params := make(map[string]interface{}, 16)
if !request.PlaceholderSearch {
params["q"] = query
}
if request.IndexUID != "" {
params["indexUid"] = request.IndexUID
}
if request.Limit != DefaultLimit {
params["limit"] = request.Limit
}
if request.ShowMatchesPosition {
params["showMatchesPosition"] = request.ShowMatchesPosition
}
if request.Filter != nil {
params["filter"] = request.Filter
}
if request.Offset != 0 {
params["offset"] = request.Offset
}
if request.CropLength != 0 {
params["cropLength"] = request.CropLength
}
if request.HitsPerPage != 0 {
params["hitsPerPage"] = request.HitsPerPage
}
if request.Page != 0 {
params["page"] = request.Page
}
if request.CropMarker != "" {
params["cropMarker"] = request.CropMarker
}
if request.HighlightPreTag != "" {
params["highlightPreTag"] = request.HighlightPreTag
}
if request.HighlightPostTag != "" {
params["highlightPostTag"] = request.HighlightPostTag
}
if request.MatchingStrategy != "" {
params["matchingStrategy"] = request.MatchingStrategy
}
if len(request.AttributesToRetrieve) != 0 {
params["attributesToRetrieve"] = request.AttributesToRetrieve
}
if len(request.AttributesToCrop) != 0 {
params["attributesToCrop"] = request.AttributesToCrop
}
if len(request.AttributesToHighlight) != 0 {
params["attributesToHighlight"] = request.AttributesToHighlight
}
if len(request.Facets) != 0 {
params["facets"] = request.Facets
}
if len(request.Sort) != 0 {
params["sort"] = request.Sort
}
return params
}