0

我是弹性搜索的新手,试图通过使用查询、日期直方图、刻面从弹性搜索中检索索引数据。我有弹性搜索和 kibana 在服务器上正常运行。现在我想从 elasticsearch 中提取特定的索引数据,并将其绘制为另一个本地应用程序(Spring Web 应用程序)中的图表。因此想到使用 spring data elasticsearch,但在 Internet 上找到了使用 elasticsearch 存储库的示例应用程序。

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

请帮助我使用spring data elasticsearch将数据从elasticsearch中提取出来,或者如果有其他更好的方法可以做到这一点。(我不想像示例中那样使用对象/存储库,只需要将数据作为 JSON 字符串获取)。

4

1 回答 1

0

最后,我使用了普通的 Elasticseach java 客户端。下面的代码可能有用。

<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);
    }

}

在启动监听器中,

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");
        }
    } 
}
于 2014-10-02T20:28:09.957 回答