use of org.elasticsearch.action.index.IndexRequestBuilder in project ecs-dashboard by carone1.
the class ElasticBillingDAO method insert.
/**
* {@inheritDoc}
*/
@Override
public void insert(NamespaceBillingInfo billingData, Date collectionTime) {
// Generate JSON for namespace billing info
XContentBuilder namespaceBuilder = toJsonFormat(billingData, collectionTime);
elasticClient.prepareIndex(billingNamespaceIndexDayName, BILLING_NAMESPACE_INDEX_TYPE).setSource(namespaceBuilder).get();
if (billingData.getBucketBillingInfo() == null || billingData.getBucketBillingInfo().isEmpty()) {
// nothing to insert
return;
}
BulkRequestBuilder requestBuilder = elasticClient.prepareBulk();
// Generate JSON for namespace billing info
for (BucketBillingInfo bucketBillingInfo : billingData.getBucketBillingInfo()) {
XContentBuilder bucketBuilder = toJsonFormat(bucketBillingInfo, collectionTime);
IndexRequestBuilder request = elasticClient.prepareIndex().setIndex(billingBucketIndexDayName).setType(BILLING_BUCKET_INDEX_TYPE).setSource(bucketBuilder);
requestBuilder.add(request);
}
BulkResponse bulkResponse = requestBuilder.execute().actionGet();
int items = bulkResponse.getItems().length;
LOGGER.info("Took " + bulkResponse.getTookInMillis() + " ms to index [" + items + "] items in Elasticsearch" + "index: " + billingNamespaceIndexDayName + " index type: " + BILLING_BUCKET_INDEX_TYPE);
if (bulkResponse.hasFailures()) {
LOGGER.error("Failures occured while items in Elasticsearch " + "index: " + billingNamespaceIndexDayName + " index type: " + BILLING_BUCKET_INDEX_TYPE);
}
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project ecs-dashboard by carone1.
the class ElasticBillingDAO method insert.
/**
* {@inheritDoc}
*/
@Override
public void insert(ObjectBuckets objectBuckets, Date collectionTime) {
if (objectBuckets == null || objectBuckets.getObjectBucket() == null || objectBuckets.getObjectBucket().isEmpty()) {
// nothing to insert
return;
}
BulkRequestBuilder requestBuilder = elasticClient.prepareBulk();
// Generate JSON for object buckets info
for (ObjectBucket objectBucket : objectBuckets.getObjectBucket()) {
XContentBuilder objectBucketBuilder = toJsonFormat(objectBucket, collectionTime);
IndexRequestBuilder request = elasticClient.prepareIndex().setIndex(objectBucketIndexDayName).setType(OBJECT_BUCKET_INDEX_TYPE).setSource(objectBucketBuilder);
requestBuilder.add(request);
}
BulkResponse bulkResponse = requestBuilder.execute().actionGet();
int items = bulkResponse.getItems().length;
LOGGER.info("Took " + bulkResponse.getTookInMillis() + " ms to index [" + items + "] items in ElasticSearch" + "index: " + objectBucketIndexDayName + " index type: " + OBJECT_BUCKET_INDEX_TYPE);
if (bulkResponse.hasFailures()) {
LOGGER.error("Failures occured while items in ElasticSearch " + "index: " + objectBucketIndexDayName + " index type: " + OBJECT_BUCKET_INDEX_TYPE);
}
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project ecs-dashboard by carone1.
the class ElasticS3ObjectDAO method insert.
/**
* {@inheritDoc}
*/
@Override
public void insert(ListObjectsResult listObjectsResult, String namespace, String bucket, Date collectionTime) {
if (listObjectsResult == null || listObjectsResult.getObjects() == null || listObjectsResult.getObjects().isEmpty()) {
// nothing to insert
return;
}
BulkRequestBuilder requestBuilder = elasticClient.prepareBulk();
// Generate JSON for object buckets info
for (S3Object s3Object : listObjectsResult.getObjects()) {
XContentBuilder s3ObjectBuilder = toJsonFormat(s3Object, namespace, bucket, collectionTime);
IndexRequestBuilder request = elasticClient.prepareIndex().setIndex(s3ObjectIndexDayName).setType(S3_OBJECT_INDEX_TYPE).setSource(s3ObjectBuilder);
requestBuilder.add(request);
}
BulkResponse bulkResponse = requestBuilder.execute().actionGet();
int items = bulkResponse.getItems().length;
LOGGER.info("Took " + bulkResponse.getTookInMillis() + " ms to index [" + items + "] items in Elasticsearch " + "index: " + s3ObjectIndexDayName + " index type: " + S3_OBJECT_INDEX_TYPE);
if (bulkResponse.hasFailures()) {
LOGGER.error("Failure(s) occured while items in Elasticsearch " + "index: " + s3ObjectIndexDayName + " index type: " + S3_OBJECT_INDEX_TYPE);
}
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method indexDoc.
private IndexResponse indexDoc(final EsDocInfo di) throws Throwable {
// batchCurSize++;
final IndexRequestBuilder req = getClient().prepareIndex(targetIndex, di.getType(), di.getId());
req.setSource(di.getSource());
if (di.getVersion() != 0) {
req.setVersion(di.getVersion()).setVersionType(VersionType.EXTERNAL);
}
if (debug) {
debug("Indexing to index " + targetIndex + " with DocInfo " + di);
}
return req.execute().actionGet();
}
use of org.elasticsearch.action.index.IndexRequestBuilder 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);
}
Aggregations