简体   繁体   English

Spring数据ElasticSearch-查询

[英]Spring data elasticsearch - query

I'm new to elasticsearch, trying to retrieve indexed data from elasticsearch by using query,date histogram,facets. 我是Elasticsearch的新手,试图通过使用查询,日期直方图,构面从Elasticsearch检索索引数据。 I have elasticsearch and kibana running properly on server. 我在服务器上正常运行了elasticsearch和kibana。 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). 现在,我想从elasticsearch中提取特定的索引数据,并将其作为图绘制在另一个本地开发的应用程序(Spring Web应用程序)中。 So thought of using spring data elasticsearch but found sample applications using elasticsearch repositories over internet. 因此,想到使用spring数据elasticsearch,但在Internet上找到了使用Elasticsearch存储库的示例应用程序。

https://github.com/BioMedCentralLtd/spring-data-elasticsearch-sample-application 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. 请协助我使用spring数据elasticsearch从弹性搜索中提取数据的方法,或者是否有其他更好的方法。 (I don't want to use the objects/repositories as in sample, just need to get the data as JSON string). (我不想像示例中那样使用对象/存储库,只需要将数据作为JSON字符串获取)。

Finally I have used plain Elasticseach java client to work with. 最终,我使用了普通的Elasticseach Java客户端进行工作。 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");
        }
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM