英文:
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: "-Je513EBi1vVkAiBdanX"
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<User, Long> {
List<User> findByName(String name);
}
My Service:
public List<User> getUsers(String qry){
return userRepository.findByName(qry);
}
My Model:
@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
}
答案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(";");
HttpHost[] httpHostsAsArray = new HttpHost[httpHosts.length];
int index = 0;
for (String httpHostAsString : httpHosts) {
HttpHost httpHost = new HttpHost(httpHostAsString.split(":")[0], new Integer(httpHostAsString.split(":")[1]), "http");
httpHostsAsArray[index++] = httpHost;
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHostsAsArray)
.setRequestConfigCallback(builder -> builder
.setConnectTimeout(connectTimeOutInMs)
.setSocketTimeout(socketTimeOutInMs)
);
return new RestHighLevelClient(restClientBuilder);
}
专注分享java语言的经验与见解,让所有开发者获益!
评论