percolate.go 8.5 KB

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