1// Copyright 2012-2015 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 5package elastic 6 7import ( 8 "encoding/json" 9 "fmt" 10 "net/url" 11 "strings" 12 13 "gopkg.in/olivere/elastic.v2/uritemplates" 14) 15 16// PercolateService is documented at http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/search-percolate.html. 17type PercolateService struct { 18 client *Client 19 pretty bool 20 index string 21 typ string 22 id string 23 version interface{} 24 versionType string 25 routing []string 26 preference string 27 ignoreUnavailable *bool 28 percolateIndex string 29 percolatePreference string 30 percolateRouting string 31 source string 32 allowNoIndices *bool 33 expandWildcards string 34 percolateFormat string 35 percolateType string 36 bodyJson interface{} 37 bodyString string 38} 39 40// NewPercolateService creates a new PercolateService. 41func NewPercolateService(client *Client) *PercolateService { 42 return &PercolateService{ 43 client: client, 44 routing: make([]string, 0), 45 } 46} 47 48// Index is the name of the index of the document being percolated. 49func (s *PercolateService) Index(index string) *PercolateService { 50 s.index = index 51 return s 52} 53 54// Type is the type of the document being percolated. 55func (s *PercolateService) Type(typ string) *PercolateService { 56 s.typ = typ 57 return s 58} 59 60// Id is to substitute the document in the request body with a 61// document that is known by the specified id. On top of the id, 62// the index and type parameter will be used to retrieve 63// the document from within the cluster. 64func (s *PercolateService) Id(id string) *PercolateService { 65 s.id = id 66 return s 67} 68 69// ExpandWildcards indicates whether to expand wildcard expressions 70// to concrete indices that are open, closed or both. 71func (s *PercolateService) ExpandWildcards(expandWildcards string) *PercolateService { 72 s.expandWildcards = expandWildcards 73 return s 74} 75 76// PercolateFormat indicates whether to return an array of matching 77// query IDs instead of objects. 78func (s *PercolateService) PercolateFormat(percolateFormat string) *PercolateService { 79 s.percolateFormat = percolateFormat 80 return s 81} 82 83// PercolateType is the type to percolate document into. Defaults to type. 84func (s *PercolateService) PercolateType(percolateType string) *PercolateService { 85 s.percolateType = percolateType 86 return s 87} 88 89// PercolateRouting is the routing value to use when percolating 90// the existing document. 91func (s *PercolateService) PercolateRouting(percolateRouting string) *PercolateService { 92 s.percolateRouting = percolateRouting 93 return s 94} 95 96// Source is the URL-encoded request definition. 97func (s *PercolateService) Source(source string) *PercolateService { 98 s.source = source 99 return s 100} 101 102// AllowNoIndices indicates whether to ignore if a wildcard indices 103// expression resolves into no concrete indices. 104// (This includes `_all` string or when no indices have been specified). 105func (s *PercolateService) AllowNoIndices(allowNoIndices bool) *PercolateService { 106 s.allowNoIndices = &allowNoIndices 107 return s 108} 109 110// IgnoreUnavailable indicates whether specified concrete indices should 111// be ignored when unavailable (missing or closed). 112func (s *PercolateService) IgnoreUnavailable(ignoreUnavailable bool) *PercolateService { 113 s.ignoreUnavailable = &ignoreUnavailable 114 return s 115} 116 117// PercolateIndex is the index to percolate the document into. Defaults to index. 118func (s *PercolateService) PercolateIndex(percolateIndex string) *PercolateService { 119 s.percolateIndex = percolateIndex 120 return s 121} 122 123// PercolatePreference defines which shard to prefer when executing 124// the percolate request. 125func (s *PercolateService) PercolatePreference(percolatePreference string) *PercolateService { 126 s.percolatePreference = percolatePreference 127 return s 128} 129 130// Version is an explicit version number for concurrency control. 131func (s *PercolateService) Version(version interface{}) *PercolateService { 132 s.version = version 133 return s 134} 135 136// VersionType is the specific version type. 137func (s *PercolateService) VersionType(versionType string) *PercolateService { 138 s.versionType = versionType 139 return s 140} 141 142// Routing is a list of specific routing values. 143func (s *PercolateService) Routing(routing []string) *PercolateService { 144 s.routing = routing 145 return s 146} 147 148// Preference specifies the node or shard the operation should be 149// performed on (default: random). 150func (s *PercolateService) Preference(preference string) *PercolateService { 151 s.preference = preference 152 return s 153} 154 155// Pretty indicates that the JSON response be indented and human readable. 156func (s *PercolateService) Pretty(pretty bool) *PercolateService { 157 s.pretty = pretty 158 return s 159} 160 161// Doc wraps the given document into the "doc" key of the body. 162func (s *PercolateService) Doc(doc interface{}) *PercolateService { 163 return s.BodyJson(map[string]interface{}{"doc": doc}) 164} 165 166// BodyJson is the percolator request definition using the percolate DSL. 167func (s *PercolateService) BodyJson(body interface{}) *PercolateService { 168 s.bodyJson = body 169 return s 170} 171 172// BodyString is the percolator request definition using the percolate DSL. 173func (s *PercolateService) BodyString(body string) *PercolateService { 174 s.bodyString = body 175 return s 176} 177 178// buildURL builds the URL for the operation. 179func (s *PercolateService) buildURL() (string, url.Values, error) { 180 // Build URL 181 var path string 182 var err error 183 if s.id == "" { 184 path, err = uritemplates.Expand("/{index}/{type}/_percolate", map[string]string{ 185 "index": s.index, 186 "type": s.typ, 187 }) 188 } else { 189 path, err = uritemplates.Expand("/{index}/{type}/{id}/_percolate", map[string]string{ 190 "index": s.index, 191 "type": s.typ, 192 "id": s.id, 193 }) 194 } 195 if err != nil { 196 return "", url.Values{}, err 197 } 198 199 // Add query string parameters 200 params := url.Values{} 201 if s.pretty { 202 params.Set("pretty", "1") 203 } 204 if s.version != nil { 205 params.Set("version", fmt.Sprintf("%v", s.version)) 206 } 207 if s.versionType != "" { 208 params.Set("version_type", s.versionType) 209 } 210 if len(s.routing) > 0 { 211 params.Set("routing", strings.Join(s.routing, ",")) 212 } 213 if s.preference != "" { 214 params.Set("preference", s.preference) 215 } 216 if s.ignoreUnavailable != nil { 217 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) 218 } 219 if s.percolateIndex != "" { 220 params.Set("percolate_index", s.percolateIndex) 221 } 222 if s.percolatePreference != "" { 223 params.Set("percolate_preference", s.percolatePreference) 224 } 225 if s.percolateRouting != "" { 226 params.Set("percolate_routing", s.percolateRouting) 227 } 228 if s.source != "" { 229 params.Set("source", s.source) 230 } 231 if s.allowNoIndices != nil { 232 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) 233 } 234 if s.expandWildcards != "" { 235 params.Set("expand_wildcards", s.expandWildcards) 236 } 237 if s.percolateFormat != "" { 238 params.Set("percolate_format", s.percolateFormat) 239 } 240 if s.percolateType != "" { 241 params.Set("percolate_type", s.percolateType) 242 } 243 return path, params, nil 244} 245 246// Validate checks if the operation is valid. 247func (s *PercolateService) Validate() error { 248 var invalid []string 249 if s.index == "" { 250 invalid = append(invalid, "Index") 251 } 252 if s.typ == "" { 253 invalid = append(invalid, "Type") 254 } 255 if len(invalid) > 0 { 256 return fmt.Errorf("missing required fields: %v", invalid) 257 } 258 return nil 259} 260 261// Do executes the operation. 262func (s *PercolateService) Do() (*PercolateResponse, error) { 263 // Check pre-conditions 264 if err := s.Validate(); err != nil { 265 return nil, err 266 } 267 268 // Get URL for request 269 path, params, err := s.buildURL() 270 if err != nil { 271 return nil, err 272 } 273 274 // Setup HTTP request body 275 var body interface{} 276 if s.bodyJson != nil { 277 body = s.bodyJson 278 } else { 279 body = s.bodyString 280 } 281 282 // Get HTTP response 283 res, err := s.client.PerformRequest("GET", path, params, body) 284 if err != nil { 285 return nil, err 286 } 287 288 // Return operation response 289 ret := new(PercolateResponse) 290 if err := json.Unmarshal(res.Body, ret); err != nil { 291 return nil, err 292 } 293 return ret, nil 294} 295 296// PercolateResponse is the response of PercolateService.Do. 297type PercolateResponse struct { 298 TookInMillis int64 `json:"took"` // search time in milliseconds 299 Total int64 `json:"total"` // total matches 300 Matches []*PercolateMatch `json:"matches,omitempty"` 301 Facets SearchFacets `json:"facets,omitempty"` // results from facets 302 Aggregations Aggregations `json:"aggregations,omitempty"` // results from aggregations 303} 304 305// PercolateMatch returns a single match in a PercolateResponse. 306type PercolateMatch struct { 307 Index string `json:"_index,omitempty"` 308 Id string `json:"_id"` 309 Score float64 `json:"_score,omitempty"` 310} 311