001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.plugin.jdbc.client;
017
018import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
019import org.elasticsearch.action.delete.DeleteRequest;
020import org.elasticsearch.action.index.IndexRequest;
021import org.elasticsearch.client.Client;
022import org.elasticsearch.common.settings.ImmutableSettings;
023import org.elasticsearch.common.settings.Settings;
024import org.elasticsearch.common.unit.ByteSizeValue;
025import org.elasticsearch.common.unit.TimeValue;
026
027import java.io.IOException;
028import java.io.InputStream;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * Interface for providing convenient administrative methods for ingesting data into Elasticsearch.
034 */
035public interface Ingest {
036
037    /**
038     * Index document
039     *
040     * @param index  the index
041     * @param type   the type
042     * @param id     the id
043     * @param source the source
044     * @return this
045     */
046    Ingest index(String index, String type, String id, String source);
047
048    /**
049     * Delete document
050     *
051     * @param index the index
052     * @param type  the type
053     * @param id    the id
054     * @return this
055     */
056    Ingest delete(String index, String type, String id);
057
058    Ingest newClient(Client client);
059
060    Ingest newClient(Settings settings);
061
062    Ingest newClient(Map<String, String> settings);
063
064    List<String> getConnectedNodes();
065
066    Client client();
067
068    /**
069     * Set the maximum number of actions per bulk request
070     *
071     * @param maxActions maximum number of bulk actions
072     * @return this ingest
073     */
074    Ingest maxActionsPerBulkRequest(int maxActions);
075
076    /**
077     * Set the maximum concurent bulk requests
078     *
079     * @param maxConcurentBulkRequests maximum number of concurrent ingest requests
080     * @return this Ingest
081     */
082    Ingest maxConcurrentBulkRequests(int maxConcurentBulkRequests);
083
084    /**
085     * Set the maximum volume for bulk request before flush
086     *
087     * @param maxVolume maximum volume
088     * @return this ingest
089     */
090    Ingest maxVolumePerBulkRequest(ByteSizeValue maxVolume);
091
092    /**
093     * Set the flush interval for automatic flushing outstanding ingest requests
094     *
095     * @param flushInterval the flush interval, default is 30 seconds
096     * @return this ingest
097     */
098    Ingest flushIngestInterval(TimeValue flushInterval);
099
100    /**
101     * Set request timeout. Default is 60s.
102     *
103     * @param timeout timeout
104     * @return this ingest
105     */
106    Ingest maxRequestWait(TimeValue timeout);
107
108    /**
109     * The number of shards for index creation
110     *
111     * @param shards the number of shards
112     * @return this
113     */
114    Ingest shards(int shards);
115
116    /**
117     * The number of replica for index creation
118     *
119     * @param replica the number of replica
120     * @return this
121     */
122    Ingest replica(int replica);
123
124    void setSettings(Settings settings);
125
126    ImmutableSettings.Builder getSettingsBuilder();
127
128    Settings getSettings();
129
130    /**
131     * Create settings
132     *
133     * @param in the input stream with settings
134     */
135    void setting(InputStream in) throws IOException;
136
137    /**
138     * Create a key/value in the settings
139     *
140     * @param key   the key
141     * @param value the value
142     */
143    void addSetting(String key, String value);
144
145    /**
146     * Create a key/value in the settings
147     *
148     * @param key   the key
149     * @param value the value
150     */
151    void addSetting(String key, Boolean value);
152
153    /**
154     * Create a key/value in the settings
155     *
156     * @param key   the key
157     * @param value the value
158     */
159    void addSetting(String key, Integer value);
160
161    void mapping(String type, InputStream in) throws IOException;
162
163    void mapping(String type, String mapping) throws IOException;
164
165    Map<String, String> getMappings();
166
167    Ingest putMapping(String index);
168
169    Ingest deleteMapping(String index, String type);
170
171    /**
172     * Create a new index
173     *
174     * @return this ingest
175     */
176    Ingest newIndex(String index) throws IOException;
177
178    Ingest newIndex(String index, String type, InputStream settings, InputStream mappings) throws IOException;
179
180    Ingest newIndex(String index, Settings settings, Map<String, String> mappings) throws IOException;
181
182    /**
183     * Delete index
184     *
185     * @return this ingest
186     */
187    Ingest deleteIndex(String index);
188
189    /**
190     * Start bulk mode
191     *
192     * @return this ingest
193     */
194    Ingest startBulk(String index) throws IOException;
195
196    /**
197     * Stops bulk mode. Enables refresh.
198     *
199     * @return this Ingest
200     */
201    Ingest stopBulk(String index) throws IOException;
202
203    /**
204     * Bulked index request. Each request will be added to a queue for bulking requests.
205     * Submitting request will be done when bulk limits are exceeded.
206     *
207     * @param indexRequest the index request to add
208     * @return this ingest
209     */
210    Ingest bulkIndex(IndexRequest indexRequest);
211
212    /**
213     * Bulked delete request. Each request will be added to a queue for bulking requests.
214     * Submitting request will be done when bulk limits are exceeded.
215     *
216     * @param deleteRequest the delete request to add
217     * @return this ingest
218     */
219    Ingest bulkDelete(DeleteRequest deleteRequest);
220
221    /**
222     * Flush ingest, move all pending documents to the bulk indexer
223     *
224     * @return this
225     */
226    Ingest flushIngest();
227
228    /**
229     * Wait for all outstanding responses
230     *
231     * @param maxWait maximum wait time
232     * @return this ingest
233     * @throws InterruptedException
234     */
235    Ingest waitForResponses(TimeValue maxWait) throws InterruptedException;
236
237    /**
238     * Flush the index
239     */
240    Ingest flush(String index);
241
242    /**
243     * Refresh the index.
244     *
245     * @return this ingest
246     */
247    Ingest refresh(String index);
248
249    /**
250     * Add replica level.
251     *
252     * @param level the replica level
253     * @return number of shards after updating replica level
254     */
255    int updateReplicaLevel(String index, int level) throws IOException;
256
257    /**
258     * Wait for cluster being healthy.
259     *
260     * @throws IOException
261     */
262    Ingest waitForCluster(ClusterHealthStatus status, TimeValue timeValue) throws IOException;
263
264    /**
265     * Wait for index recovery (after replica change)
266     *
267     * @return number of shards found
268     */
269    int waitForRecovery(String index) throws IOException;
270
271    Metric getMetric();
272
273    boolean hasThrowable();
274
275    /**
276     * Return last throwable if exists.
277     *
278     * @return last throwable
279     */
280    Throwable getThrowable();
281
282    /**
283     * Shutdown the ingesting
284     */
285    void shutdown();
286
287    boolean isShutdown();
288
289    void suspend();
290
291    void resume();
292}