Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
ths-elasticsearch
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王祥松
ths-elasticsearch
Commits
48eef10d
Commit
48eef10d
authored
Sep 19, 2019
by
XSwang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parents
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
428 additions
and
0 deletions
+428
-0
.gitignore
.gitignore
+34
-0
pom.xml
pom.xml
+66
-0
ThsElasticsearchApplication.java
...uaching/thselasticsearch/ThsElasticsearchApplication.java
+13
-0
ElasticSearchClientConfig.java
...ng/thselasticsearch/config/ElasticSearchClientConfig.java
+44
-0
ElasticSearchConfig.java
...huaching/thselasticsearch/config/ElasticSearchConfig.java
+48
-0
WebFluxConfig.java
...a/com/huaching/thselasticsearch/config/WebFluxConfig.java
+24
-0
ElasticSearchUtil.java
...aching/thselasticsearch/controller/ElasticSearchUtil.java
+102
-0
TextController.java
.../huaching/thselasticsearch/controller/TextController.java
+28
-0
ApiVisitDevService.java
...huaching/thselasticsearch/service/ApiVisitDevService.java
+6
-0
ApiVisitDevServiceImpl.java
...thselasticsearch/service/impl/ApiVisitDevServiceImpl.java
+42
-0
application.yml
src/main/resources/application.yml
+5
-0
ThsElasticsearchApplicationTests.java
...ng/thselasticsearch/ThsElasticsearchApplicationTests.java
+16
-0
No files found.
.gitignore
0 → 100644
View file @
48eef10d
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/
pom.xml
0 → 100644
View file @
48eef10d
<?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>
src/main/java/com/huaching/thselasticsearch/ThsElasticsearchApplication.java
0 → 100644
View file @
48eef10d
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
);
}
}
src/main/java/com/huaching/thselasticsearch/config/ElasticSearchClientConfig.java
0 → 100644
View file @
48eef10d
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
);
}
}
}
src/main/java/com/huaching/thselasticsearch/config/ElasticSearchConfig.java
0 → 100644
View file @
48eef10d
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
;
}
}
src/main/java/com/huaching/thselasticsearch/config/WebFluxConfig.java
0 → 100644
View file @
48eef10d
//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);
// }
//}
src/main/java/com/huaching/thselasticsearch/controller/ElasticSearchUtil.java
0 → 100644
View file @
48eef10d
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
();
}
}
}
src/main/java/com/huaching/thselasticsearch/controller/TextController.java
0 → 100644
View file @
48eef10d
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
;
}
}
src/main/java/com/huaching/thselasticsearch/service/ApiVisitDevService.java
0 → 100644
View file @
48eef10d
package
com
.
huaching
.
thselasticsearch
.
service
;
public
interface
ApiVisitDevService
{
void
search
(
String
search
);
}
src/main/java/com/huaching/thselasticsearch/service/impl/ApiVisitDevServiceImpl.java
0 → 100644
View file @
48eef10d
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
);
}
}
src/main/resources/application.yml
0 → 100644
View file @
48eef10d
elasticsearch
:
hostname
:
47.97.200.228
port
:
9200
scheme
:
http
\ No newline at end of file
src/test/java/com/huaching/thselasticsearch/ThsElasticsearchApplicationTests.java
0 → 100644
View file @
48eef10d
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
()
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment