springboot 整合 spring data elasticsearch 打印查询语句

学习elasticsearch的时候,想要查看elasticsearch发送的DSL语句,类似MyBatis可以在idea的控制台上输出Mysql语句,可以添加如下配置

一、配置RestHighLevelClient

/**
 * @author wfd
 * @description
 * @create 2021/3/7 23:10
 */
@Configuration
public class EsConfig {
   

	// @Bean
	// public RestHighLevelClient restHighLevelClient() {
   
	// 	return new RestHighLevelClient(
	// 			RestClient.builder(
	// 					new HttpHost("192.168.126.130", 9200, "http")));
	// }
	
	
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Spring Boot应用程序中使用ElasticsearchClient进行SQL语句查询,需要以下步骤: 1. 添加ElasticsearchClient和其依赖项的Maven依赖关系: ```xml <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.9.3</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.9.3</version> </dependency> ``` 2. 配置ElasticsearchClient连接: ```yaml elasticsearch: rest: hosts: localhost:9200 scheme: http ``` 3. 创建ElasticsearchClient连接客户端: ```java import java.util.Collections; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.elasticsearch.RestClientProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.client.RestClients.ElasticsearchRestClient; import org.springframework.data.elasticsearch.client.RestClients.ElasticsearchRestClientBuilder; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; @Configuration public class ElasticsearchConfig { private final RestClientProperties properties; @Autowired public ElasticsearchConfig(RestClientProperties properties) { this.properties = properties; } @Bean public RestHighLevelClient elasticsearchClient() { return new ElasticsearchRestClientBuilder(Collections.singletonList(properties.getUris())) .withScheme(properties.getScheme()) .withBasicAuth(properties.getUsername(), properties.getPassword()) .build(); } @Bean public ElasticsearchRestTemplate elasticsearchTemplate(RestHighLevelClient elasticsearchClient, ElasticsearchConverter elasticsearchConverter) { return new ElasticsearchRestTemplate(new ElasticsearchRestClient(elasticsearchClient), elasticsearchConverter); } @Bean public ElasticsearchConverter elasticsearchConverter() { return new ElasticsearchConverter(new SimpleElasticsearchMappingContext()); } } ``` 4. 编写SQL查询,例如: ```sql SELECT * FROM my_index WHERE title LIKE '%Spring%'; ``` 5. 在Spring Boot应用程序中使用ElasticsearchClient执行SQL查询: ```java import java.io.IOException; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.core.CountResponse; import org.elasticsearch.client.core.SearchRequest; import org.elasticsearch.client.core.SearchResponse; import org.elasticsearch.client.core.TermVectorsRequest; import org.elasticsearch.client.core.TermVectorsResponse; import org.elasticsearch.client.sql.SqlRequest; import org.elasticsearch.client.sql.SqlResponse; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.client.RestClients.ElasticsearchRestClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class ElasticsearchController { @Autowired private ElasticsearchRestClient elasticsearchClient; @GetMapping("/search") public void search(@RequestParam("query") String query) throws IOException { SqlRequest sqlRequest = new SqlRequest(query); SqlResponse sqlResponse = elasticsearchClient.sql().query(sqlRequest).getResponse(); System.out.println(sqlResponse.toString()); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值