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);
}
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);
}
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;
}
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'");
}
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) + "]");
}
}
Aggregations