Search in sources :

Example 1 with ForceMergeRequest

use of org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest in project elasticsearch by elastic.

the class RestForceMergeAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ForceMergeRequest mergeRequest = new ForceMergeRequest(Strings.splitStringByCommaToArray(request.param("index")));
    mergeRequest.indicesOptions(IndicesOptions.fromRequest(request, mergeRequest.indicesOptions()));
    mergeRequest.maxNumSegments(request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments()));
    mergeRequest.onlyExpungeDeletes(request.paramAsBoolean("only_expunge_deletes", mergeRequest.onlyExpungeDeletes()));
    mergeRequest.flush(request.paramAsBoolean("flush", mergeRequest.flush()));
    return channel -> client.admin().indices().forceMerge(mergeRequest, new RestBuilderListener<ForceMergeResponse>(channel) {

        @Override
        public RestResponse buildResponse(ForceMergeResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) ForceMergeResponse(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse) Strings(org.elasticsearch.common.Strings) ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) ForceMergeResponse(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 2 with ForceMergeRequest

use of org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest in project elasticsearch by elastic.

the class IndicesRequestIT method testForceMerge.

public void testForceMerge() {
    String mergeShardAction = ForceMergeAction.NAME + "[n]";
    interceptTransportActions(mergeShardAction);
    ForceMergeRequest mergeRequest = new ForceMergeRequest(randomIndicesOrAliases());
    internalCluster().coordOnlyNodeClient().admin().indices().forceMerge(mergeRequest).actionGet();
    clearInterceptedActions();
    assertSameIndices(mergeRequest, mergeShardAction);
}
Also used : ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest)

Example 3 with ForceMergeRequest

use of org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest in project graylog2-server by Graylog2.

the class Indices method optimizeIndex.

public void optimizeIndex(String index, int maxNumSegments, Duration timeout) {
    // https://www.elastic.co/guide/en/elasticsearch/reference/2.1/indices-forcemerge.html
    final ForceMergeRequest request = c.admin().indices().prepareForceMerge(index).setMaxNumSegments(maxNumSegments).setOnlyExpungeDeletes(false).setFlush(true).request();
    // Using a specific timeout to override the global Elasticsearch request timeout
    c.admin().indices().forceMerge(request).actionGet(timeout.getQuantity(), timeout.getUnit());
}
Also used : ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest)

Example 4 with ForceMergeRequest

use of org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest in project crate by crate.

the class DDLStatementDispatcher method executeMergeSegments.

private static CompletableFuture<Long> executeMergeSegments(OptimizeTableAnalyzedStatement analysis, TransportForceMergeAction transportForceMergeAction) {
    ForceMergeRequest request = new ForceMergeRequest(analysis.indexNames().toArray(new String[0]));
    // Pass parameters to ES request
    request.maxNumSegments(analysis.settings().getAsInt(OptimizeSettings.MAX_NUM_SEGMENTS.name(), ForceMergeRequest.Defaults.MAX_NUM_SEGMENTS));
    request.onlyExpungeDeletes(analysis.settings().getAsBoolean(OptimizeSettings.ONLY_EXPUNGE_DELETES.name(), ForceMergeRequest.Defaults.ONLY_EXPUNGE_DELETES));
    request.flush(analysis.settings().getAsBoolean(OptimizeSettings.FLUSH.name(), ForceMergeRequest.Defaults.FLUSH));
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    FutureActionListener<ForceMergeResponse, Long> listener = new FutureActionListener<>(Functions.constant((long) analysis.indexNames().size()));
    transportForceMergeAction.execute(request, listener);
    return listener;
}
Also used : ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest) ForceMergeResponse(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse) FutureActionListener(io.crate.action.FutureActionListener)

Example 5 with ForceMergeRequest

use of org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest in project elasticsearch by elastic.

the class IndexShardTests method testDocStats.

