Commit 48eef10d by XSwang

init

parents
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
.mvn
mvnw
mvnw.cmd
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.huaching</groupId>
<artifactId>ths-elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ths-elasticsearch</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.2.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.huaching.thselasticsearch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ThsElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(ThsElasticsearchApplication.class, args);
}
}
package com.huaching.thselasticsearch.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PreDestroy;
import java.io.IOException;
@Configuration
public class ElasticSearchClientConfig {
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchClientConfig.class);
@Autowired
private ElasticSearchConfig elasticSearchConfig;
@Autowired
private RestHighLevelClient restHighLevelClient;
@Bean
public RestHighLevelClient restHighLevelClient(){
return new RestHighLevelClient(
RestClient.builder(
new HttpHost(elasticSearchConfig.getHostname(), elasticSearchConfig.getPort(), elasticSearchConfig.getScheme())));
}
@PreDestroy
public void close(){
try {
restHighLevelClient.close();
} catch (IOException e) {
logger.error("RestHighLevelClient 关闭出异常", e);
}
}
}
package com.huaching.thselasticsearch.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
/**
* 地址
*/
private String hostname;
/**
* 端口
*/
private Integer port;
/**
* 请求方式
*/
private String scheme;
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getScheme() {
return scheme;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
}
//package com.huaching.thselasticsearch.config;
//
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpHeaders;
//import org.springframework.web.reactive.config.CorsRegistry;
//import org.springframework.web.reactive.config.WebFluxConfigurer;
//
//@Configuration
//public class WebFluxConfig implements WebFluxConfigurer {
//
// /**
// * 全局跨域配置,根据各自需求定义
// * @param registry
// */
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// registry.addMapping("/**")
// .allowCredentials(true)
// .allowedOrigins("*")
// .allowedHeaders("*")
// .allowedMethods("*")
// .exposedHeaders(HttpHeaders.SET_COOKIE);
// }
//}
package com.huaching.thselasticsearch.controller;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ElasticSearchUtil {
// public static String sqlSelect(String url, String index, String sql){
//
// if (url == null || url.isEmpty()){
// return null;
// }
// if (sql == null || sql.isEmpty()){
// return null;
// }
//
// Map<String, String> headerMap = new HashMap<>(4);
// headerMap.put("Content-type", "application/json");
//
// Map<String, String> paramsMap = new HashMap<>(4);
// paramsMap.put("query", sql);
// String jsonStr = JSONObject.toJSONString(paramsMap, SerializerFeature.WriteMapNullValue);
//
// String httpUrl = url + "/_sql/translate?format=json";
// String res = HttpUtil.postHeaderString(httpUrl, headerMap, jsonStr);
//
// String httpUrl1 = url + "/" + index + "/_search";
//
// return HttpUtil.postHeaderString(httpUrl1, headerMap, res);
// }
public static void main(String[] args) throws IOException {
// System.out.println(ElasticSearchUtil.sqlSelect("http://47.97.200.228:9200", "api_visit_dev","SELECT responeBody FROM api_visit_dev limit 1"));
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("47.97.200.228", 9200, "http")));
GetRequest getRequest = new GetRequest("api_visit_dev");
getRequest.id("k32YHWwBkEEehDN-Y5o4");
// getRequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = new String[]{"responeBody"};
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
getRequest.fetchSourceContext(fetchSourceContext);
// getRequest.storedFields("responeBody");
// GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
// System.out.println(getResponse.getSource());
// String message = getResponse.getField("message").getValue();
// IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
/**
* 搜索api
*/
SearchRequest searchRequest = new SearchRequest("api_visit_dev");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getHits());
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.huaching.thselasticsearch.controller;
import com.huaching.thselasticsearch.service.ApiVisitDevService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("test")
public class TextController {
private static final Logger logger = LoggerFactory.getLogger(TextController.class);
@Autowired
private ApiVisitDevService apiVisitDevService;
@RequestMapping("search")
public Map testSearch(){
apiVisitDevService.search(null);
return null;
}
}
package com.huaching.thselasticsearch.service;
public interface ApiVisitDevService {
void search(String search);
}
package com.huaching.thselasticsearch.service.impl;
import com.huaching.thselasticsearch.config.ElasticSearchClientConfig;
import com.huaching.thselasticsearch.service.ApiVisitDevService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class ApiVisitDevServiceImpl implements ApiVisitDevService {
private static final Logger logger = LoggerFactory.getLogger(ApiVisitDevServiceImpl.class);
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public void search(String search) {
SearchRequest searchRequest = new SearchRequest("api_visit_dev");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = null;
try {
searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(searchResponse);
}
}
elasticsearch:
hostname: 47.97.200.228
port: 9200
scheme: http
\ No newline at end of file
package com.huaching.thselasticsearch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ThsElasticsearchApplicationTests {
@Test
public void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment