001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     tdelprat
018 *     bdelbosc
019 */
020
021package org.nuxeo.elasticsearch.api;
022
023import java.util.List;
024import java.util.NoSuchElementException;
025
026import org.elasticsearch.client.Client;
027
028import com.google.common.util.concurrent.ListenableFuture;
029
030/**
031 * Administration interface for Elasticsearch service
032 *
033 * @since 5.9.3
034 */
035public interface ElasticSearchAdmin {
036
037    /**
038     * Retrieves the {@link Client} that can be used to access Elasticsearch API
039     *
040     * @since 5.9.3
041     */
042    ESClient getClient();
043
044    /**
045     * Initialize Elasticsearch indexes. Setup the index settings and mapping for each index that has been registered.
046     *
047     * @param dropIfExists if {true} remove an existing index
048     * @since 5.9.3
049     */
050    void initIndexes(boolean dropIfExists);
051
052    /**
053     * Reinitialize an index. This will drop the existing index, recreate it with its settings and mapping, the index
054     * will be empty.
055     *
056     * @since 7.3
057     */
058    void dropAndInitIndex(String indexName);
059
060    /**
061     * Reinitialize the index of a repository. This will drop the existing index, recreate it with its settings and
062     * mapping, the index will be empty.
063     *
064     * @since 7.1
065     */
066    default void dropAndInitRepositoryIndex(String repositoryName) {
067        dropAndInitRepositoryIndex(repositoryName, true);
068    }
069
070    /**
071     * Reinitialize the index of a repository. This will drop the existing index, recreate it with its settings and
072     * mapping, the index will be empty. When syncAlias is false then search alias is not updated with the new index,
073     * you need to explicitly call {@link #syncSearchAndWriteAlias(String)}
074     *
075     * @since 9.3
076     */
077    void dropAndInitRepositoryIndex(String repositoryName, boolean syncAlias);
078
079    /**
080     * List repository names that have Elasticsearch support.
081     *
082     * @since 7.1
083     */
084    List<String> getRepositoryNames();
085
086    /**
087     * Get the search index name associated with the repository name.
088     *
089     * @throws NoSuchElementException if there is no Elasticsearch index associated with the requested repository.
090     * @since 7.2
091     */
092    String getIndexNameForRepository(String repositoryName);
093
094    /**
095     * Gets the repository name associated with the index.
096     *
097     * @since 9.10
098     */
099    String getRepositoryForIndex(String indexName);
100
101    /**
102     * Get the index names with the given type.
103     *
104     * @since 7.10
105     */
106    List<String> getIndexNamesForType(String type);
107
108    /**
109     * Get the first search index name with the given type.
110     *
111     * @throws NoSuchElementException if there is no Elasticsearch index with the given type.
112     * @since 7.10
113     */
114    String getIndexNameForType(String type);
115
116    /**
117     * Returns the index to use for any write operations.
118     *
119     * @since 9.3
120     */
121    String getWriteIndexName(String searchIndexName);
122
123    /**
124     * Make sure that the search alias point to the same index as the write alias.
125     *
126     * @since 9.3
127     */
128    void syncSearchAndWriteAlias(String searchIndexName);
129
130    /**
131     * Returns true if there are indexing activities scheduled or running.
132     *
133     * @since 5.9.5
134     */
135    boolean isIndexingInProgress();
136
137    /**
138     * A {@link java.util.concurrent.Future} that accepts callback on completion when all the indexing worker are done.
139     *
140     * @since 7.2
141     */
142    ListenableFuture<Boolean> prepareWaitForIndexing();
143
144    /**
145     * Refresh all document indexes, immediately after the operation occurs, so that the updated document appears in
146     * search results immediately. There is no fsync thus doesn't guarantee durability.
147     *
148     * @since 5.9.3
149     */
150    void refresh();
151
152    /**
153     * Refresh document index for the specific repository, immediately after the operation occurs, so that the updated
154     * document appears in search results immediately. There is no fsync thus doesn't guarantee durability.
155     *
156     * @since 5.9.4
157     */
158    void refreshRepositoryIndex(String repositoryName);
159
160    /**
161     * Elasticsearch flush on all document indexes, triggers a lucene commit, empties the transaction log. Data is
162     * flushed to disk.
163     *
164     * @since 5.9.3
165     */
166    void flush();
167
168    /**
169     * Elasticsearch flush on document index for a specific repository, triggers a lucene commit, empties the
170     * transaction log. Data is flushed to disk.
171     *
172     * @since 5.9.4
173     */
174    void flushRepositoryIndex(String repositoryName);
175
176    /**
177     * Elasticsearch run {@link ElasticSearchAdmin#optimizeRepositoryIndex} on all document indexes,
178     *
179     * @since 7.2
180     */
181    void optimize();
182
183    /**
184     * Elasticsearch optimize operation allows to reduce the number of segments to one, Note that this can potentially
185     * be a very heavy operation.
186     *
187     * @since 7.2
188     */
189    void optimizeRepositoryIndex(String repositoryName);
190
191    /**
192     * Elasticsearch optimize operation allows to reduce the number of segments to one, Note that this can potentially
193     * be a very heavy operation.
194     *
195     * @since 7.3
196     */
197    void optimizeIndex(String indexName);
198
199    /**
200     * Returns the number of indexing worker scheduled waiting to be executed.
201     *
202     * @since 7.1
203     */
204    long getPendingWorkerCount();
205
206    /**
207     * Returns the number of indexing worker that are currently running.
208     *
209     * @since 7.1
210     */
211    long getRunningWorkerCount();
212
213    /**
214     * Returns the total number of command processed by Elasticsearch for this Nuxeo instance. Useful for test
215     * assertion.
216     *
217     * @since 5.9.4
218     */
219    int getTotalCommandProcessed();
220
221    /**
222     * Returns true if the Elasticsearch is embedded with Nuxeo, sharing the same JVM.
223     *
224     * @since 7.2
225     */
226    boolean isEmbedded();
227
228    /**
229     * When true use an external version for Elasticsearch document, this enable an optimistic concurrency control
230     * ensuring that an older version of a document never overwrites a newer version.
231     *
232     * @since 8.3
233     */
234    boolean useExternalVersion();
235
236}