I used org.elasticsearch:elasticsearch version 6.8 and for connectivity I used TransportClient. I then decided to upgrade to version 7.6 and since TransportClient is Deprecated, I had to use the connection to elastic via the RestHighLevelClient. I'm using a repository to work with an elastic data in springframework. But now I don't have elastic data storage through a repository working. My repository is inherited from the ElasticsearchRepository interface. I use the standard saveAll method.
I have a list of MyPostgresModel and I need to save that into Elastic. I want to do quickly that's why I use @Transactional and use saveAll method with Iterable. MyPostgresModel has toDocument method to return MyElasticModel object.
This is works: myElasticRepository.save(users.get(0).toDocument());
And this isn't works: myElasticRepository.saveAll(() -> users.stream().map(MyPostgresModel::toDocument).iterator());
I have no idea. Why not? That also returns "type is missing;7930: type is missing;7931:.." error. And the debuger tells me that elastic responses "400 Bad Request" status - data validation is not going.
Before I used the TransportClient everything was OK and after all TransportClient and RestHighLevelClient use a common ElasticsearchOperations interface...
I use the following config class for connection:
@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();
}
}
Here is the example of my elastic model:
@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;
}
}
Service method:
@Transactional
public void saveUsers(@NonNull List<PostgresUser> users) {
// myElasticRepository.save(users.get(0).toDocument());
myElasticRepository.saveAll(() -> users.stream().map(PostgresUser::toDocument).iterator());
}