Debian Codegraph @

search_request.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import (
8 "encoding/json"
9 "strings"
10 )
11
12 // SearchRequest combines a search request and its
13 // query details (see SearchSource).
14 // It is used in combination with MultiSearch.
15 type SearchRequest struct {
16 searchType string
17 indices []string
18 types []string
19 routing *string
20 preference *string
21 requestCache *bool
22 ignoreUnavailable *bool
23 allowNoIndices *bool
24 expandWildcards string
25 scroll string
26 source interface{}
27 }
28
29 // NewSearchRequest creates a new search request.
30 func NewSearchRequest() *SearchRequest {
31 return &SearchRequest{}
32 }
33
34 // SearchRequest must be one of dfs_query_then_fetch, query_then_fetch
35 // or query_and_fetch (deprecated in 5.3).
36 func (r *SearchRequest) SearchType(searchType string) *SearchRequest {
37 r.searchType = searchType
38 return r
39 }
40
41 // SearchTypeDfsQueryThenFetch sets search type to dfs_query_then_fetch.
42 func (r *SearchRequest) SearchTypeDfsQueryThenFetch() *SearchRequest {
43 return r.SearchType("dfs_query_then_fetch")
44 }
45
46 // SearchTypeQueryThenFetch sets search type to query_then_fetch.
47 func (r *SearchRequest) SearchTypeQueryThenFetch() *SearchRequest {
48 return r.SearchType("query_then_fetch")
49 }
50
51 // SearchTypeQueryAndFetch sets search type to query_and_fetch which
52 // was deprecated in 5.3.
53 func (r *SearchRequest) SearchTypeQueryAndFetch() *SearchRequest {
54 return r.SearchType("query_and_fetch")
55 }
56
57 func (r *SearchRequest) Index(indices ...string) *SearchRequest {
58 r.indices = append(r.indices, indices...)
59 return r
60 }
61
62 func (r *SearchRequest) HasIndices() bool {
63 return len(r.indices) > 0
64 }
65
66 func (r *SearchRequest) Type(types ...string) *SearchRequest {
67 r.types = append(r.types, types...)
68 return r
69 }
70
71 func (r *SearchRequest) Routing(routing string) *SearchRequest {
72 r.routing = &routing
73 return r
74 }
75
76 func (r *SearchRequest) Routings(routings ...string) *SearchRequest {
77 if routings != nil {
78 routings := strings.Join(routings, ",")
79 r.routing = &routings
80 } else {
81 r.routing = nil
82 }
83 return r
84 }
85
86 func (r *SearchRequest) Preference(preference string) *SearchRequest {
87 r.preference = &preference
88 return r
89 }
90
91 func (r *SearchRequest) RequestCache(requestCache bool) *SearchRequest {
92 r.requestCache = &requestCache
93 return r
94 }
95
96 // IgnoreUnavailable indicates whether specified concrete indices should be
97 // ignored when unavailable (missing or closed).
98 func (s *SearchRequest) IgnoreUnavailable(ignoreUnavailable bool) *SearchRequest {
99 s.ignoreUnavailable = &ignoreUnavailable
100 return s
101 }
102
103 // AllowNoIndices indicates whether to ignore if a wildcard indices
104 // expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified).
105 func (s *SearchRequest) AllowNoIndices(allowNoIndices bool) *SearchRequest {
106 s.allowNoIndices = &allowNoIndices
107 return s
108 }
109
110 // ExpandWildcards indicates whether to expand wildcard expression to
111 // concrete indices that are open, closed or both.
112 func (s *SearchRequest) ExpandWildcards(expandWildcards string) *SearchRequest {
113 s.expandWildcards = expandWildcards
114 return s
115 }
116
117 func (r *SearchRequest) Scroll(scroll string) *SearchRequest {
118 r.scroll = scroll
119 return r
120 }
121
122 func (r *SearchRequest) SearchSource(searchSource *SearchSource) *SearchRequest {
123 return r.Source(searchSource)
124 }
125
126 func (r *SearchRequest) Source(source interface{}) *SearchRequest {
127 r.source = source
128 return r
129 }
130
131 // header is used e.g. by MultiSearch to get information about the search header
132 // of one SearchRequest.
133 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-multi-search.html
134 func (r *SearchRequest) header() interface{} {
135 h := make(map[string]interface{})
136 if r.searchType != "" {
137 h["search_type"] = r.searchType
138 }
139
140 switch len(r.indices) {
141 case 0:
142 case 1:
143 h["index"] = r.indices[0]
144 default:
145 h["indices"] = r.indices
146 }
147
148 switch len(r.types) {
149 case 0:
150 case 1:
151 h["type"] = r.types[0]
152 default:
153 h["types"] = r.types
154 }
155
156 if r.routing != nil && *r.routing != "" {
157 h["routing"] = *r.routing
158 }
159 if r.preference != nil && *r.preference != "" {
160 h["preference"] = *r.preference
161 }
162 if r.requestCache != nil {
163 h["request_cache"] = *r.requestCache
164 }
165 if r.ignoreUnavailable != nil {
166 h["ignore_unavailable"] = *r.ignoreUnavailable
167 }
168 if r.allowNoIndices != nil {
169 h["allow_no_indices"] = *r.allowNoIndices
170 }
171 if r.expandWildcards != "" {
172 h["expand_wildcards"] = r.expandWildcards
173 }
174 if r.scroll != "" {
175 h["scroll"] = r.scroll
176 }
177
178 return h
179 }
180
181 // Body allows to access the search body of the request, as generated by the DSL.
182 // Notice that Body is read-only. You must not change the request body.
183 //
184 // Body is used e.g. by MultiSearch to get information about the search body
185 // of one SearchRequest.
186 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-multi-search.html
187 func (r *SearchRequest) Body() (string, error) {
188 switch t := r.source.(type) {
189 default:
190 body, err := json.Marshal(r.source)
191 if err != nil {
192 return "", err
193 }
194 return string(body), nil
195 case *SearchSource:
196 src, err := t.Source()
197 if err != nil {
198 return "", err
199 }
200 body, err := json.Marshal(src)
201 if err != nil {
202 return "", err
203 }
204 return string(body), nil
205 case json.RawMessage:
206 return string(t), nil
207 case *json.RawMessage:
208 return string(*t), nil
209 case string:
210 return t, nil
211 case *string:
212 if t != nil {
213 return *t, nil
214 }
215 return "{}", nil
216 }
217 }