88
99package org .elasticsearch .index .analysis ;
1010
11+ import org .elasticsearch .ElasticsearchStatusException ;
1112import org .elasticsearch .ExceptionsHelper ;
1213import org .elasticsearch .Version ;
1314import org .elasticsearch .action .ActionListener ;
1617import org .elasticsearch .action .ActionType ;
1718import org .elasticsearch .action .DelegatingActionListener ;
1819import org .elasticsearch .action .DocWriteRequest ;
20+ import org .elasticsearch .action .bulk .BulkItemResponse ;
1921import org .elasticsearch .action .index .IndexRequest ;
2022import org .elasticsearch .action .index .TransportIndexAction ;
2123import org .elasticsearch .action .search .SearchRequestBuilder ;
3133import org .elasticsearch .common .util .concurrent .ThreadContext ;
3234import org .elasticsearch .index .IndexNotFoundException ;
3335import org .elasticsearch .index .query .QueryBuilders ;
36+ import org .elasticsearch .index .query .TermQueryBuilder ;
37+ import org .elasticsearch .index .reindex .BulkByScrollResponse ;
38+ import org .elasticsearch .index .reindex .DeleteByQueryAction ;
39+ import org .elasticsearch .index .reindex .DeleteByQueryRequest ;
3440import org .elasticsearch .indices .SystemIndexDescriptor ;
41+ import org .elasticsearch .logging .LogManager ;
42+ import org .elasticsearch .logging .Logger ;
3543import org .elasticsearch .rest .RestStatus ;
3644import org .elasticsearch .xcontent .XContentBuilder ;
3745
3846import java .io .IOException ;
3947import java .io .UncheckedIOException ;
48+ import java .util .List ;
4049import java .util .concurrent .Executor ;
4150import java .util .function .BiConsumer ;
4251import java .util .function .Supplier ;
4554import static org .elasticsearch .xcontent .XContentFactory .jsonBuilder ;
4655
4756public class WordListsIndexService {
57+ private static final Logger logger = LogManager .getLogger (WordListsIndexService .class );
58+
4859 public static final String WORD_LISTS_FEATURE_NAME = "word_lists" ;
4960 public static final String WORD_LISTS_ORIGIN = "word_lists" ;
5061
@@ -175,38 +186,8 @@ public void onFailure(Exception e) {
175186
176187 super .onFailure (e );
177188 }
178- });
179-
180-
181- // client.prepareSearch(WORD_LISTS_ALIAS_NAME)
182- // .setQuery(QueryBuilders.termQuery(WORD_LIST_ID_FIELD, wordListId))
183- // .setSize(1)
184- // .setPreference(Preference.LOCAL.type())
185- // .setTrackTotalHits(true)
186- // .execute(new DelegatingActionListener<>(listener) {
187- // @Override
188- // public void onResponse(SearchResponse searchResponse) {
189- // final long wordListCount = searchResponse.getHits().getTotalHits().value;
190- // if (wordListCount > 1) {
191- // listener.onFailure(new IllegalStateException(wordListCount + " word lists have ID [" + wordListId + "]"));
192- // } else if (wordListCount == 1) {
193- // listener.onResponse((String) searchResponse.getHits().getHits()[0].getSourceAsMap().get(WORD_LIST_VALUE_FIELD));
194- // } else {
195- // listener.onResponse(null);
196- // }
197- // }
198- //
199- // @Override
200- // public void onFailure(Exception e) {
201- // Throwable cause = ExceptionsHelper.unwrapCause(e);
202- // if (cause instanceof IndexNotFoundException) {
203- // delegate.onResponse(null);
204- // return;
205- // }
206- //
207- // super.onFailure(e);
208- // }
209- // });
189+ }
190+ );
210191 }
211192
212193 public void putWordList (String index , String wordListName , String wordListValue , ActionListener <PutWordListResult > listener ) {
@@ -227,14 +208,41 @@ public void putWordList(String index, String wordListName, String wordListValue,
227208 l .onResponse (result );
228209 })
229210 );
211+ }
230212
231- // client.index(indexRequest, listener.delegateFailure((l, indexResponse) -> {
232- // PutWordListResult result = indexResponse.status() == RestStatus.CREATED
233- // ? PutWordListResult.CREATED
234- // : PutWordListResult.UPDATED;
235- //
236- // l.onResponse(result);
237- // }));
213+ public void deleteIndexWordLists (String index , ActionListener <Boolean > listener ) {
214+ TermQueryBuilder queryBuilder = QueryBuilders .termQuery (WORD_LIST_INDEX_FIELD , index );
215+ DeleteByQueryRequest request = new DeleteByQueryRequest ().indices (WORD_LISTS_ALIAS_NAME ).setQuery (queryBuilder );
216+ executeAsyncWithOrigin (client , WORD_LISTS_ORIGIN , DeleteByQueryAction .INSTANCE , request , new ActionListener <>() {
217+ @ Override
218+ public void onResponse (BulkByScrollResponse response ) {
219+ List <BulkItemResponse .Failure > failures = response .getBulkFailures ();
220+ if (failures .isEmpty () == false ) {
221+ logger .warn ("Failed to delete word lists for index [" + index + "]" );
222+ failures .forEach (failure -> logger .warn (failure .getMessage (), failure .getCause ()));
223+ listener .onFailure (
224+ new ElasticsearchStatusException (
225+ "Failed to delete word lists for index [" + index + "]. See log for details." ,
226+ RestStatus .INTERNAL_SERVER_ERROR
227+ )
228+ );
229+ }
230+
231+ boolean deletedWordLists = response .getDeleted () > 0 ;
232+ listener .onResponse (deletedWordLists );
233+ }
234+
235+ @ Override
236+ public void onFailure (Exception e ) {
237+ Throwable cause = ExceptionsHelper .unwrapCause (e );
238+ if (cause instanceof IndexNotFoundException ) {
239+ listener .onResponse (false );
240+ return ;
241+ }
242+
243+ listener .onFailure (e );
244+ }
245+ });
238246 }
239247
240248 private static String generateWordListId (String index , String wordListName ) {
0 commit comments