Custom @ReadingConverter isn't being triggered but when I was initially implementing this code it worked normally.
Configuration class:
@Configuration
public class ElasticsearchConfig {
@Bean
public MappingElasticsearchConverter mappingElasticsearchConverter() {
return new MappingElasticsearchConverter(new SimpleElasticsearchMappingContext());
}
@Bean
public ElasticsearchCustomConversions elasticsearchCustomConversions(
MappingElasticsearchConverter mappingElasticsearchConverter, ObjectMapper objectMapper) {
return new ElasticsearchCustomConversions(List.of(
new LogEntryReadingConverter(mappingElasticsearchConverter, objectMapper)
));
}
}
Here is the implementation of the converter:
@Component
@ReadingConverter
public class LogEntryReadingConverter implements Converter<Document, LogEntry> {
private final MappingElasticsearchConverter elasticsearchConverter;
private final ObjectMapper objectMapper;
public LogEntryReadingConverter(MappingElasticsearchConverter elasticsearchConverter, ObjectMapper objectMapper) {
this.elasticsearchConverter = elasticsearchConverter;
this.objectMapper = objectMapper;
}
public LogEntry convert(Document source) {
// Use Elasticsearch's internal conversion (respects @Field annotations)
LogEntry logEntry = elasticsearchConverter.read(LogEntry.class, source);
// some custom logic is here
return logEntry;
}
My document class:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@Document(indexName = "logstash")
public class LogEntry implements ILogModel {
@Id
private String id;
// more fields are here
}
My repository class:
@Repository
public interface HaProxyLogRepository extends ElasticsearchRepository<LogEntry, String> {
Page<LogEntry> findByTimestampBetween(LocalDateTime from, LocalDateTime to, Pageable pageable);
}
I would expect the convert method to be called before execution of the repository method "findByTimestampBetween" on method call. It worked before, for some odd reason it never triggers now.
I am using Spring Boot 3.1.2. and Spring Data Elasticsearch 5.1.2.
Removing MappingElasticsearchConverter bean and dependency from configurtion class solved my issue:
@Configuration
public class ElasticsearchConfig {
@Bean
public ElasticsearchCustomConversions elasticsearchCustomConversions(
ObjectMapper objectMapper) {
return new ElasticsearchCustomConversions(List.of(
new LogEntryReadingConverter(objectMapper)
));
}
}