-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.* | ||
target/* | ||
*.iml | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>spring-boot</artifactId> | ||
<groupId>com.git.hui.boot</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>142-search-es</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.elasticsearch</groupId> | ||
<artifactId>elasticsearch</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.elasticsearch.client</groupId> | ||
<artifactId>elasticsearch-rest-high-level-client</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# es实例demo | ||
|
||
## es基本case | ||
|
||
权限验证开启 | ||
|
||
修改配置文件 `config/elasticsearch.yml`,添加下面的配置 | ||
|
||
```yaml | ||
xpack.security.enabled: true | ||
xpack.security.authc.accept_default_password: false | ||
``` | ||
启动es服务 | ||
```bash | ||
bin/elasticsearch | ||
``` | ||
|
||
生成密码 | ||
|
||
```bash | ||
# 执行完毕之后输入密码, 比如测试的密码都是 test123 (生产环境不要这么干) | ||
bin/elasticsearch-setup-passwords interactive | ||
``` | ||
|
||
es的交互,主要使用 `Basic Auth` 方式进行身份校验,简单来讲,就是在请求头中,添加 | ||
|
||
```bash | ||
Authorization: Basic ZWxhc3RpYzp0ZXN0MTIz | ||
``` | ||
|
||
## 数据准备 | ||
|
||
```json | ||
POST first-index/_doc | ||
{ | ||
"message": "GET /search HTTP/1.1 200 1070000", | ||
"user": { | ||
"id": "kimchy", | ||
"name": "YiHui" | ||
}, | ||
"hobby": [ | ||
"java", | ||
"python" | ||
] | ||
} | ||
``` |
21 changes: 21 additions & 0 deletions
21
spring-boot/142-search-es/src/main/java/com/git/hui/boot/search/es/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.git.hui.boot.search.es; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author yihui | ||
* @date 2022/1/7 | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public Application(EsTest esTest) throws Exception { | ||
esTest.testGet(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...ot/142-search-es/src/main/java/com/git/hui/boot/search/es/ElasticsearchConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.git.hui.boot.search.es; | ||
|
||
import lombok.Getter; | ||
import org.apache.http.HttpHost; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestClientBuilder; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Getter | ||
@Configuration | ||
public class ElasticsearchConfiguration { | ||
|
||
@Value("${elasticsearch.host}") | ||
private String host; | ||
|
||
@Value("${elasticsearch.port}") | ||
private int port; | ||
|
||
@Value("${elasticsearch.connTimeout}") | ||
private int connTimeout; | ||
|
||
@Value("${elasticsearch.socketTimeout}") | ||
private int socketTimeout; | ||
|
||
@Value("${elasticsearch.connectionRequestTimeout}") | ||
private int connectionRequestTimeout; | ||
|
||
@Value("${elasticsearch.user}") | ||
private String user; | ||
|
||
@Value("${elasticsearch.pwd}") | ||
private String pwd; | ||
|
||
@Bean(destroyMethod = "close", name = "client") | ||
public RestHighLevelClient initRestClient() { | ||
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port)) | ||
.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder | ||
.setConnectTimeout(connTimeout) | ||
.setSocketTimeout(socketTimeout) | ||
.setConnectionRequestTimeout(connectionRequestTimeout)); | ||
return new RestHighLevelClient(builder); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
spring-boot/142-search-es/src/main/java/com/git/hui/boot/search/es/EsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.git.hui.boot.search.es; | ||
|
||
import org.elasticsearch.action.get.GetRequest; | ||
import org.elasticsearch.action.get.GetResponse; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.Base64Utils; | ||
|
||
import java.util.Map; | ||
|
||
|
||
/** | ||
* @author yihui | ||
* @date 2022/1/7 | ||
*/ | ||
@Service | ||
public class EsTest { | ||
@Autowired | ||
private RestHighLevelClient client; | ||
|
||
private static String auth = "Basic ZWxhc3RpYzp0ZXN0MTIz"; | ||
|
||
public EsTest(ElasticsearchConfiguration elasticsearchConfiguration) { | ||
auth = Base64Utils.encodeToString((elasticsearchConfiguration.getUser() + ":" + elasticsearchConfiguration.getPwd()).getBytes()); | ||
auth = "Basic " + auth; | ||
} | ||
|
||
public void testGet() throws Exception { | ||
GetRequest getRequest = new GetRequest("first-index", "_doc", "gvarh3gBF9fSFsHNuO49"); | ||
RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder(); | ||
requestOptions.addHeader("Authorization", auth); | ||
GetResponse getResponse = client.get(getRequest, requestOptions.build()); | ||
if (getResponse.isExists()) { | ||
String sourceAsString = getResponse.getSourceAsString(); | ||
System.out.println(sourceAsString); | ||
} else { | ||
System.out.println("no string!"); | ||
} | ||
} | ||
|
||
public Map<String, Object> searchDataById(String index, String type, String id, String fields) { | ||
try { | ||
GetRequest getRequest = new GetRequest(index, type, id); | ||
RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder(); | ||
requestOptions.addHeader("Authorization", auth); | ||
GetResponse getResponse = client.get(getRequest, requestOptions.build()); | ||
return getResponse.getSource(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-boot/142-search-es/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
spring: | ||
elasticsearch: | ||
rest: | ||
uris: http://localhost:9200 | ||
username: elastic | ||
password: test123 | ||
|
||
elasticsearch: | ||
host: localhost | ||
port: 9200 | ||
user: elastic | ||
pwd: test123 | ||
connTimeout: 3000 | ||
socketTimeout: 5000 | ||
connectionRequestTimeout: 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters