Search in sources :

Example 96 with IndexRequestBuilder

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);
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BucketBillingInfo(com.emc.ecs.management.entity.BucketBillingInfo) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 97 with IndexRequestBuilder

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);
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ObjectBucket(com.emc.ecs.management.entity.ObjectBucket) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 98 with IndexRequestBuilder

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);
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) S3Object(com.emc.object.s3.bean.S3Object) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 99 with IndexRequestBuilder

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();
}
Also used : DeleteIndexRequestBuilder(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder)

Example 100 with IndexRequestBuilder

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);
}
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)

Aggregations

IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)227 ArrayList (java.util.ArrayList)134 SearchResponse (org.elasticsearch.action.search.SearchResponse)125 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)48 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)45 Matchers.containsString (org.hamcrest.Matchers.containsString)38 GeoPoint (org.elasticsearch.common.geo.GeoPoint)36 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)31 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)23 Settings (org.elasticsearch.common.settings.Settings)21 IOException (java.io.IOException)19 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)19 Client (org.elasticsearch.client.Client)18 SearchHit (org.elasticsearch.search.SearchHit)17 LinkedHashMap (java.util.LinkedHashMap)16 SearchHits (org.elasticsearch.search.SearchHits)16 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)15 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)15 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)15 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)15