indices_analyze.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 (
  6. "context"
  7. "fmt"
  8. "net/url"
  9. "gopkg.in/olivere/elastic.v5/uritemplates"
  10. )
  11. // IndicesAnalyzeService performs the analysis process on a text and returns
  12. // the tokens breakdown of the text.
  13. //
  14. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-analyze.html
  15. // for detail.
  16. type IndicesAnalyzeService struct {
  17. client *Client
  18. pretty bool
  19. index string
  20. request *IndicesAnalyzeRequest
  21. format string
  22. preferLocal *bool
  23. bodyJson interface{}
  24. bodyString string
  25. }
  26. // NewIndicesAnalyzeService creates a new IndicesAnalyzeService.
  27. func NewIndicesAnalyzeService(client *Client) *IndicesAnalyzeService {
  28. return &IndicesAnalyzeService{
  29. client: client,
  30. request: new(IndicesAnalyzeRequest),
  31. }
  32. }
  33. // Index is the name of the index to scope the operation.
  34. func (s *IndicesAnalyzeService) Index(index string) *IndicesAnalyzeService {
  35. s.index = index
  36. return s
  37. }
  38. // Format of the output.
  39. func (s *IndicesAnalyzeService) Format(format string) *IndicesAnalyzeService {
  40. s.format = format
  41. return s
  42. }
  43. // PreferLocal, when true, specifies that a local shard should be used
  44. // if available. When false, a random shard is used (default: true).
  45. func (s *IndicesAnalyzeService) PreferLocal(preferLocal bool) *IndicesAnalyzeService {
  46. s.preferLocal = &preferLocal
  47. return s
  48. }
  49. // Request passes the analyze request to use.
  50. func (s *IndicesAnalyzeService) Request(request *IndicesAnalyzeRequest) *IndicesAnalyzeService {
  51. if request == nil {
  52. s.request = new(IndicesAnalyzeRequest)
  53. } else {
  54. s.request = request
  55. }
  56. return s
  57. }
  58. // Analyzer is the name of the analyzer to use.
  59. func (s *IndicesAnalyzeService) Analyzer(analyzer string) *IndicesAnalyzeService {
  60. s.request.Analyzer = analyzer
  61. return s
  62. }
  63. // Attributes is a list of token attributes to output; this parameter works
  64. // only with explain=true.
  65. func (s *IndicesAnalyzeService) Attributes(attributes ...string) *IndicesAnalyzeService {
  66. s.request.Attributes = attributes
  67. return s
  68. }
  69. // CharFilter is a list of character filters to use for the analysis.
  70. func (s *IndicesAnalyzeService) CharFilter(charFilter ...string) *IndicesAnalyzeService {
  71. s.request.CharFilter = charFilter
  72. return s
  73. }
  74. // Explain, when true, outputs more advanced details (default: false).
  75. func (s *IndicesAnalyzeService) Explain(explain bool) *IndicesAnalyzeService {
  76. s.request.Explain = explain
  77. return s
  78. }
  79. // Field specifies to use a specific analyzer configured for this field (instead of passing the analyzer name).
  80. func (s *IndicesAnalyzeService) Field(field string) *IndicesAnalyzeService {
  81. s.request.Field = field
  82. return s
  83. }
  84. // Filter is a list of filters to use for the analysis.
  85. func (s *IndicesAnalyzeService) Filter(filter ...string) *IndicesAnalyzeService {
  86. s.request.Filter = filter
  87. return s
  88. }
  89. // Text is the text on which the analysis should be performed (when request body is not used).
  90. func (s *IndicesAnalyzeService) Text(text ...string) *IndicesAnalyzeService {
  91. s.request.Text = text
  92. return s
  93. }
  94. // Tokenizer is the name of the tokenizer to use for the analysis.
  95. func (s *IndicesAnalyzeService) Tokenizer(tokenizer string) *IndicesAnalyzeService {
  96. s.request.Tokenizer = tokenizer
  97. return s
  98. }
  99. // Pretty indicates that the JSON response be indented and human readable.
  100. func (s *IndicesAnalyzeService) Pretty(pretty bool) *IndicesAnalyzeService {
  101. s.pretty = pretty
  102. return s
  103. }
  104. // BodyJson is the text on which the analysis should be performed.
  105. func (s *IndicesAnalyzeService) BodyJson(body interface{}) *IndicesAnalyzeService {
  106. s.bodyJson = body
  107. return s
  108. }
  109. // BodyString is the text on which the analysis should be performed.
  110. func (s *IndicesAnalyzeService) BodyString(body string) *IndicesAnalyzeService {
  111. s.bodyString = body
  112. return s
  113. }
  114. // buildURL builds the URL for the operation.
  115. func (s *IndicesAnalyzeService) buildURL() (string, url.Values, error) {
  116. // Build URL
  117. var err error
  118. var path string
  119. if s.index == "" {
  120. path = "/_analyze"
  121. } else {
  122. path, err = uritemplates.Expand("/{index}/_analyze", map[string]string{
  123. "index": s.index,
  124. })
  125. }
  126. if err != nil {
  127. return "", url.Values{}, err
  128. }
  129. // Add query string parameters
  130. params := url.Values{}
  131. if s.pretty {
  132. params.Set("pretty", "1")
  133. }
  134. if s.format != "" {
  135. params.Set("format", s.format)
  136. }
  137. if s.preferLocal != nil {
  138. params.Set("prefer_local", fmt.Sprintf("%v", *s.preferLocal))
  139. }
  140. return path, params, nil
  141. }
  142. // Do will execute the request with the given context.
  143. func (s *IndicesAnalyzeService) Do(ctx context.Context) (*IndicesAnalyzeResponse, error) {
  144. // Check pre-conditions
  145. if err := s.Validate(); err != nil {
  146. return nil, err
  147. }
  148. path, params, err := s.buildURL()
  149. if err != nil {
  150. return nil, err
  151. }
  152. // Setup HTTP request body
  153. var body interface{}
  154. if s.bodyJson != nil {
  155. body = s.bodyJson
  156. } else if s.bodyString != "" {
  157. body = s.bodyString
  158. } else {
  159. // Request parameters are deprecated in 5.1.1, and we must use a JSON
  160. // structure in the body to pass the parameters.
  161. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-analyze.html
  162. body = s.request
  163. }
  164. res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
  165. if err != nil {
  166. return nil, err
  167. }
  168. ret := new(IndicesAnalyzeResponse)
  169. if err = s.client.decoder.Decode(res.Body, ret); err != nil {
  170. return nil, err
  171. }
  172. return ret, nil
  173. }
  174. func (s *IndicesAnalyzeService) Validate() error {
  175. var invalid []string
  176. if s.bodyJson == nil && s.bodyString == "" {
  177. if len(s.request.Text) == 0 {
  178. invalid = append(invalid, "Text")
  179. }
  180. }
  181. if len(invalid) > 0 {
  182. return fmt.Errorf("missing required fields: %v", invalid)
  183. }
  184. return nil
  185. }
  186. // IndicesAnalyzeRequest specifies the parameters of the analyze request.
  187. type IndicesAnalyzeRequest struct {
  188. Text []string `json:"text,omitempty"`
  189. Analyzer string `json:"analyzer,omitempty"`
  190. Tokenizer string `json:"tokenizer,omitempty"`
  191. Filter []string `json:"filter,omitempty"`
  192. CharFilter []string `json:"char_filter,omitempty"`
  193. Field string `json:"field,omitempty"`
  194. Explain bool `json:"explain,omitempty"`
  195. Attributes []string `json:"attributes,omitempty"`
  196. }
  197. type IndicesAnalyzeResponse struct {
  198. Tokens []IndicesAnalyzeResponseToken `json:"tokens"` // json part for normal message
  199. Detail IndicesAnalyzeResponseDetail `json:"detail"` // json part for verbose message of explain request
  200. }
  201. type IndicesAnalyzeResponseToken struct {
  202. Token string `json:"token"`
  203. StartOffset int `json:"start_offset"`
  204. EndOffset int `json:"end_offset"`
  205. Type string `json:"type"`
  206. Position int `json:"position"`
  207. }
  208. type IndicesAnalyzeResponseDetail struct {
  209. CustomAnalyzer bool `json:"custom_analyzer"`
  210. Charfilters []interface{} `json:"charfilters"`
  211. Analyzer struct {
  212. Name string `json:"name"`
  213. Tokens []struct {
  214. Token string `json:"token"`
  215. StartOffset int `json:"start_offset"`
  216. EndOffset int `json:"end_offset"`
  217. Type string `json:"type"`
  218. Position int `json:"position"`
  219. Bytes string `json:"bytes"`
  220. PositionLength int `json:"positionLength"`
  221. } `json:"tokens"`
  222. } `json:"analyzer"`
  223. Tokenizer struct {
  224. Name string `json:"name"`
  225. Tokens []struct {
  226. Token string `json:"token"`
  227. StartOffset int `json:"start_offset"`
  228. EndOffset int `json:"end_offset"`
  229. Type string `json:"type"`
  230. Position int `json:"position"`
  231. } `json:"tokens"`
  232. } `json:"tokenizer"`
  233. Tokenfilters []struct {
  234. Name string `json:"name"`
  235. Tokens []struct {
  236. Token string `json:"token"`
  237. StartOffset int `json:"start_offset"`
  238. EndOffset int `json:"end_offset"`
  239. Type string `json:"type"`
  240. Position int `json:"position"`
  241. Keyword bool `json:"keyword"`
  242. } `json:"tokens"`
  243. } `json:"tokenfilters"`
  244. }