io.searchbox.action.Action Java Examples

The following examples show how to use io.searchbox.action.Action. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractClientFactory.java    From apiman with Apache License 2.0 7 votes vote down vote up
/**
 * Called to initialize the storage.
 * @param client the jest client
 * @param indexName the name of the ES index to initialize
 * @param defaultIndexName the default ES index - used to determine which -settings.json file to use
 */
protected void initializeClient(JestClient client, String indexName, String defaultIndexName) {
    try {
        client.execute(new Health.Builder().build());
        Action<JestResult> action = new IndicesExists.Builder(indexName).build();
        // There was occasions where a race occurred here when multiple threads try to
        // create the index simultaneously. This caused a non-fatal, but annoying, exception.
        synchronized(AbstractClientFactory.class) {
            JestResult result = client.execute(action);
            if (!result.isSucceeded()) {
                createIndex(client, indexName, defaultIndexName + "-settings.json"); //$NON-NLS-1$
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: JestClientHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Method to use the JEST client to execute an elastic action
 *
 * @param <T> The expected class of the action
 * @param action action
 *
 * @return a jest action result
 */
@Retryable(maxAttempts = 3, value = RestClientException.class, backoff = @Backoff(delay = 5000, multiplier = 2))
public <T extends JestResult> T execute(Action<T> action)
{
    T actionResult = null;
    try
    {
        actionResult = jestClientFactory.getJestClient().execute(action);

        // log the error if the action failed but no exception is thrown
        if (actionResult == null || !actionResult.isSucceeded())
        {
            LOGGER.error("Failed to execute JEST client action. action={}, actionResult={}", action, actionResult);
        }
    }
    catch (final IOException ioException)
    {
        LOGGER.error("Failed to execute JEST client action.", ioException);
        //throw a runtime exception so that the client needs not catch
        throw new RestClientException(ioException.getMessage()); //NOPMD
    }

    return actionResult;
}
 
Example #3
Source File: BufferedJestHttpClient.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends JestResult> void executeAsync(Action<T> clientRequest, JestResultHandler<? super T> resultHandler) {
    HttpUriRequest request;
    try {
        request = prepareRequest((BufferedBulk)clientRequest);
    } catch (IOException e) {
        resultHandler.failed(e);
        return;
    }

    getAsyncClient().execute(request, responseHandler(clientRequest, resultHandler, request));

}
 
Example #4
Source File: JestClient.java    From wES with MIT License 5 votes vote down vote up
private <T extends JestResult> T _exec(Action<T> action) throws Exception {
	T result = client.execute(action);
	if (result != null && result.isSucceeded() && result.getErrorMessage() == null) {
		return result;
	}
	if (result != null && result.getErrorMessage() != null) {
		throw new Exception(result.getErrorMessage());
	}
	return null;
}
 
Example #5
Source File: IndexFunctionsDaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean isIndexExists(String indexName)
{
    Action action = new IndicesExists.Builder(indexName).build();
    JestResult result = jestClientHelper.execute(action);

    return result.isSucceeded();
}
 
Example #6
Source File: IndexFunctionsDaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * The delete index function will take as an argument the index name and will delete the index.
 */
@Override
public final void deleteIndex(String indexName)
{
    Action action = new DeleteIndex.Builder(indexName).build();

    LOGGER.info("Deleting Elasticsearch index, indexName={}.", indexName);
    JestResult result = jestClientHelper.execute(action);

    LOGGER.info("Deleting Elasticsearch index, indexName={}. result successful is {} ", indexName, result.isSucceeded());
}
 
Example #7
Source File: IndexFunctionsDaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * The delete document by id function will delete a document in the index by the document id.
 */
@Override
public final void deleteDocumentById(String indexName, String documentType, String id)
{
    LOGGER.info("Deleting Elasticsearch document from index, indexName={}, documentType={}, id={}.", indexName, documentType, id);

    Action action = new Delete.Builder(id).index(indexName).type(documentType).build();

    JestResult result = jestClientHelper.execute(action);

    LOGGER.info("Deleting Elasticsearch document from index, indexName={}, documentType={}, id={} is successfully {}. ", indexName, documentType, id,
        result.isSucceeded());
}
 
Example #8
Source File: IndexFunctionsDaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public DocsStats getIndexStats(String indexName)
{
    Action getStats = new Stats.Builder().addIndex(indexName).build();
    JestResult jestResult = jestClientHelper.execute(getStats);
    Assert.isTrue(jestResult.isSucceeded(), jestResult.getErrorMessage());
    JsonObject statsJson = jestResult.getJsonObject().getAsJsonObject("indices").getAsJsonObject(indexName).getAsJsonObject("primaries");
    JsonObject docsJson = statsJson.getAsJsonObject("docs");
    return new DocsStats(docsJson.get("count").getAsLong(), docsJson.get("deleted").getAsLong());
}
 
Example #9
Source File: HttpESMetricsDispatcher.java    From gradle-metrics-plugin with Apache License 2.0 5 votes vote down vote up
private <T extends JestResult> T execute(Action<T> clientRequest, boolean allowNotFound) {
    try {
        T result = client.execute(clientRequest);
        if (!result.isSucceeded() && (!allowNotFound || result.getResponseCode() != 404)) {
            throw new RuntimeException("Jest request failed with " + result.getErrorMessage());
        }
        return result;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example #10
Source File: BufferedJestHttpClient.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private <T extends JestResult> BufferedResultCallback<T> responseHandler(Action<T> clientRequest, JestResultHandler<? super T> resultHandler, HttpUriRequest request) {
    return new BufferedResultCallback(clientRequest, resultHandler);
}
 
Example #11
Source File: BufferedJestHttpClient.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public BufferedResultCallback(Action<T> clientRequest, JestResultHandler<T> request) {
    this.clientRequest = clientRequest;
    this.resultHandler = request;
}