import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
+import java.util.Map;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.util.ProgressLogger;
-import java.util.Map;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
-
import static java.lang.String.format;
/**
/**
* Delete all the documents matching the given search request. This method is blocking.
* Index is refreshed, so docs are not searchable as soon as method is executed.
+ *
+ * Note that the parameter indexName could be removed if progress logs are not needed.
*/
public static void delete(EsClient client, String indexName, SearchRequestBuilder searchRequest) {
BulkIndexer bulk = new BulkIndexer(client, indexName);
import java.sql.Statement;
import org.apache.commons.dbutils.DbUtils;
import org.elasticsearch.action.support.IndicesOptions;
-import org.elasticsearch.index.query.QueryBuilders;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.log.Loggers;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
import org.sonar.db.version.DatabaseVersion;
+import org.sonar.server.es.BulkIndexer;
import org.sonar.server.es.EsClient;
import org.sonar.server.issue.index.IssueIndexDefinition;
import org.sonar.server.view.index.ViewIndexDefinition;
+import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
+
@ServerSide
public class BackendCleanup {
public void clearIndexes() {
Loggers.get(getClass()).info("Truncate Elasticsearch indices");
try {
- esClient.prepareClearCache()
- .get();
- esClient.prepareDeleteByQuery(esClient.prepareState().get()
- .getState().getMetaData().concreteAllIndices())
- .setQuery(QueryBuilders.matchAllQuery())
- .get();
- esClient.prepareRefresh(esClient.prepareState().get()
- .getState().getMetaData().concreteAllIndices())
- .get();
- esClient.prepareFlush(esClient.prepareState().get()
- .getState().getMetaData().concreteAllIndices())
- .get();
+ esClient.prepareClearCache().get();
+
+ for (String index : esClient.prepareState().get().getState().getMetaData().concreteAllIndices()) {
+ clearIndex(index);
+ }
} catch (Exception e) {
throw new IllegalStateException("Unable to clear indexes", e);
}
* Completely remove a index with all types
*/
public void clearIndex(String indexName) {
- esClient.prepareDeleteByQuery(esClient.prepareState().get()
- .getState().getMetaData().concreteIndices(IndicesOptions.strictExpand(), indexName))
- .setQuery(QueryBuilders.matchAllQuery())
- .get();
+ String[] indicesToClear = esClient.prepareState().get().getState().getMetaData().concreteIndices(IndicesOptions.strictExpand(), indexName);
+ for (String index : indicesToClear) {
+ BulkIndexer.delete(esClient, index, esClient.prepareSearch(index).setQuery(matchAllQuery()));
+ }
}
}
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.index.query.FilterBuilders;
-import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
+import org.sonar.server.es.BulkIndexer;
import org.sonar.server.es.EsClient;
import static com.google.common.collect.Lists.newArrayList;
+import static org.elasticsearch.index.query.FilterBuilders.termsFilter;
+import static org.elasticsearch.index.query.QueryBuilders.filteredQuery;
+import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
@ServerSide
@ComputeEngineSide
.setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES))
.setFetchSource(ViewIndexDefinition.FIELD_UUID, null)
.setSize(100)
- .setQuery(QueryBuilders.matchAllQuery());
+ .setQuery(matchAllQuery());
SearchResponse response = esSearch.get();
List<String> result = newArrayList();
}
public void delete(Collection<String> viewUuids) {
- esClient
- .prepareDeleteByQuery(ViewIndexDefinition.INDEX)
+ SearchRequestBuilder searchRequest = esClient.prepareSearch(ViewIndexDefinition.INDEX)
.setTypes(ViewIndexDefinition.TYPE_VIEW)
- .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
- FilterBuilders.termsFilter(ViewIndexDefinition.FIELD_UUID, viewUuids)
- ))
- .get();
+ .setQuery(filteredQuery(matchAllQuery(), termsFilter(ViewIndexDefinition.FIELD_UUID, viewUuids)
+ ));
+ BulkIndexer.delete(esClient, ViewIndexDefinition.INDEX, searchRequest);
}
}
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
public class EsTester extends ExternalResource {
}
public void truncateIndices() {
- client.prepareDeleteByQuery(client.prepareState().get()
- .getState().getMetaData().concreteAllIndices())
- .setQuery(QueryBuilders.matchAllQuery())
- .get();
- client.prepareRefresh(client.prepareState().get()
- .getState().getMetaData().concreteAllIndices())
- .setForce(true)
- .get();
- client.prepareFlush(client.prepareState().get()
- .getState().getMetaData().concreteAllIndices())
- .get();
+ String[] indices = client.prepareState().get().getState().getMetaData().concreteAllIndices();
+ for (String index : indices) {
+ BulkIndexer.delete(client, index, client().prepareSearch(index).setQuery(matchAllQuery()));
+ }
}
public void putDocuments(String index, String type, Class<?> testClass, String... jsonPaths) throws Exception {