use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.
the class UpdateMappingIntegrationIT method testUpdateMappingConcurrently.
public void testUpdateMappingConcurrently() throws Throwable {
createIndex("test1", "test2");
final AtomicReference<Exception> threadException = new AtomicReference<>();
final AtomicBoolean stop = new AtomicBoolean(false);
Thread[] threads = new Thread[3];
final CyclicBarrier barrier = new CyclicBarrier(threads.length);
final ArrayList<Client> clientArray = new ArrayList<>();
for (Client c : clients()) {
clientArray.add(c);
}
for (int j = 0; j < threads.length; j++) {
threads[j] = new Thread(new Runnable() {
@SuppressWarnings("unchecked")
@Override
public void run() {
try {
barrier.await();
for (int i = 0; i < 100; i++) {
if (stop.get()) {
return;
}
Client client1 = clientArray.get(i % clientArray.size());
Client client2 = clientArray.get((i + 1) % clientArray.size());
String indexName = i % 2 == 0 ? "test2" : "test1";
String typeName = "type" + (i % 10);
String fieldName = Thread.currentThread().getName() + "_" + i;
PutMappingResponse response = client1.admin().indices().preparePutMapping(indexName).setType(typeName).setSource(JsonXContent.contentBuilder().startObject().startObject(typeName).startObject("properties").startObject(fieldName).field("type", "text").endObject().endObject().endObject().endObject()).get();
assertThat(response.isAcknowledged(), equalTo(true));
GetMappingsResponse getMappingResponse = client2.admin().indices().prepareGetMappings(indexName).get();
ImmutableOpenMap<String, MappingMetaData> mappings = getMappingResponse.getMappings().get(indexName);
assertThat(mappings.containsKey(typeName), equalTo(true));
assertThat(((Map<String, Object>) mappings.get(typeName).getSourceAsMap().get("properties")).keySet(), Matchers.hasItem(fieldName));
}
} catch (Exception e) {
threadException.set(e);
stop.set(true);
}
}
});
threads[j].setName("t_" + j);
threads[j].start();
}
for (Thread t : threads) t.join();
if (threadException.get() != null) {
throw threadException.get();
}
}
use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.
the class TaskResultsService method storeResult.
public void storeResult(TaskResult taskResult, ActionListener<Void> listener) {
ClusterState state = clusterService.state();
if (state.routingTable().hasIndex(TASK_INDEX) == false) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest();
createIndexRequest.settings(taskResultIndexSettings());
createIndexRequest.index(TASK_INDEX);
createIndexRequest.mapping(TASK_TYPE, taskResultIndexMapping(), XContentType.JSON);
createIndexRequest.cause("auto(task api)");
createIndexAction.execute(null, createIndexRequest, new ActionListener<CreateIndexResponse>() {
@Override
public void onResponse(CreateIndexResponse result) {
doStoreResult(taskResult, listener);
}
@Override
public void onFailure(Exception e) {
if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) {
// we have the index, do it
try {
doStoreResult(taskResult, listener);
} catch (Exception inner) {
inner.addSuppressed(e);
listener.onFailure(inner);
}
} else {
listener.onFailure(e);
}
}
});
} else {
IndexMetaData metaData = state.getMetaData().index(TASK_INDEX);
if (metaData.getMappings().containsKey(TASK_TYPE) == false) {
// The index already exists but doesn't have our mapping
client.admin().indices().preparePutMapping(TASK_INDEX).setType(TASK_TYPE).setSource(taskResultIndexMapping(), XContentType.JSON).execute(new ActionListener<PutMappingResponse>() {
@Override
public void onResponse(PutMappingResponse putMappingResponse) {
doStoreResult(taskResult, listener);
}
@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
} else {
doStoreResult(taskResult, listener);
}
}
}
use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project sonarqube by SonarSource.
the class IndexCreator method createIndex.
private void createIndex(IndexDefinitions.Index index) {
LOGGER.info(String.format("Create index %s", index.getName()));
Settings.Builder settings = Settings.builder();
settings.put(index.getSettings());
settings.put(SETTING_HASH, new IndexDefinitionHash().of(index));
CreateIndexResponse indexResponse = client.prepareCreate(index.getName()).setSettings(settings).get();
if (!indexResponse.isAcknowledged()) {
throw new IllegalStateException("Failed to create index " + index.getName());
}
client.waitForStatus(ClusterHealthStatus.YELLOW);
// create types
for (Map.Entry<String, IndexDefinitions.IndexType> entry : index.getTypes().entrySet()) {
LOGGER.info(String.format("Create type %s/%s", index.getName(), entry.getKey()));
PutMappingResponse mappingResponse = client.preparePutMapping(index.getName()).setType(entry.getKey()).setSource(entry.getValue().getAttributes()).get();
if (!mappingResponse.isAcknowledged()) {
throw new IllegalStateException("Failed to create type " + entry.getKey());
}
}
client.waitForStatus(ClusterHealthStatus.YELLOW);
}
use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project titan by thinkaurelius.
the class ElasticSearchIndex method register.
@Override
public void register(String store, String key, KeyInformation information, BaseTransaction tx) throws BackendException {
XContentBuilder mapping;
Class<?> dataType = information.getDataType();
Mapping map = Mapping.getMapping(information);
Preconditions.checkArgument(map == Mapping.DEFAULT || AttributeUtil.isString(dataType), "Specified illegal mapping [%s] for data type [%s]", map, dataType);
try {
mapping = XContentFactory.jsonBuilder().startObject().startObject(store).field(TTL_FIELD, new HashMap<String, Object>() {
{
put("enabled", true);
}
}).startObject("properties").startObject(key);
if (AttributeUtil.isString(dataType)) {
if (map == Mapping.DEFAULT)
map = Mapping.TEXT;
log.debug("Registering string type for {} with mapping {}", key, map);
mapping.field("type", "string");
switch(map) {
case STRING:
mapping.field("index", "not_analyzed");
break;
case TEXT:
//default, do nothing
break;
case TEXTSTRING:
mapping.endObject();
//add string mapping
mapping.startObject(getDualMappingName(key));
mapping.field("type", "string");
mapping.field("index", "not_analyzed");
break;
default:
throw new AssertionError("Unexpected mapping: " + map);
}
} else if (dataType == Float.class) {
log.debug("Registering float type for {}", key);
mapping.field("type", "float");
} else if (dataType == Double.class) {
log.debug("Registering double type for {}", key);
mapping.field("type", "double");
} else if (dataType == Byte.class) {
log.debug("Registering byte type for {}", key);
mapping.field("type", "byte");
} else if (dataType == Short.class) {
log.debug("Registering short type for {}", key);
mapping.field("type", "short");
} else if (dataType == Integer.class) {
log.debug("Registering integer type for {}", key);
mapping.field("type", "integer");
} else if (dataType == Long.class) {
log.debug("Registering long type for {}", key);
mapping.field("type", "long");
} else if (dataType == Boolean.class) {
log.debug("Registering boolean type for {}", key);
mapping.field("type", "boolean");
} else if (dataType == Geoshape.class) {
log.debug("Registering geo_point type for {}", key);
mapping.field("type", "geo_point");
} else if (dataType == Date.class || dataType == Instant.class) {
log.debug("Registering date type for {}", key);
mapping.field("type", "date");
} else if (dataType == Boolean.class) {
log.debug("Registering boolean type for {}", key);
mapping.field("type", "boolean");
} else if (dataType == UUID.class) {
log.debug("Registering uuid type for {}", key);
mapping.field("type", "string");
mapping.field("index", "not_analyzed");
}
mapping.endObject().endObject().endObject().endObject();
} catch (IOException e) {
throw new PermanentBackendException("Could not render json for put mapping request", e);
}
try {
PutMappingResponse response = client.admin().indices().preparePutMapping(indexName).setIgnoreConflicts(false).setType(store).setSource(mapping).execute().actionGet();
} catch (Exception e) {
throw convert(e);
}
}
use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project fess by codelibs.
the class AdminUpgradeAction method addFieldMapping.
private void addFieldMapping(final IndicesAdminClient indicesClient, final String index, final String type, final String field, final String source) {
final GetFieldMappingsResponse gfmResponse = indicesClient.prepareGetFieldMappings(index).addTypes(type).setFields(field).execute().actionGet();
final FieldMappingMetaData fieldMappings = gfmResponse.fieldMappings(index, type, field);
if (fieldMappings == null || fieldMappings.isNull()) {
try {
final PutMappingResponse pmResponse = indicesClient.preparePutMapping(index).setType(type).setSource(source).execute().actionGet();
if (!pmResponse.isAcknowledged()) {
logger.warn("Failed to add " + field + " to " + index + "/" + type);
}
} catch (final Exception e) {
logger.warn("Failed to add " + field + " to " + index + "/" + type, e);
}
}
}
Aggregations