nmanandhan
nmanandhan

Reputation: 320

Spring data elasticsearch - query

I'm new to elasticsearch, trying to retrieve indexed data from elasticsearch by using query,date histogram,facets. I have elasticsearch and kibana running properly on server. Now I want to pull the specific indexed data out of elasticsearch and plot it as graphs in another home grown application(Spring web application). So thought of using spring data elasticsearch but found sample applications using elasticsearch repositories over internet.

https://github.com/BioMedCentralLtd/spring-data-elasticsearch-sample-application

Please assist me the way to just pull the data out of elasticsearch using spring data elasticsearch or if there any other better way to do this. (I don't want to use the objects/repositories as in sample, just need to get the data as JSON string).

Upvotes: 0

Views: 1889

Answers (1)

nmanandhan
nmanandhan

Reputation: 320

Finally I have used plain Elasticseach java client to work with. The below code may be useful.

<bean id="esConnection" class="com.es.connection.ESConnection" scope="singleton" autowire="byName">
    <property name="host" value="${es.host}" />
    <property name="port" value="${es.port}" />
    <property name="clusterName" value="${es.cluster}" />
</bean>

import javax.annotation.PostConstruct;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class ESConnection {
    TransportClient client;
    private String host;
    private int port;
    private String clusterName;

    public ESConnection() {

    }

    public ESConnection(String host,int port,String clusterName) {
        this.host = host;
        this.clusterName = clusterName;
        this.port = port;

    }

    @PostConstruct
    public void connect() {
        Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name",clusterName)
                .build();
        client = new TransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(host,port));          
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    public Client getClient() {
        return (Client) client;
    }

    public void close() {
        if (client != null) {
            client.close();
        }
    }

    @Override
    public String toString() {
        return String.format("%s, Host: %s, Port: %s, Cluster: %s", super.toString(), host, port, clusterName);
    }

}

In Start up listener,

public class StartupListener implements ServletContextListener {

    @Autowired
    ESConnection esConnection;

    public void contextInitialized(ServletContextEvent sce) {
        try {
            ServletContext context = sce.getServletContext();
            context.setAttribute("esConnection", esConnection);
        } catch (SchedulerException se) {
        } catch (Exception e) {
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        if (this.esConnection != null) {
            this.esConnection.close();
            context.removeAttribute("esConnection");
        }
    } 
}

Upvotes: 0

Related Questions