hibernate-searchhibernate-search-6

Reuse AwsSigningRequestInterceptor outside of Hibernate Search


I am using Hibernate Search 6.2 within my Spring Boot 2.7 project using elasticsearch-aws backend. That works well.

Now I am integrating elasticsearch-evolution for schema migrations. In order to reuse the AWS signing mechanisms of Hibernate Search witin elasticsearch-evolution I am trying to hook the AwsSigningRequestInterceptor into RestClientBuilder's setHttpClientConfigCallback (as documented in their Readme).

Since that AwsSigningRequestInterceptor is not public I am trying to leverage ElasticsearchAwsHttpClientConfigurer. Do you have some hints how to access the needed BeanResolver and ConfigurationPropertySource within the Spring context? Normal injection does not work.


Solution: Thanks to @yrodiere following code works:

@Autowired
SessionFactory sessionFactory;

@Bean
public RestClient restClient() {
  SearchMapping mapping = Search.mapping(sessionFactory);
  Backend backend = mapping.backend();
  ElasticsearchBackend elasticsearchBackend = 
  backend.unwrap(ElasticsearchBackend.class);
  return elasticsearchBackend.client(RestClient.class);
}

Solution

  • Hibernate Search resolves this particular bean through an internal registry of its own, not through Spring DI. I guess you could just call new ElasticsearchAwsHttpClientConfigurer().

    Though to be honest, you're dealing with internal code here, which can (and will) change its exposed constructors/methods at any time, so this is not a future-proof strategy.

    I think your best option would be to retrieve Hibernate Search's Elasticsearch client and somehow have elasticsearch-evolution use it.

    If it's not possible, your best remaining solution is probably to copy-paste the relevant code into your project, if the license of your project is compatible with Hibernate Search's own LGPL licensing. Otherwise you could publish an LGPL project of your own with adapted code.