public void testDocStats() throws IOException {
    IndexShard indexShard = null;
    try {
        indexShard = newStartedShard();
        // at least two documents so we have docs to delete
        final long numDocs = randomIntBetween(2, 32);
        // Delete at least numDocs/10 documents otherwise the number of deleted docs will be below 10%
        // and forceMerge will refuse to expunge deletes
        final long numDocsToDelete = randomIntBetween((int) Math.ceil(Math.nextUp(numDocs / 10.0)), Math.toIntExact(numDocs));
        for (int i = 0; i < numDocs; i++) {
            final String id = Integer.toString(i);
            final ParsedDocument doc = testParsedDocument(id, "test", null, new ParseContext.Document(), new BytesArray("{}"), null);
            final Engine.Index index = new Engine.Index(new Term("_uid", doc.uid()), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), -1, false);
            final Engine.IndexResult result = indexShard.index(index);
            assertThat(result.getVersion(), equalTo(1L));
        }
        indexShard.refresh("test");
        {
            final DocsStats docsStats = indexShard.docStats();
            assertThat(docsStats.getCount(), equalTo(numDocs));
            assertThat(docsStats.getDeleted(), equalTo(0L));
        }
        final List<Integer> ids = randomSubsetOf(Math.toIntExact(numDocsToDelete), IntStream.range(0, Math.toIntExact(numDocs)).boxed().collect(Collectors.toList()));
        for (final Integer i : ids) {
            final String id = Integer.toString(i);
            final ParsedDocument doc = testParsedDocument(id, "test", null, new ParseContext.Document(), new BytesArray("{}"), null);
            final Engine.Index index = new Engine.Index(new Term("_uid", doc.uid()), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), -1, false);
            final Engine.IndexResult result = indexShard.index(index);
            assertThat(result.getVersion(), equalTo(2L));
        }
        // flush the buffered deletes
        final FlushRequest flushRequest = new FlushRequest();
        flushRequest.force(false);
        flushRequest.waitIfOngoing(false);
        indexShard.flush(flushRequest);
        indexShard.refresh("test");
        {
            final DocsStats docStats = indexShard.docStats();
            assertThat(docStats.getCount(), equalTo(numDocs));
            // Lucene will delete a segment if all docs are deleted from it; this means that we lose the deletes when deleting all docs
            assertThat(docStats.getDeleted(), equalTo(numDocsToDelete == numDocs ? 0 : numDocsToDelete));
        }
        // merge them away
        final ForceMergeRequest forceMergeRequest = new ForceMergeRequest();
        forceMergeRequest.onlyExpungeDeletes(randomBoolean());
        forceMergeRequest.maxNumSegments(1);
        indexShard.forceMerge(forceMergeRequest);
        indexShard.refresh("test");
        {
            final DocsStats docStats = indexShard.docStats();
            assertThat(docStats.getCount(), equalTo(numDocs));
            assertThat(docStats.getDeleted(), equalTo(0L));
        }
    } finally {
        closeShards(indexShard);
    }
}
Also used : ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) Lucene.cleanLuceneIndex(org.elasticsearch.common.lucene.Lucene.cleanLuceneIndex) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) ParseContext(org.elasticsearch.index.mapper.ParseContext) Engine(org.elasticsearch.index.engine.Engine)

Aggregations

ForceMergeRequest (org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest)6 FlushRequest (org.elasticsearch.action.admin.indices.flush.FlushRequest)2 ForceMergeResponse (org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse)2 FutureActionListener (io.crate.action.FutureActionListener)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Term (org.apache.lucene.index.Term)1 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)1 ClusterSearchShardsRequest (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest)1 IndicesAliasesRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest)1 GetAliasesRequest (org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)1 ClearIndicesCacheRequest (org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)1 CloseIndexRequest (org.elasticsearch.action.admin.indices.close.CloseIndexRequest)1 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)1 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)1 IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)1 TypesExistsRequest (org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest)1 SyncedFlushRequest (org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest)1 GetIndexRequest (org.elasticsearch.action.admin.indices.get.GetIndexRequest)1