Search in sources :

Example 71 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project graylog2-server by Graylog2.

the class MongoIndexRangeServiceTest method testCalculateRangeWithNonExistingIndex.

@Test(expected = IndexNotFoundException.class)
public void testCalculateRangeWithNonExistingIndex() throws Exception {
    when(indices.indexRangeStatsOfIndex("does-not-exist")).thenThrow(new IndexNotFoundException("does-not-exist"));
    indexRangeService.calculateRange("does-not-exist");
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Test(org.junit.Test)

Example 72 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project camel by apache.

the class ElasticsearchProducer method process.

public void process(Exchange exchange) throws Exception {
    // 2. Index and type will be set by:
    // a. If the incoming body is already an action request
    // b. If the body is not an action request we will use headers if they
    // are set.
    // c. If the body is not an action request and the headers aren't set we
    // will use the configuration.
    // No error is thrown by the component in the event none of the above
    // conditions are met. The java es client
    // will throw.
    Message message = exchange.getIn();
    final ElasticsearchOperation operation = resolveOperation(exchange);
    // Set the index/type headers on the exchange if necessary. This is used
    // for type conversion.
    boolean configIndexName = false;
    String indexName = message.getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class);
    if (indexName == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_NAME, configuration.getIndexName());
        configIndexName = true;
    }
    boolean configIndexType = false;
    String indexType = message.getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class);
    if (indexType == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, configuration.getIndexType());
        configIndexType = true;
    }
    boolean configWaitForActiveShards = false;
    Integer waitForActiveShards = message.getHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS, Integer.class);
    if (waitForActiveShards == null) {
        message.setHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS, configuration.getWaitForActiveShards());
        configWaitForActiveShards = true;
    }
    if (operation == ElasticsearchOperation.INDEX) {
        IndexRequest indexRequest = message.getBody(IndexRequest.class);
        message.setBody(client.index(indexRequest).actionGet().getId());
    } else if (operation == ElasticsearchOperation.UPDATE) {
        UpdateRequest updateRequest = message.getBody(UpdateRequest.class);
        message.setBody(client.update(updateRequest).actionGet().getId());
    } else if (operation == ElasticsearchOperation.GET_BY_ID) {
        GetRequest getRequest = message.getBody(GetRequest.class);
        message.setBody(client.get(getRequest));
    } else if (operation == ElasticsearchOperation.MULTIGET) {
        MultiGetRequest multiGetRequest = message.getBody(MultiGetRequest.class);
        message.setBody(client.multiGet(multiGetRequest));
    } else if (operation == ElasticsearchOperation.BULK) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        message.setBody(client.bulk(bulkRequest).actionGet());
    } else if (operation == ElasticsearchOperation.BULK_INDEX) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        List<String> indexedIds = new ArrayList<String>();
        for (BulkItemResponse response : client.bulk(bulkRequest).actionGet().getItems()) {
            indexedIds.add(response.getId());
        }
        message.setBody(indexedIds);
    } else if (operation == ElasticsearchOperation.DELETE) {
        DeleteRequest deleteRequest = message.getBody(DeleteRequest.class);
        message.setBody(client.delete(deleteRequest).actionGet());
    } else if (operation == ElasticsearchOperation.EXISTS) {
        // ExistsRequest API is deprecated, using SearchRequest instead with size=0 and terminate_after=1
        SearchRequest searchRequest = new SearchRequest(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class));
        try {
            client.prepareSearch(searchRequest.indices()).setSize(0).setTerminateAfter(1).get();
            message.setBody(true);
        } catch (IndexNotFoundException e) {
            message.setBody(false);
        }
    } else if (operation == ElasticsearchOperation.SEARCH) {
        SearchRequest searchRequest = message.getBody(SearchRequest.class);
        message.setBody(client.search(searchRequest).actionGet());
    } else if (operation == ElasticsearchOperation.MULTISEARCH) {
        MultiSearchRequest multiSearchRequest = message.getBody(MultiSearchRequest.class);
        message.setBody(client.multiSearch(multiSearchRequest));
    } else if (operation == ElasticsearchOperation.DELETE_INDEX) {
        DeleteIndexRequest deleteIndexRequest = message.getBody(DeleteIndexRequest.class);
        message.setBody(client.admin().indices().delete(deleteIndexRequest).actionGet());
    } else {
        throw new IllegalArgumentException(ElasticsearchConstants.PARAM_OPERATION + " value '" + operation + "' is not supported");
    }
    // subsequent endpoint index/type with the first endpoint index/type.
    if (configIndexName) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_NAME);
    }
    if (configIndexType) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_TYPE);
    }
    if (configWaitForActiveShards) {
        message.removeHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) Message(org.apache.camel.Message) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 73 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project beam by apache.

the class ElasticSearchIOTestUtils method upgradeIndexAndGetCurrentNumDocs.

/**
   * Forces an upgrade of the given index to make recently inserted documents available for search.
   *
   * @return The number of docs in the index
   */
static long upgradeIndexAndGetCurrentNumDocs(String index, String type, Client client) {
    try {
        client.admin().indices().upgrade(new UpgradeRequest(index)).actionGet();
        SearchResponse response = client.prepareSearch(index).setTypes(type).execute().actionGet(5000);
        return response.getHits().getTotalHits();
    // it is fine to ignore bellow exceptions because in testWriteWithBatchSize* sometimes,
    // we call upgrade before any doc have been written
    // (when there are fewer docs processed than batchSize).
    // In that cases index/type has not been created (created upon first doc insertion)
    } catch (IndexNotFoundException e) {
    } catch (java.lang.IllegalArgumentException e) {
        if (!e.getMessage().contains("No search type")) {
            throw e;
        }
    }
    return 0;
}
Also used : UpgradeRequest(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeRequest) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 74 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project components by Talend.

