简体   繁体   中英

Elastic Search: Transport Client to High Level Rest Client

I am trying to establish a connection to my local Elastic Search Instance.

Previously, I used transport client to create

  1. Controller.
  2. Config
  3. Loader(using @PostConstruct)
  4. Repository(Extends ElasticSearchRepositores)
  5. Entity(@Document annotation)

I tried to achieve the same functionality using HighLevel Rest Client.

Following are my components:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.abd.def.hig.monitor.repo")
public class ElasticsearchConfig {

    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() {
        RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost",9200,"http")));
         // System.out.println("client is" + client.indices().get());
        return client;

      }
}
public interface ElasticSearchRepo extends ElasticsearchRepository<Store,Long> {

}
package com.fg.pos.tpdjavaagent.monitor.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import com.fg.pos.tpdjavaagent.monitor.model.ApplicationStats;

@Document(indexName = "storedtls", type = "storedtls")
public class Store {

    @Id
    String siteCode;

    String siteName;

    String formatCode;

    String zone;

    String posType;

    String ipAddress;

    String tpAdmin;

    String applicationVersion;

    String osType;

    String ISPType;

    public Store(String siteCode, String siteName, String formatCode, String zone, String posType, String ipAddress,
            String tpAdmin, String applicationVersion, String osType, String iSPType,
            ApplicationStats applicationStats) {
        super();
        this.siteCode = siteCode;
        this.siteName = siteName;
        this.formatCode = formatCode;
        this.zone = zone;
        this.posType = posType;
        this.ipAddress = ipAddress;
        this.tpAdmin = tpAdmin;
        this.applicationVersion = applicationVersion;
        this.osType = osType;
        ISPType = iSPType;
        this.applicationStats = applicationStats;
    }

    ApplicationStats applicationStats;

    public String getSiteCode() {
        return siteCode;
    }

    public void setSiteCode(String siteCode) {
        this.siteCode = siteCode;
    }

    public String getSiteName() {
        return siteName;
    }

    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    public String getFormatCode() {
        return formatCode;
    }

    public void setFormatCode(String formatCode) {
        this.formatCode = formatCode;
    }

    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
    }

    public String getPosType() {
        return posType;
    }

    public void setPosType(String posType) {
        this.posType = posType;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public String getTpAdmin() {
        return tpAdmin;
    }

    public void setTpAdmin(String tpAdmin) {
        this.tpAdmin = tpAdmin;
    }

    public String getApplicationVersion() {
        return applicationVersion;
    }

    public void setApplicationVersion(String applicationVersion) {
        this.applicationVersion = applicationVersion;
    }

    public String getOsType() {
        return osType;
    }

    public void setOsType(String osType) {
        this.osType = osType;
    }

    public String getISPType() {
        return ISPType;
    }

    public void setISPType(String iSPType) {
        ISPType = iSPType;
    }

    public ApplicationStats getApplicationStats() {
        return applicationStats;
    }

    public void setApplicationStats(ApplicationStats applicationStats) {
        this.applicationStats = applicationStats;
    }


}

When I remove @EnableElasticsearchRepositories from the config file, everything works fine. However, when I add the same, I get the error stated below:

Consider defining a bean named 'elasticsearchTemplate' in your configuration.

One way around to resolve this issue when I used transport client was to simply add:

@Bean
    ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }

Inside my configurations. However, the client method shows error when High Level Client is used because it is of Client type.

Please let me know if anything is possible to fix this.

Thanks in advance.

New Spring Data ElasticSearch Upgraded their method. So instead of:

@Bean
    ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }

It is supposed to be:

@Bean
    ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchRestTemplate(client());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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