@Configuration public class ElasticSearchConfig { public static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); // builder.addHeader("Authorization", "Bearer " + TOKEN); // builder.setHttpAsyncResponseConsumerFactory( // new HttpAsyncResponseConsumerFactory // .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024)); COMMON_OPTIONS = builder.build(); } @Bean public RestHighLevelClient esRestClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))); return client; } }
时间: 2024-02-14 15:29:02 浏览: 166
你的`ElasticSearchConfig`类是一个配置类,用于配置Elasticsearch的连接。它创建了一个`RestHighLevelClient`,并指定了Elasticsearch的主机和端口。
你可以在你的测试类中使用`@ContextConfiguration`注解来指定加载这个配置类。例如:
```java
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ElasticSearchConfig.class)
public class YourTest {
// 测试方法...
}
```
这样,当你运行测试时,Spring会加载`ElasticSearchConfig`中的配置,并创建一个包含`RestHighLevelClient`的Spring上下文。你就可以在测试中使用`RestHighLevelClient`来与Elasticsearch进行交互了。
阅读全文