use of org.elasticsearch.ElasticsearchException in project elasticsearch-indexing-proxy by codelibs.
the class RequestUtils method createBulkRequest.
public static BulkRequestBuilder createBulkRequest(final Client client, final StreamInput streamInput, final String index) throws IOException {
final BulkRequestBuilder builder = client.prepareBulk();
final BulkRequest request = builder.request();
request.readFrom(streamInput);
if (index != null) {
request.requests().stream().forEach(req -> {
if (req instanceof DeleteRequest) {
((DeleteRequest) req).index(index);
} else if (req instanceof DeleteByQueryRequest) {
((DeleteByQueryRequest) req).indices(index);
} else if (req instanceof IndexRequest) {
((IndexRequest) req).index(index);
} else if (req instanceof UpdateRequest) {
((UpdateRequest) req).index(index);
} else if (req instanceof UpdateByQueryRequest) {
((UpdateByQueryRequest) req).indices(index);
} else {
throw new ElasticsearchException("Unsupported request in bulk: " + req);
}
});
}
return builder;
}
use of org.elasticsearch.ElasticsearchException 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.ElasticsearchException in project arctic-sea by 52North.
the class ElasticsearchAdminHandler method addUuidToMetadataIfNeeded.
@SuppressWarnings("unchecked")
private void addUuidToMetadataIfNeeded(String uuid) throws ElasticsearchException {
GetResponse resp = client.prepareGet(settings.getIndexId(), MetadataDataMapping.METADATA_TYPE_NAME, MetadataDataMapping.METADATA_ROW_ID).setOperationThreaded(false).get();
Object retValues = resp.getSourceAsMap().get(MetadataDataMapping.METADATA_UUIDS_FIELD.getName());
List<String> values;
if (retValues instanceof String) {
values = new LinkedList<>();
values.add((String) retValues);
} else if (retValues instanceof List<?>) {
values = (List<String>) retValues;
} else {
throw new ConfigurationError("Invalid %s field type %s should have String or java.util.Collection<String>", MetadataDataMapping.METADATA_UUIDS_FIELD, retValues.getClass());
}
// add new uuid if needed
if (!values.stream().anyMatch(m -> m.equals(uuid))) {
Map<String, Object> uuids = new HashMap<>();
values.add(uuid);
uuids.put(MetadataDataMapping.METADATA_UUIDS_FIELD.getName(), values);
uuids.put(MetadataDataMapping.METADATA_UPDATE_TIME_FIELD.getName(), Calendar.getInstance(DateTimeZone.UTC.toTimeZone()));
client.prepareUpdate(settings.getIndexId(), MetadataDataMapping.METADATA_TYPE_NAME, "1").setDoc(uuids).get();
logger.info("UUID {} is added to the {} type", uuid, MetadataDataMapping.METADATA_TYPE_NAME);
}
}
use of org.elasticsearch.ElasticsearchException in project core-ng-project by neowu.
the class ElasticSearchImpl method indices.
@Override
public List<ElasticSearchIndex> indices() {
StopWatch watch = new StopWatch();
try {
AdminClient adminClient = client().admin();
ClusterStateResponse response = adminClient.cluster().state(new ClusterStateRequest().clear().metaData(true)).actionGet();
ImmutableOpenMap<String, IndexMetaData> indices = response.getState().getMetaData().indices();
List<ElasticSearchIndex> results = new ArrayList<>(indices.size());
for (ObjectObjectCursor<String, IndexMetaData> cursor : indices) {
IndexMetaData metaData = cursor.value;
ElasticSearchIndex index = new ElasticSearchIndex();
index.index = metaData.getIndex().getName();
index.state = metaData.getState();
results.add(index);
}
return results;
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
logger.info("indices, elapsedTime={}", watch.elapsedTime());
}
}
use of org.elasticsearch.ElasticsearchException in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method search.
@Override
public SearchResponse<T> search(SearchRequest request) {
validate(request);
StopWatch watch = new StopWatch();
long esTookTime = 0;
String index = request.index == null ? this.index : request.index;
long hits = 0;
try {
SearchRequestBuilder builder = client().prepareSearch(index).setQuery(request.query);
if (request.type != null)
builder.setSearchType(request.type);
request.aggregations.forEach(builder::addAggregation);
request.sorts.forEach(builder::addSort);
if (request.skip != null)
builder.setFrom(request.skip);
if (request.limit != null)
builder.setSize(request.limit);
logger.debug("search, index={}, type={}, request={}", index, type, builder);
org.elasticsearch.action.search.SearchResponse searchResponse = builder.get();
hits = searchResponse.getHits().getTotalHits();
esTookTime = searchResponse.getTook().nanos();
if (searchResponse.getFailedShards() > 0)
logger.warn("some shard failed, response={}", searchResponse);
return searchResponse(searchResponse);
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("elasticsearch", elapsedTime, (int) hits, 0);
logger.debug("search, hits={}, esTookTime={}, elapsedTime={}", hits, esTookTime, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
Aggregations