Elasticsearch client

picturesafe-search wraps the Elasticsearch Java client and provides a high level API to access its functionality. If you need to access certain functions, which are not covered by picturesafe-search, it is possible to use the wrapped Elasticsearch Java High Level REST Client directly.

Access via ElasticsearchService

@Component
@ComponentScan
public class MyClass {

    @Autowired
    private ElasticsearchService elasticsearchService;

    private RestHighLevelClient getRestHighLevelClient() {
        return elasticsearchService.getRestClient();
    }
}

Access via SingleIndexElasticsearchService

@Component
@ComponentScan
public class MyClass {
    
    @Autowired
    private SingleIndexElasticsearchService singleIndexElasticsearchService;

    private RestHighLevelClient getRestHighLevelClient() {
        return singleIndexElasticsearchService.getElasticsearchService().getRestClient();
    }
}

Access via DefaultClientConfiguration

When using the the DefaultElasticConfiguration or the DefaultClientConfiguration, the RestClientConfiguration Spring Bean provided by the DefaultClientConfiguration can be autowired:

@Component
@ComponentScan
public class MyClass {

    @Autowired
    private RestClientConfiguration restClientConfiguration;

    private RestHighLevelClient getRestHighLevelClient() {
        return restClientConfiguration.getClient();
    }
}

Access via Spring Bean

When not using the the DefaultElasticConfiguration or the DefaultClientConfiguration, a client Spring Bean has to be defined as follows:

@Configuration
@PropertySource("classpath:elasticsearch.properties")
public class DefaultClientConfiguration {

    @Value("${elasticsearch.hosts:localhost:9200}")
    private String elasticsearchHosts;

    @Value("${elasticsearch.sniffer.enabled:false}")
    private boolean snifferEnabled;

    @Bean
    public RestClientConfiguration restClientConfiguration() {
        final RestClientConfiguration rcc = new RestClientConfiguration(elasticsearchHosts);
        rcc.setSnifferEnabled(snifferEnabled);
        return rcc;
    }
}

The Elasticsearch Java High Level REST Client can then be accessed via the RestClientConfiguration:

@Component
@ComponentScan
public class MyClass {

    @Autowired
    private RestClientConfiguration restClientConfiguration;

    private RestHighLevelClient getRestHighLevelClient() {
        return restClientConfiguration.getClient();
    }
}

Since picturesafe-search requires a RestClientConfiguration Spring Bean, the Elasticsearch Java High Level REST Client can also be accessed via the ElasticsearchService or the SingleIndexElasticsearchService with this configuration.