弹性搜索:将客户端传输到高级其余客户端

时间:2019-02-20 09:06:40

标签: java spring-boot spring-mvc elasticsearch elastic-stack

我正在尝试建立与本地弹性搜索实例的连接。

以前,我使用传输客户端创建

  1. 控制器。
  2. 配置
  3. 加载程序(使用@PostConstruct)
  4. 存储库(扩展了ElasticSearchRepositores)
  5. 实体(@Document注释)

我尝试使用HighLevel Rest Client实现相同的功能。

以下是我的组成部分:

const boundary = "myboundary"
let data = ""
data += "--" + boundary + "\r\n"
data += 'content-disposition: form-data; '
// Define the name of the form data
      + 'name="' + this.props.label + '"; '
// Provide the real name of the file
      + 'filename="'     + uri.split('/').pop() + '"\r\n'
// And the MIME type of the file
data += 'Content-Type: ' + type + '\r\n'

// There's a blank line between the metadata and the data
data += '\r\n'

// Append the binary data to our body's request
data += `data:${type};base64,` + image + '\r\n'
data += "--" + boundary + "--"

var XHR = new XMLHttpRequest()

XHR.addEventListener('load', callbackConf.callback )

XHR.addEventListener('error', callbackConf.errorCallback )

XHR.open('POST', global.config.getServerAddress() + this.props.api )
XHR.setRequestHeader( 'Authorization', 'Bearer ' +  global.config.token )
XHR.setRequestHeader( 'Content-Type', 'multipart/form-data; boundary=' + boundary )

XHR.send( data )
You can achieve this by a few lines of CSS and JS.

CSS:

        div.clip-context {
          max-height: 95px;
          word-break: break-all;
          white-space: normal;
          word-wrap: break-word; //Breaking unicode line for MS-Edge works with this property;
        }

JS:

        $(document).ready(function(){
             for(let c of $("div.clip-context")){
                    //If each of element content exceeds 95px its css height, extract some first 
                    //lines by specifying first length of its text content. 
                   if($(c).innerHeight() >= 95){
                        //Define text length for extracting, here 170.
                        $(c).text($(c).text().substr(0, 170)); 
                        $(c).append(" ...");
                   }
             }

        });

HTML:

        <div class="clip-context">
            (Here some text)
        </div>
@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;

      }
}

当我从配置文件中删除@EnableElasticsearchRepositories时,一切正常。但是,当我添加相同内容时,出现以下错误:

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


}

在我的配置中。但是,使用高级客户端时,客户端方法会显示错误,因为它属于客户端类型。

请让我知道是否有什么办法可以解决此问题。

先谢谢了。

1 个答案:

答案 0 :(得分:0)

新的Spring Data ElasticSearch升级了他们的方法。所以代替:

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

应该是:

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