search_request.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. package elastic
  5. import "strings"
  6. // SearchRequest combines a search request and its
  7. // query details (see SearchSource).
  8. // It is used in combination with MultiSearch.
  9. type SearchRequest struct {
  10. searchType string // default in ES is "query_then_fetch"
  11. indices []string
  12. types []string
  13. routing *string
  14. preference *string
  15. requestCache *bool
  16. ignoreUnavailable *bool
  17. allowNoIndices *bool
  18. expandWildcards string
  19. scroll string
  20. source interface{}
  21. }
  22. // NewSearchRequest creates a new search request.
  23. func NewSearchRequest() *SearchRequest {
  24. return &SearchRequest{}
  25. }
  26. // SearchRequest must be one of "query_then_fetch", "query_and_fetch",
  27. // "scan", "count", "dfs_query_then_fetch", or "dfs_query_and_fetch".
  28. // Use one of the constants defined via SearchType.
  29. func (r *SearchRequest) SearchType(searchType string) *SearchRequest {
  30. r.searchType = searchType
  31. return r
  32. }
  33. func (r *SearchRequest) SearchTypeDfsQueryThenFetch() *SearchRequest {
  34. return r.SearchType("dfs_query_then_fetch")
  35. }
  36. func (r *SearchRequest) SearchTypeDfsQueryAndFetch() *SearchRequest {
  37. return r.SearchType("dfs_query_and_fetch")
  38. }
  39. func (r *SearchRequest) SearchTypeQueryThenFetch() *SearchRequest {
  40. return r.SearchType("query_then_fetch")
  41. }
  42. func (r *SearchRequest) SearchTypeQueryAndFetch() *SearchRequest {
  43. return r.SearchType("query_and_fetch")
  44. }
  45. func (r *SearchRequest) SearchTypeScan() *SearchRequest {
  46. return r.SearchType("scan")
  47. }
  48. func (r *SearchRequest) SearchTypeCount() *SearchRequest {
  49. return r.SearchType("count")
  50. }
  51. func (r *SearchRequest) Index(indices ...string) *SearchRequest {
  52. r.indices = append(r.indices, indices...)
  53. return r
  54. }
  55. func (r *SearchRequest) HasIndices() bool {
  56. return len(r.indices) > 0
  57. }
  58. func (r *SearchRequest) Type(types ...string) *SearchRequest {
  59. r.types = append(r.types, types...)
  60. return r
  61. }
  62. func (r *SearchRequest) Routing(routing string) *SearchRequest {
  63. r.routing = &routing
  64. return r
  65. }
  66. func (r *SearchRequest) Routings(routings ...string) *SearchRequest {
  67. if routings != nil {
  68. routings := strings.Join(routings, ",")
  69. r.routing = &routings
  70. } else {
  71. r.routing = nil
  72. }
  73. return r
  74. }
  75. func (r *SearchRequest) Preference(preference string) *SearchRequest {
  76. r.preference = &preference
  77. return r
  78. }
  79. func (r *SearchRequest) RequestCache(requestCache bool) *SearchRequest {
  80. r.requestCache = &requestCache
  81. return r
  82. }
  83. // IgnoreUnavailable indicates whether specified concrete indices should be
  84. // ignored when unavailable (missing or closed).
  85. func (s *SearchRequest) IgnoreUnavailable(ignoreUnavailable bool) *SearchRequest {
  86. s.ignoreUnavailable = &ignoreUnavailable
  87. return s
  88. }
  89. // AllowNoIndices indicates whether to ignore if a wildcard indices
  90. // expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified).
  91. func (s *SearchRequest) AllowNoIndices(allowNoIndices bool) *SearchRequest {
  92. s.allowNoIndices = &allowNoIndices
  93. return s
  94. }
  95. // ExpandWildcards indicates whether to expand wildcard expression to
  96. // concrete indices that are open, closed or both.
  97. func (s *SearchRequest) ExpandWildcards(expandWildcards string) *SearchRequest {
  98. s.expandWildcards = expandWildcards
  99. return s
  100. }
  101. func (r *SearchRequest) Scroll(scroll string) *SearchRequest {
  102. r.scroll = scroll
  103. return r
  104. }
  105. func (r *SearchRequest) SearchSource(searchSource *SearchSource) *SearchRequest {
  106. return r.Source(searchSource)
  107. }
  108. func (r *SearchRequest) Source(source interface{}) *SearchRequest {
  109. switch v := source.(type) {
  110. case *SearchSource:
  111. src, err := v.Source()
  112. if err != nil {
  113. // Do not do anything in case of an error
  114. return r
  115. }
  116. r.source = src
  117. default:
  118. r.source = source
  119. }
  120. return r
  121. }
  122. // header is used e.g. by MultiSearch to get information about the search header
  123. // of one SearchRequest.
  124. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-multi-search.html
  125. func (r *SearchRequest) header() interface{} {
  126. h := make(map[string]interface{})
  127. if r.searchType != "" {
  128. h["search_type"] = r.searchType
  129. }
  130. switch len(r.indices) {
  131. case 0:
  132. case 1:
  133. h["index"] = r.indices[0]
  134. default:
  135. h["indices"] = r.indices
  136. }
  137. switch len(r.types) {
  138. case 0:
  139. case 1:
  140. h["type"] = r.types[0]
  141. default:
  142. h["types"] = r.types
  143. }
  144. if r.routing != nil && *r.routing != "" {
  145. h["routing"] = *r.routing
  146. }
  147. if r.preference != nil && *r.preference != "" {
  148. h["preference"] = *r.preference
  149. }
  150. if r.requestCache != nil {
  151. h["request_cache"] = *r.requestCache
  152. }
  153. if r.ignoreUnavailable != nil {
  154. h["ignore_unavailable"] = *r.ignoreUnavailable
  155. }
  156. if r.allowNoIndices != nil {
  157. h["allow_no_indices"] = *r.allowNoIndices
  158. }
  159. if r.expandWildcards != "" {
  160. h["expand_wildcards"] = r.expandWildcards
  161. }
  162. if r.scroll != "" {
  163. h["scroll"] = r.scroll
  164. }
  165. return h
  166. }
  167. // body is used by MultiSearch to get information about the search body
  168. // of one SearchRequest.
  169. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-multi-search.html
  170. func (r *SearchRequest) body() interface{} {
  171. return r.source
  172. }