使用Spring Boot与Elasticsearch时出错。

huangapple 未分类评论47阅读模式
英文:

Error while using Elasticsearch with Spring Boot

问题

以下是您要翻译的内容:

我正试图在Spring Boot应用程序中实现Elasticsearch,我按照文档中描述的步骤进行操作,当涉及客户端集成(使用推荐的高级Rest客户端)、映射或索引时,一切正常。但是当我尝试查询文档时,出现以下错误:

java.lang.NumberFormatException: For input string: "-Je513EBi1vVkAiBdanX"

我的配置:

@Configuration
@EnableElasticsearchRepositories
public class ElasticConfig extends AbstractElasticsearchConfiguration {

    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration configuration = ClientConfiguration.localhost();
        RestHighLevelClient client = RestClients.create(configuration).rest();
        return client;
    }

    @Bean
    public ElasticsearchRestTemplate elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
}

我的存储库:

@Repository
public interface UserRepository extends ElasticsearchRepository<User, Long> {
    List<User> findByName(String name);
}

我的服务:

public List<User> getUsers(String qry){
    return userRepository.findByName(qry);
}

我的模型:

@Document(indexName = "users")
public class User {

    @Id private Long id;
    private String name;
    private String username;
    private String avatar;
    private String bio;
    private boolean checked;

    public User() { }

    public User(
            @JsonProperty("id") Long id,
            @JsonProperty("name") String name,
            @JsonProperty("username") String username,
            @JsonProperty("avatar") String avatar,
            @JsonProperty("bio") String bio,
            @JsonProperty("checked") boolean checked) {
        this.id = id;
        this.name = name;
        this.username = username;
        this.avatar = avatar;
        this.bio = bio;
        this.checked = checked;
    }

    // Getters
    // Setters
}
英文:

I'm trying to implement Elasticsearch in a Spring boot app, I followed the steps described in the documentation and everything works fine when it comes to client integration (using the recommended Hight Level Rest Client), mapping or indexing.. But when I try to query the documents I get the following error:

java.lang.NumberFormatException: For input string: &quot;-Je513EBi1vVkAiBdanX&quot;

My config:

@Configuration
@EnableElasticsearchRepositories
public class ElasticConfig extends AbstractElasticsearchConfiguration {

    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration configuration = ClientConfiguration.localhost();
        RestHighLevelClient client = RestClients.create(configuration).rest();
        return client;
    }

    @Bean
    public ElasticsearchRestTemplate elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
}

My repository:

@Repository
public interface UserRepository extends ElasticsearchRepository&lt;User, Long&gt; {
    List&lt;User&gt; findByName(String name);
}

My Service:

public List&lt;User&gt; getUsers(String qry){
    return userRepository.findByName(qry);
}

My Model:

@Document(indexName = &quot;users&quot;)
public class User {

    @Id private Long id;
    private String name;
    private String username;
    private String avatar;
    private String bio;
    private boolean checked;

    public User() { }

    public User(
            @JsonProperty(&quot;id&quot;) Long id,
            @JsonProperty(&quot;name&quot;) String name,
            @JsonProperty(&quot;username&quot;) String username,
            @JsonProperty(&quot;avatar&quot;) String avatar,
            @JsonProperty(&quot;bio&quot;) String bio,
            @JsonProperty(&quot;checked&quot;) boolean checked) {
        this.id = id;
        this.name = name;
        this.username = username;
        this.avatar = avatar;
        this.bio = bio;
        this.checked = checked;
    }

    // Getters
    // Setters
}

答案1

得分: 0

以下是翻译好的部分:

请确认您正在使用的 Spring Data 和 Elasticsearch 版本。另外,我对您实现的 RestHighLevelClient 有一些疑虑。您引用了一个 localhost,但端口呢?
以下是一段可工作代码的片段:

@Bean
public RestHighLevelClient elasticRestClient() {
    String[] httpHosts = httpHostsProperty.split(";");
    HttpHost[] httpHostsAsArray = new HttpHost[httpHosts.length];
    int index = 0;

    for (String httpHostAsString : httpHosts) {
        HttpHost httpHost = new HttpHost(httpHostAsString.split(":")[0], Integer.parseInt(httpHostAsString.split(":")[1]), "http");
        httpHostsAsArray[index++] = httpHost;
    }

    RestClientBuilder restClientBuilder = RestClient.builder(httpHostsAsArray)
            .setRequestConfigCallback(builder -> builder
                    .setConnectTimeout(connectTimeOutInMs)
                    .setSocketTimeout(socketTimeOutInMs)
            );

    return new RestHighLevelClient(restClientBuilder);
}

希望这能满足您的要求。如有其他翻译需求,请随时提问。

英文:

Can you confirm the version of Spring Data and Elasticsearch you are using? In addition, I'm a bit concerned on the way you implemented your RestHighLevelClient. You refer to a localhost but what about the port?
Please find a snippet of working code below:

    @Bean
public RestHighLevelClient elasticRestClient () {
    String[] httpHosts = httpHostsProperty.split(&quot;;&quot;);
    HttpHost[] httpHostsAsArray = new HttpHost[httpHosts.length];
    int index = 0;

    for (String httpHostAsString : httpHosts) {
        HttpHost httpHost = new HttpHost(httpHostAsString.split(&quot;:&quot;)[0], new Integer(httpHostAsString.split(&quot;:&quot;)[1]), &quot;http&quot;);
        httpHostsAsArray[index++] = httpHost;
    }

    RestClientBuilder restClientBuilder = RestClient.builder(httpHostsAsArray)
            .setRequestConfigCallback(builder -&gt; builder
                    .setConnectTimeout(connectTimeOutInMs)
                    .setSocketTimeout(socketTimeOutInMs)
            );

    return new RestHighLevelClient(restClientBuilder);
}

huangapple
  • 本文由 发表于 2020年5月29日 05:41:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/62075008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定