Search in sources :

Example 21 with CreateIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest in project vorto by eclipse.

the class ElasticSearchService method forceReindexAllModels.

/**
 * This forces a full reindexing of all model, and should be used in the rare occasion where
 * a change in the mapping has been created, e.g. a new searchable field, or a change in a field's
 * type.<br/>
 * The following operations are performed in order, synchronously:
 * <ol>
 *   <li>
 *     Checks if the vorto index actually exists. If it doesn't, creates it and returns (nothing
 *     else worth doing).
 *   </li>
 *   <li>
 *     Checks if the vorto temp index ({@link ElasticSearchService#VORTO_INDEX_TEMP}) exists. If
 *     so, deletes the temp index.
 *   </li>
 *   <li>
 *     Creates the temp index with the current mapping.
 *   </li>
 *   <li>
 *     Uses a reindex request to merge the vorto index ({@link ElasticSearchService#VORTO_INDEX})
 *     into the temp index.
 *   </li>
 *   <li>
 *     Deletes the vorto index.
 *   </li>
 *   <li>
 *     Re-creates the vorto index.
 *   </li>
 *   <li>
 *     Merges back the temp index in the vorto index.
 *   </li>
 *   <li>
 *     Deletes the temp index.
 *   </li>
 *   <li>
 *     Reindexes all model in the vorto index - see {@link IIndexingService#reindexAllModels()}.
 *   </li>
 * </ol>
 *
 * @return
 */
@Override
public IndexingResult forceReindexAllModels() {
    // no Vorto index - nothing to do
    if (!indexExist(VORTO_INDEX)) {
        createIndexIfNotExisting();
        return new IndexingResult();
    }
    // first delete the VORTO_INDEX_TEMP index if it exists
    if (indexExist(VORTO_INDEX_TEMP)) {
        DeleteIndexRequest deleteTempIndexRequest = new DeleteIndexRequest().indices(VORTO_INDEX_TEMP);
        try {
            client.indices().delete(deleteTempIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException ioe) {
            throw new IndexingException(ioe.getMessage(), ioe);
        }
    }
    // creates the temporary index to hold the new mapping
    CreateIndexRequest createTempIndexRequest = new CreateIndexRequest(VORTO_INDEX_TEMP);
    createTempIndexRequest.mapping(createMappingForIndex());
    try {
        client.indices().create(createTempIndexRequest, RequestOptions.DEFAULT);
    } catch (IOException ioe) {
        throw new IndexingException(ioe.getMessage(), ioe);
    }
    // copies the vorto index data to the new temp index with new mapping
    ReindexRequest reindexRequest = new ReindexRequest().setSourceIndices(VORTO_INDEX).setDestIndex(VORTO_INDEX_TEMP);
    reindexRequest.setConflicts("proceed");
    reindexRequest.setRefresh(true);
    try {
        client.reindex(reindexRequest, RequestOptions.DEFAULT);
    } catch (IOException ioe) {
        throw new IndexingException(ioe.getMessage(), ioe);
    }
    // deletes the vorto index
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest().indices(VORTO_INDEX);
    try {
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
    } catch (IOException ioe) {
        throw new IndexingException(ioe.getMessage(), ioe);
    }
    // re-creates the vorto index
    createIndexIfNotExisting();
    // moves data back to vorto index
    ReindexRequest moveBackToVortoIndex = new ReindexRequest().setSourceIndices(VORTO_INDEX_TEMP).setDestIndex(VORTO_INDEX);
    reindexRequest.setConflicts("proceed");
    reindexRequest.setRefresh(true);
    try {
        client.reindex(moveBackToVortoIndex, RequestOptions.DEFAULT);
    } catch (IOException ioe) {
        throw new IndexingException(ioe.getMessage(), ioe);
    }
    // deletes the temp index
    DeleteIndexRequest deleteTempIndexRequest = new DeleteIndexRequest().indices(VORTO_INDEX_TEMP);
    try {
        client.indices().delete(deleteTempIndexRequest, RequestOptions.DEFAULT);
    } catch (IOException ioe) {
        throw new IndexingException(ioe.getMessage(), ioe);
    }
    // finally, re-import all models
    return reindexAllModels();
}
Also used : ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest)

Aggregations

CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)17 IOException (java.io.IOException)6 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)6 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 GetIndexRequest (org.elasticsearch.client.indices.GetIndexRequest)5 RefreshRequest (org.elasticsearch.action.admin.indices.refresh.RefreshRequest)4 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)4 CreateIndexResponse (org.elasticsearch.client.indices.CreateIndexResponse)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 CreateIndexRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)2 DocumentMapping (com.b2international.index.mapping.DocumentMapping)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Retryer (com.github.rholder.retry.Retryer)1 RetryerBuilder (com.github.rholder.retry.RetryerBuilder)1 StopStrategies (com.github.rholder.retry.StopStrategies)1