Search code examples
javaelasticsearchelasticsearch-jest

Jest client/ ElasticSearch cluster informations


I'm using jest client in a java application to connect to an ElasticSearch cluster and now i want to find informations on how to get cluster informations like this with jest api:

{
"name" : "",
"cluster_name" : "",
"version" : {
"number" : "2.3.2",
"build_hash" : "",
"build_timestamp" : "",
"build_snapshot" : ,
"lucene_version" : "
},
"tagline" : "You Know, for Search"
}

Solution

  • I've spent a little time in reading jest source code and get a result. Please look at the code below:

    Jest.java

    import io.searchbox.client.JestClient;
    import io.searchbox.client.JestClientFactory;
    import io.searchbox.client.JestResult;
    import io.searchbox.client.config.HttpClientConfig;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Vladislav Kislyi <vladislav.kisliy@gmail.com>
     */
    public class Jest {
    
        public static void main(String[] args) throws IOException {
            // Construct a new Jest client according to configuration via factory
            JestClientFactory factory = new JestClientFactory();
            factory.setHttpClientConfig(new HttpClientConfig.Builder("http://localhost:9200")
                    .multiThreaded(true)
                    .build());
            JestClient client = factory.getObject();
    
            GetStartPage getStartPage = new GetStartPage.Builder().build();
            try {
                JestResult execute = client.execute(getStartPage);
                System.out.println("result =" + execute.getJsonString());
            } catch (IOException ex) {
                Logger.getLogger(Jest.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        }
    }
    

    and GetStartPage.java

    import io.searchbox.action.AbstractAction;
    import io.searchbox.action.GenericResultAbstractAction;
    
    public class GetStartPage extends GenericResultAbstractAction {
    
        protected GetStartPage(Builder builder) {
            super(builder);
            setURI(buildURI());
        }
    
        protected String buildURI() {
            return super.buildURI() + "/?pretty";
        }
    
        @Override
        public String getRestMethodName() {
            return "GET";
        }
    
        public static class Builder extends AbstractAction.Builder<GetStartPage, Builder> {
    
            @Override
            public GetStartPage build() {
                return new GetStartPage(this);
            }
        }
    }
    

    You get in console exactly what you wanted