Skip to content

Instantly share code, notes, and snippets.

@ferhatsb
Last active December 16, 2015 00:29
Show Gist options
  • Select an option

  • Save ferhatsb/5348186 to your computer and use it in GitHub Desktop.

Select an option

Save ferhatsb/5348186 to your computer and use it in GitHub Desktop.
jest singleton
public class JestTest {

    private static JestHttpClient jestHttpClient;

    public JestHttpClient getJestHttpClient() {

        if (jestHttpClient == null) {
            ClientConfig clientConfig = new ClientConfig();
            LinkedHashSet<String> servers = new LinkedHashSet<String>();
            servers.add("http://localhost:9200");
            clientConfig.getProperties().put(ClientConstants.SERVER_LIST, servers);
            clientConfig.getProperties().put(ClientConstants.IS_MULTI_THREADED, true);

            factory.setClientConfig(clientConfig);
            jestHttpClient = (JestHttpClient) factory.getObject();
            return jestHttpClient;

        } else {
            return jestHttpClient;
        }
    }

    public void bulkIndex(List<Document> documents) {

        Bulk bulk = new Bulk();
        for (Document document : documents) {
            bulk.addIndex(new Index.Builder(document).index(getIndex().toLowerCase()).type(getType()).id((String) document.get("id")).build());
        }
        JestResult result = getJestHttpClient().execute(bulk);
        if (!result.isSucceeded()) {
            logger.error("IndexDocumentCommand::the bulk operation to index documents failed with message" + result.getErrorMessage());
        } else {
            logger.error("IndexDocumentCommand::the bulk operation to index documents succeeded with message" + result.getJsonString());
        }
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment