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
16type OptimizeService struct {
17	client             *Client
18	indices            []string
19	maxNumSegments     *int
20	onlyExpungeDeletes *bool
21	flush              *bool
22	waitForMerge       *bool
23	force              *bool
24	pretty             bool
25}
26
27func NewOptimizeService(client *Client) *OptimizeService {
28	builder := &OptimizeService{
29		client:  client,
30		indices: make([]string, 0),
31	}
32	return builder
33}
34
35func (s *OptimizeService) Index(index string) *OptimizeService {
36	s.indices = append(s.indices, index)
37	return s
38}
39
40func (s *OptimizeService) Indices(indices ...string) *OptimizeService {
41	s.indices = append(s.indices, indices...)
42	return s
43}
44
45func (s *OptimizeService) MaxNumSegments(maxNumSegments int) *OptimizeService {
46	s.maxNumSegments = &maxNumSegments
47	return s
48}
49
50func (s *OptimizeService) OnlyExpungeDeletes(onlyExpungeDeletes bool) *OptimizeService {
51	s.onlyExpungeDeletes = &onlyExpungeDeletes
52	return s
53}
54
55func (s *OptimizeService) Flush(flush bool) *OptimizeService {
56	s.flush = &flush
57	return s
58}
59
60func (s *OptimizeService) WaitForMerge(waitForMerge bool) *OptimizeService {
61	s.waitForMerge = &waitForMerge
62	return s
63}
64
65func (s *OptimizeService) Force(force bool) *OptimizeService {
66	s.force = &force
67	return s
68}
69
70func (s *OptimizeService) Pretty(pretty bool) *OptimizeService {
71	s.pretty = pretty
72	return s
73}
74
75func (s *OptimizeService) Do() (*OptimizeResult, error) {
76	// Build url
77	path := "/"
78
79	// Indices part
80	indexPart := make([]string, 0)
81	for _, index := range s.indices {
82		index, err := uritemplates.Expand("{index}", map[string]string{
83			"index": index,
84		})
85		if err != nil {
86			return nil, err
87		}
88		indexPart = append(indexPart, index)
89	}
90	if len(indexPart) > 0 {
91		path += strings.Join(indexPart, ",")
92	}
93
94	path += "/_optimize"
95
96	// Parameters
97	params := make(url.Values)
98	if s.maxNumSegments != nil {
99		params.Set("max_num_segments", fmt.Sprintf("%d", *s.maxNumSegments))
100	}
101	if s.onlyExpungeDeletes != nil {
102		params.Set("only_expunge_deletes", fmt.Sprintf("%v", *s.onlyExpungeDeletes))
103	}
104	if s.flush != nil {
105		params.Set("flush", fmt.Sprintf("%v", *s.flush))
106	}
107	if s.waitForMerge != nil {
108		params.Set("wait_for_merge", fmt.Sprintf("%v", *s.waitForMerge))
109	}
110	if s.force != nil {
111		params.Set("force", fmt.Sprintf("%v", *s.force))
112	}
113	if s.pretty {
114		params.Set("pretty", fmt.Sprintf("%v", s.pretty))
115	}
116
117	// Get response
118	res, err := s.client.PerformRequest("POST", path, params, nil)
119	if err != nil {
120		return nil, err
121	}
122
123	// Return result
124	ret := new(OptimizeResult)
125	if err := json.Unmarshal(res.Body, ret); err != nil {
126		return nil, err
127	}
128	return ret, nil
129}
130
131// -- Result of an optimize request.
132
133type OptimizeResult struct {
134	Shards shardsInfo `json:"_shards,omitempty"`
135}
136