the class ElasticsearchTestUtils method upgradeIndexAndGetCurrentNumDocs.

/**
 * Force an upgrade of all indices to make recently inserted documents available for search.
 *
 * @param client Elasticsearch TCP client to use for upgrade
 * @return The number of docs in the index
 */
static long upgradeIndexAndGetCurrentNumDocs(String index, String type, Client client) {
    try {
        client.admin().indices().upgrade(new UpgradeRequest(index)).actionGet();
        SearchResponse response = client.prepareSearch(index).setTypes(type).execute().actionGet(5000);
        return response.getHits().getTotalHits();
    // it is fine to ignore bellow exceptions because in testWriteWithBatchSize* sometimes,
    // we call upgrade before any doc have been written
    // (when there are fewer docs processed than batchSize).
    // In that cases index/type has not been created (created upon first doc insertion)
    } catch (IndexNotFoundException e) {
    } catch (java.lang.IllegalArgumentException e) {
        if (!e.getMessage().contains("No search type")) {
            throw e;
        }
    }
    return 0;
}
Also used : UpgradeRequest(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeRequest) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 75 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project metron by apache.

the class ElasticsearchMetaAlertUpdateDao method update.

@Override
public Document update(Document update, Optional<String> index) throws IOException {
    if (MetaAlertConstants.METAALERT_TYPE.equals(update.getSensorType())) {
        // We've been passed an update to the meta alert.
        throw new UnsupportedOperationException("Meta alerts cannot be directly updated");
    } else {
        Map<Document, Optional<String>> updates = new HashMap<>();
        updates.put(update, index);
        try {
            // We need to update an alert itself.  Only that portion of the update can be delegated.
            // We still need to get meta alerts potentially associated with it and update.
            SearchResponse response = getMetaAlertsForAlert(update.getGuid());
            Collection<Document> metaAlerts = response.getResults().stream().map(result -> toDocument(result, update.getTimestamp())).collect(Collectors.toList());
            // Each meta alert needs to be updated with the new alert
            for (Document metaAlert : metaAlerts) {
                replaceAlertInMetaAlert(metaAlert, update);
                updates.put(metaAlert, Optional.of(METAALERTS_INDEX));
            }
        } catch (IndexNotFoundException e) {
            List<String> indicesNotFound = e.getMetadata(INDEX_NOT_FOUND_INDICES_KEY);
            // Otherwise throw the exception.
            if (indicesNotFound.size() != 1 || !METAALERTS_INDEX.equals(indicesNotFound.get(0))) {
                throw e;
            }
        }
        // Run the alert's update
        elasticsearchDao.batchUpdate(updates);
        return update;
    }
}
Also used : MetaAlertStatus(org.apache.metron.indexing.dao.metaalert.MetaAlertStatus) MetaAlertConstants(org.apache.metron.indexing.dao.metaalert.MetaAlertConstants) HashMap(java.util.HashMap) QueryBuilders.nestedQuery(org.elasticsearch.index.query.QueryBuilders.nestedQuery) AbstractLuceneMetaAlertUpdateDao(org.apache.metron.indexing.dao.metaalert.lucene.AbstractLuceneMetaAlertUpdateDao) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) SearchResult(org.apache.metron.indexing.dao.search.SearchResult) MetaAlertCreateRequest(org.apache.metron.indexing.dao.metaalert.MetaAlertCreateRequest) SearchHit(org.elasticsearch.search.SearchHit) Document(org.apache.metron.indexing.dao.update.Document) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) ElasticsearchUtils(org.apache.metron.elasticsearch.utils.ElasticsearchUtils) CommentAddRemoveRequest(org.apache.metron.indexing.dao.update.CommentAddRemoveRequest) Collection(java.util.Collection) MetaScores(org.apache.metron.indexing.dao.metaalert.MetaScores) IOException(java.io.IOException) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) ScoreMode(org.apache.lucene.search.join.ScoreMode) Constants(org.apache.metron.common.Constants) Collectors(java.util.stream.Collectors) MetaAlertRetrieveLatestDao(org.apache.metron.indexing.dao.metaalert.MetaAlertRetrieveLatestDao) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) List(java.util.List) METAALERTS_INDEX(org.apache.metron.elasticsearch.dao.ElasticsearchMetaAlertDao.METAALERTS_INDEX) InvalidCreateException(org.apache.metron.indexing.dao.search.InvalidCreateException) Optional(java.util.Optional) MetaAlertConfig(org.apache.metron.indexing.dao.metaalert.MetaAlertConfig) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) Collections(java.util.Collections) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse) Optional(java.util.Optional) HashMap(java.util.HashMap) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) List(java.util.List) Document(org.apache.metron.indexing.dao.update.Document) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse)

Aggregations

IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)92 ClusterState (org.elasticsearch.cluster.ClusterState)22 ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)21 ShardId (org.elasticsearch.index.shard.ShardId)19 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)16 Index (org.elasticsearch.index.Index)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)12 List (java.util.List)11 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)11 ClusterName (org.elasticsearch.cluster.ClusterName)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)9 Settings (org.elasticsearch.common.settings.Settings)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 HashMap (java.util.HashMap)8 Nullable (javax.annotation.Nullable)8 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)8 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)8