一:引入SpringDataElasticsearch依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
二:创建RestHighLevelClient配置类
@Configuration
public class EsConfig {
@Bean(destroyMethod = "close")
public org.elasticsearch.client.RestHighLevelClient restClient() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("192.yourIp.103:9200")
.withBasicAuth("elastic", "123456")
.build();
org.elasticsearch.client.RestHighLevelClient client = RestClients.create(clientConfiguration).rest();
return client;
}
}
三:配置YML: logging.level — 日志等级
核心
logging:
level:
org.springframework.data.elasticsearch.core: debug
org.springframework.data.elasticsearch.client.WIRE: TRACE
spring:
elasticsearch:
rest:
uris: 192.yourIp.103:9200
username: elastic
password: 123456
logging:
level:
org.springframework.data.elasticsearch.core: debug
org.springframework.data.elasticsearch.client.WIRE: TRACE
三:请求接口,查看日志
3.1:请求代码
@RequestMapping("/product")
public class ProductController {
@Autowired
private IBizProductService iBizProductService;
@Autowired
private IEsBizProductRepository iEsBizProductRepository;
@PostMapping("/syncEsProduct")
public String syncEsProduct() throws Exception {
List<BizProduct> productList = iBizProductService.list();
Iterable<BizProduct> productIterable = iEsBizProductRepository.saveAll(productList);
return "同步Es成功";
}
}
3.2:Postman请求地址
http://localhost:8093/product/syncEsProduct
3.3:查看输出日志
2023-01-10 22:56:57.114 TRACE 26180 — [nio-8093-exec-1] o.s.data.elasticsearch.client.WIRE : [481141cc]文章来源:https://www.toymoban.com/news/detail-461409.html
Sending request POST /_bulk?timeout=1m with parameters:
Request body: {"index":{"_index":"stock","_id":"1"}}
{"_class":"com.europa.tx.stock.entity.BizProduct","id":1,"name":"可口可乐","categoryId":1,"categoryName":"纤维","stockSn":"00001","auditStatus":0,"onSelfStatus":1,"cityId":200000,"cityName":"上海","provinceId":200000,"provinceName":"上海","price":5.0,"promotionPrice":5.0,"salesPrice":5.0,"stock":100,"warningStock":95,"unit":"ml","beginTime":1673236800000,"endTime":1675051200000,"tenantId":0}
2023-01-10 22:56:57.124 TRACE 26180 --- [/O dispatcher 1] o.s.data.elasticsearch.client.WIRE : [481141cc] Received raw response: 200 OK
2023-01-10 22:56:57.139 TRACE 26180 --- [nio-8093-exec-1] o.s.data.elasticsearch.client.WIRE : [3a274db7] Sending request POST /stock/_refresh?ignore_throttled=false&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true with parameters:
2023-01-10 22:56:57.170 TRACE 26180 --- [/O dispatcher 1] o.s.data.elasticsearch.client.WIRE : [3a274db7] Received raw response: 200 OK
文章来源地址https://www.toymoban.com/news/detail-461409.html
到了这里,关于2: [SpringData集成Elasticsearch] --- 配置打印命令日志的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!