[gitee项目地址](https://gitee.com/liufei1988/springboot-es)
从`TransportClient`Elasticsearch 7开始不推荐使用,将在Elasticsearch 8中将其删除。([请参阅Elasticsearch文档](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html))。`TransportClient`只要使用的Elasticsearch[版本中](https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.RELEASE/reference/html/#elasticsearch.versions)可用,Spring Data Elasticsearch将支持。
```
/**
*Description Transport Client
*@Author liufei
*@DATE 2019/11/4 15:45
**/
@Configuration
public class TransportClientConfig extends ElasticsearchConfigurationSupport {
/**
* 设置Transport Client.
* @return
* @throws UnknownHostException
*/
@Bean
public Client elasticsearchClient() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client;
}
/**
* 创建ElasticsearchTemplate提供相同名称的bean,同时提供elasticsearchOperations和elasticsearchTemplate
* @return
* @throws UnknownHostException
*/
@Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(elasticsearchClient(), entityMapper());
}
/**
* 使用元模型对象映射 ElasticsearchMapper
* @return
*/
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
new DefaultConversionService());
entityMapper.setConversions(elasticsearchCustomConversions());
return entityMapper;
}
}
```