Java 使用ElasticsearchRepository和RestHighLevelClient的方法saveAll(list)时出错
我使用了org.elasticsearch:elasticsearch版本6.8,对于连接,我使用了TransportClient。然后,我决定升级到7.6版,因为TransportClient不推荐使用,所以我不得不通过RestHighLevelClient使用到elastic的连接。我正在使用存储库处理springframework中的弹性数据。但现在我没有通过存储库进行弹性数据存储。我的存储库继承自ElasticsearchRepository接口。我使用标准的saveAll方法 我有一个MyPostgresModel的列表,我需要将其保存到Elastic中。我想快速完成,这就是为什么我使用@Transactional并将saveAll方法与Iterable结合使用。MyPostgresModel必须使用Document方法返回MyLastICModel对象 这是有效的:Java 使用ElasticsearchRepository和RestHighLevelClient的方法saveAll(list)时出错,java,spring,spring-boot,
elasticsearch,Java,Spring,Spring Boot,
elasticsearch,我使用了org.elasticsearch:elasticsearch版本6.8,对于连接,我使用了TransportClient。然后,我决定升级到7.6版,因为TransportClient不推荐使用,所以我不得不通过RestHighLevelClient使用到elastic的连接。我正在使用存储库处理springframework中的弹性数据。但现在我没有通过存储库进行弹性数据存储。我的存储库继承自ElasticsearchRepository接口。我使用标准的saveAll方法 我有一个
myElasticRepository.save(users.get(0.toDocument())代码>
这是行不通的:myElasticRepository.saveAll(()->users.stream().map(MyPostgresModel::toDocument.iterator())代码>
我不知道。为什么不呢?它还返回“type丢失;7930:type丢失;7931:…”错误。调试程序告诉我弹性响应“400错误请求”状态-数据验证不进行
在我使用TransportClient之前,一切正常,之后TransportClient和RestHighLevelClient使用一个通用的ElasticsearchOperations界面
我使用以下配置类进行连接:
@Slf4j
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.project.elastic.repository")
public class ElasticConfig extends AbstractElasticsearchConfiguration {
@Value("${spring.data.elastic.url}")
private String url;
@Override
public RestHighLevelClient elasticsearchClient() {
return RestClients.create(ClientConfiguration.create(url)).rest();
}
}
以下是我的弹性模型的示例:
@Data
@Document(indexName = "user", type = "_doc", createIndex = false)
public class ElasticUser {
@Id
private final String id;
private final String address;
private final String firstName;
private final String lastName;
private final List<String> phones;
private final String gender;
public ElasticUser(
@NonNull String id,
@NonNull String address,
@NonNull String firstName,
@NonNull String lastName,
@NonNull List<String> phones,
@Nullable String gender
) {
this.id = id;
this.address = address;
this.firstName = firstName;
this.lastName = lastName;
this.phones = phones;
this.gender = gender;
}
}
@数据
@文档(index name=“user”,type=“\u doc”,createIndex=false)
公共类弹性用户{
@身份证
私有最终字符串id;
私有最终字符串地址;
私有最终字符串名;
私有最终字符串lastName;
私人最终名单电话;
私人最终字符串性别;
公共弹性用户(
@非空字符串id,
@非空字符串地址,
@非空字符串firstName,
@非空字符串lastName,
@非空列表电话,
@可空字符串性别
) {
this.id=id;
this.address=地址;
this.firstName=firstName;
this.lastName=lastName;
这个。电话=电话;
这个。性别=性别;
}
}
服务方式:
@Transactional
public void saveUsers(@NonNull List<PostgresUser> users) {
// myElasticRepository.save(users.get(0).toDocument());
myElasticRepository.saveAll(() -> users.stream().map(PostgresUser::toDocument).iterator());
}
@Transactional
public void saveUsers(@NonNull List users){
//myElasticRepository.save(users.get(0.toDocument());
myElasticRepository.saveAll(()->users.stream().map(PostgresUser::toDocument.iterator());
}
问题在于elasticsearch版本。我使用了7.6.2版本的客户端,我的弹性是6.8.6。我的意思是,这不允许列表。对于列表中的每一项,它都会显示其“缺失”。您是否也可以将此代码上载到github,我们可以在那里克隆并尝试它,我还需要pom
或Gradle
。这很难部署。我可以给你做一个原型,但这需要时间。也许你能告诉我怎么挖?我现在正在查看elasticsearch配置中entityMapper的方向。我已经安装了rest客户端,但没有使用spring引导和数据,所以我不确定这里的问题是spring还是rest客户端,在这个阶段,如果不复制它,很难判断:(