Search in sources :

Example 1 with PutMappingRequest

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest in project elasticsearch by elastic.

the class IndicesRequestIT method testPutMapping.

public void testPutMapping() {
    interceptTransportActions(PutMappingAction.NAME);
    PutMappingRequest putMappingRequest = new PutMappingRequest(randomUniqueIndicesOrAliases()).type("type").source("field", "type=text");
    internalCluster().coordOnlyNodeClient().admin().indices().putMapping(putMappingRequest).actionGet();
    clearInterceptedActions();
    assertSameIndices(putMappingRequest, PutMappingAction.NAME);
}
Also used : PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest)

Example 2 with PutMappingRequest

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest in project opencast by opencast.

the class AbstractElasticsearchIndex method createIndex.

/**
 * Prepares Elasticsearch index to store data for the types (or mappings) as returned by {@link #getDocumenTypes()}.
 *
 * @param idx
 *          the index name
 *
 * @throws SearchIndexException
 *           if index and type creation fails
 * @throws IOException
 *           if loading of the type definitions fails
 */
private void createIndex(String idx) throws SearchIndexException, IOException {
    // Make sure the site index exists
    try {
        logger.debug("Trying to create index for '{}'", idx);
        CreateIndexRequest indexCreateRequest = new CreateIndexRequest(idx);
        String settings = getIndexSettings(idx);
        if (settings != null)
            indexCreateRequest.settings(settings);
        CreateIndexResponse siteidxResponse = nodeClient.admin().indices().create(indexCreateRequest).actionGet();
        if (!siteidxResponse.isAcknowledged()) {
            throw new SearchIndexException("Unable to create index for '" + idx + "'");
        }
    } catch (IndexAlreadyExistsException e) {
        logger.info("Detected existing index '{}'", idx);
    }
    // Store the correct mapping
    for (String type : getDocumenTypes()) {
        PutMappingRequest siteMappingRequest = new PutMappingRequest(idx);
        siteMappingRequest.source(getIndexTypeDefinition(idx, type));
        siteMappingRequest.type(type);
        PutMappingResponse siteMappingResponse = nodeClient.admin().indices().putMapping(siteMappingRequest).actionGet();
        if (!siteMappingResponse.isAcknowledged()) {
            throw new SearchIndexException("Unable to install '" + type + "' mapping for index '" + idx + "'");
        }
    }
    // See if the index version exists and check if it matches. The request will
    // fail if there is no version index
    boolean versionIndexExists = false;
    GetRequestBuilder getRequestBuilder = nodeClient.prepareGet(idx, VERSION_TYPE, ROOT_ID);
    try {
        GetResponse response = getRequestBuilder.execute().actionGet();
        if (response.isExists() && response.getField(VERSION) != null) {
            int actualIndexVersion = Integer.parseInt((String) response.getField(VERSION).getValue());
            if (indexVersion != actualIndexVersion)
                throw new SearchIndexException("Search index is at version " + actualIndexVersion + ", but codebase expects " + indexVersion);
            versionIndexExists = true;
            logger.debug("Search index version is {}", indexVersion);
        }
    } catch (ElasticsearchException e) {
        logger.debug("Version index has not been created");
    }
    // The index does not exist, let's create it
    if (!versionIndexExists) {
        logger.debug("Creating version index for site '{}'", idx);
        IndexRequestBuilder requestBuilder = nodeClient.prepareIndex(idx, VERSION_TYPE, ROOT_ID);
        logger.debug("Index version of site '{}' is {}", idx, indexVersion);
        requestBuilder = requestBuilder.setSource(VERSION, Integer.toString(indexVersion));
        requestBuilder.execute().actionGet();
    }
    preparedIndices.add(idx);
}
Also used : SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IndexAlreadyExistsException(org.elasticsearch.indices.IndexAlreadyExistsException) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder)

Example 3 with PutMappingRequest

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest in project crate by crate.

the class TransportSchemaUpdateAction method updateMapping.

private CompletableFuture<AcknowledgedResponse> updateMapping(Index index, TimeValue timeout, String mappingSource) {
    FutureActionListener<AcknowledgedResponse, AcknowledgedResponse> putMappingListener = FutureActionListener.newInstance();
    PutMappingRequest putMappingRequest = new PutMappingRequest().indices(new String[0]).setConcreteIndex(index).source(mappingSource, XContentType.JSON).timeout(timeout).masterNodeTimeout(timeout);
    nodeClient.execute(PutMappingAction.INSTANCE, putMappingRequest, putMappingListener);
    return putMappingListener;
}
Also used : PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse)

Example 4 with PutMappingRequest

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest in project sonarqube by SonarSource.

the class EsRequestDetailsTest method should_format_PutMappingRequest.

@Test
public void should_format_PutMappingRequest() {
    PutMappingRequest request = new PutMappingRequest("index-1").type("type-1");
    assertThat(EsRequestDetails.computeDetailsAsString(request)).isEqualTo("ES put mapping request on indices 'index-1' on type 'type-1'");
}
Also used : PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) Test(org.junit.Test)

Example 5 with PutMappingRequest

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest in project sonarqube by SonarSource.

the class MigrationEsClientImpl method addMappingToExistingIndex.

@Override
public void addMappingToExistingIndex(String index, String type, String mappingName, String mappingType, Map<String, String> options) {
    String[] indices = client.getIndex(new GetIndexRequest(index)).getIndices();
    if (indices != null && indices.length == 1) {
        Loggers.get(getClass()).info("Add mapping [{}] to Elasticsearch index [{}]", mappingName, index);
        String mappingOptions = Stream.concat(Stream.of(Maps.immutableEntry("type", mappingType)), options.entrySet().stream()).map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(","));
        client.putMapping(new PutMappingRequest(index).type(type).source(mappingName, mappingOptions));
        updatedIndices.add(index);
    } else {
        throw new IllegalStateException("Expected only one index to be found, actual [" + String.join(",", indices) + "]");
    }
}
Also used : Arrays(java.util.Arrays) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Loggers(org.sonar.api.utils.log.Loggers) Stream(java.util.stream.Stream) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) Map(java.util.Map) MigrationEsClient(org.sonar.server.platform.db.migration.es.MigrationEsClient) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest)

Aggregations

PutMappingRequest (org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest)10 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)3 IOException (java.io.IOException)2 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)2 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)2 Settings (org.elasticsearch.common.settings.Settings)2 Maps (com.google.common.collect.Maps)1 FutureActionListener (io.crate.action.FutureActionListener)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)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