1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.gulimall.gulimallsearch.search;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetMappingResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gulimall.gulmallsearch.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @Author BaiYZ
* @Program gulimail
* @Description 测试elasticsearch
* @Date 2022-03-27 22:43:48
*/
@SpringBootTest
public class TestElasticsearch {
@Autowired
private ElasticsearchClient elasticsearchClient;

@Test
public void testSearch() {
System.out.println(elasticsearchClient);
}

@Test
void indexData() throws IOException {
// lambda传递了function接口的apply的实现,参数t会根据function<R,T>进行定义,R为返回T为参数类型,也就是传入lambda参数会自动类型推断——>fn接口中的apply()方法被实现了,那么调用fun.apply(参数)就会执行传递的方法的实现
CreateIndexResponse java_client_index = elasticsearchClient.indices().create(createIndexBuilder ->
createIndexBuilder.index("java_client_index")
);
//是否成功创建
Boolean acknowledged = java_client_index.acknowledged();
System.out.println("索引操作:" + acknowledged);
}

@Test
void searchData() throws IOException {
GetMappingResponse mapping = elasticsearchClient.indices().getMapping(e -> e.index("java_client_index"));
System.out.println(mapping);
System.out.println(elasticsearchClient.indices().get(r -> r.index("java_client_index")).result().size());
System.out.println(elasticsearchClient.indices().getAlias(e -> e.index("java_client_index")));
System.out.println(elasticsearchClient.indices().getSettings(e -> e.index("java_client_index")));

}

@Test
void deleteIndex() throws IOException {
System.out.println(elasticsearchClient.indices().delete(r -> r.index("java_client_index")).acknowledged());
}

@Test
void insertIndex() throws IOException {
User user = new User("byz", "男", 25);
ObjectMapper objectMapper = new ObjectMapper();
String userJson = objectMapper.writeValueAsString(user);
System.out.println(userJson);
IndexResponse indexResponse = elasticsearchClient.index(r -> r.index("user").id("1001").document(user));
System.out.println(indexResponse.result().jsonValue());
}

@Test
void updateIndex() throws IOException {
User user = new User();
user.setAge(15);
UpdateResponse<User> updateUser = elasticsearchClient.update(r -> r.index("user").id("1001").doc(user), User.class);
System.out.println(updateUser);
}

/**
* 判断是否存在
*
* @throws IOException 异常
*/
@Test
void existDocument() throws IOException {
BooleanResponse userBoolean = elasticsearchClient.exists(r -> r.index("user").id("1001"));
System.out.println(userBoolean.value());
}

/**
* 查询一条数据
*
* @throws IOException 异常
*/
@Test
void getDocument() throws IOException {
GetResponse<User> resp = elasticsearchClient.get(r -> r.index("user").id("1001"), User.class);
User source = resp.source();
System.out.println(source);
}

/**
* 删除一个文档
*
* @throws IOException 异常
*/
@Test
void deleteDocument() throws IOException {
DeleteResponse deleteResponse = elasticsearchClient.delete(r -> r.index("user").id("1001"));
System.out.println(deleteResponse.result().jsonValue());
}

/**
* 批量插入
*
* @throws IOException 异常
*/
@Test
void bulkList() throws IOException {
List<User> userList = new ArrayList<>();
userList.add(new User("jon", "男", 18));
userList.add(new User("lucy", "男", 17));
userList.add(new User("jack", "女", 199));
userList.add(new User("person", "男", 28));
userList.add(new User("user", "女", 23));
List<BulkOperation> bulkOperations = new ArrayList<>();
for (User user : userList) {
bulkOperations.add(BulkOperation.of(o -> o.index(r -> r.document(user))));
}
BulkResponse bulkResponse = elasticsearchClient.bulk(b -> b.index("user").operations(bulkOperations));
System.out.println(bulkResponse.errors());
}

/**
* 条件查询
*
* @throws IOException 异常
*/
@Test
void searchTest() throws IOException {
SearchResponse<User> search = elasticsearchClient.search(s -> s
.index("user")
.query(q -> q
.term(t -> t
.field("sex")
.value(v -> v.stringValue("男"))
))
.from(0)
.size(3)
.sort(f -> f.field(o -> o.field("age").order(SortOrder.Desc))), User.class);
HitsMetadata<User> hits = search.hits();
List<Hit<User>> hitsUser = hits.hits();
System.out.println(hits.total().value());
for (Hit<User> userHit : hitsUser) {
System.out.println(userHit.source());
}
}
}
阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  /**
* 根据页面传入数据去es中检索结果
*
* @param searchParam 搜索参数封装
* @return SearchResult 里面包含页面需要的所有信息
*/
@Override
public SearchResult search(SearchParam searchParam) {
//动态构建检索语句
try {
SearchRequest.Builder s = new SearchRequest.Builder();
// Function<SearchRequest.Builder, ObjectBuilder<SearchRequest>> fn = s -> {
//查询的索引
s.index(ElasticSearchConstant.PRODUCT_INDEX);
//查询条件
s.query(q -> q
.bool(bool -> {
if (StringUtils.hasText(searchParam.getKeyword())) {
bool.must(must -> must
.match(match -> match.field(ElasticSearchConstant.PRODUCT_INDEX).query(searchParam.getKeyword()))
);
}
bool.filter(filter -> {
if (searchParam.getCatalog3Id() != null) {
filter.term(term -> term
.field("catalogId")
.value(searchParam.getCatalog3Id()));
} else if (searchParam.getBrandId() != null && searchParam.getBrandId().size() > 0) {
List<FieldValue> collect = searchParam.getBrandId().stream().map(FieldValue::of).collect(Collectors.toList());
filter.terms(terms -> terms
.field("brandId")
.terms(termsValue -> termsValue
.value(collect)));
} else if (StringUtils.hasText(searchParam.getSkuPrice())) {
filter.range(range -> {
String skuPrice = searchParam.getSkuPrice();
String[] skuRange = skuPrice.split("_");
if (skuRange.length == 2) {
range.field("skuPrice").gte(JsonData.of(skuRange[0])).lte(JsonData.of(skuRange[1]));
} else if (skuRange.length == 1) {
if (skuPrice.startsWith("_")) {
range.field("skuPrice").lte(JsonData.of(skuRange[0]));
} else {
range.field("skuPrice").gte(JsonData.of(skuRange[0]));
}
}
return range;
});
} else if (searchParam.getAttrs() != null && searchParam.getAttrs().size() > 0) {
List<String> attrs = searchParam.getAttrs();
for (String attr : attrs) {
String[] attrString = attr.split("_");
filter.nested(nested -> nested
.scoreMode(ChildScoreMode.None)
.path("attrs")
.query(query -> query.bool(attrBool -> attrBool
.must(mustAttrs -> {
mustAttrs.term(termAttrId -> termAttrId
.field("attrs.attrId")
.value(attrString[0]));
mustAttrs.term(termAttrValue -> termAttrValue
.field("attrs.attrValue")
.value(attrString[1]));
return mustAttrs;
}
))
));
}
}
filter.term(term -> term
.field("hasStock")
.value(searchParam.getHasStock() == 1));
return filter;
});
return bool;
})
);

//排序
if (StringUtils.hasText(searchParam.getSort())) {
String[] sortString = searchParam.getSort().split("_");
s.sort(sort -> sort
.field(field -> field.field(sortString[0]).order(sortString[1].equalsIgnoreCase("asc") ? SortOrder.Asc : SortOrder.Desc)));
}
//分页 (起始-1) * 页面大小
s.from((searchParam.getPageNum() - 1) * ElasticSearchConstant.PRODUCT_PAGE_SIZE);
s.size(ElasticSearchConstant.PRODUCT_PAGE_SIZE);
//高亮
if (StringUtils.hasText(searchParam.getKeyword())) {
s.highlight(h -> h
.fields(searchParam.getKeyword(), highlight -> highlight
.preTags("<b style=-'color:red'>")
.postTags("</b>")));
}
// return s;
// };
SearchRequest searchRequest = s.build();
Object o = JSON.toJSON(searchRequest);
SearchResponse<SearchResult> search = elasticsearchClient.search(searchRequest,SearchResult.class);
String jsonString = JSON.toJSONString(searchParam);
System.out.println(jsonString);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

elasticsearch java api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

/**
* 注入一个elasticsearch clent客户端
*
* @return ElasticsearchClient 返回elasticsearch客户端
*/
@Bean
public ElasticsearchClient getElasticsearchClient() {
// Create the low-level client
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
return new ElasticsearchClient(transport);
}
阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
elasticsearch配置docker :
received plaintext http traffic on an https channel, closing connection Netty4HttpChannel
是因为开启了 ssl 认证。
在 ES/config/elasticsearch.yml 文件中把 xpack.security.http.ssl:enabled 设置成 false 即可

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12


先docker run

docker run -d \
--name elasticsearch \
--privileged=true \
-p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms128m -Xmx256m" \
elasticsearch:7.17.2

在复制文件:
docker cp elasticsearch:/usr/share/elasticsearch/config /mnt/d/Docker/docker_container_mount_data
docker cp elasticsearch:/usr/share/elasticsearch/data /mnt/d/Docker/docker_container_mount_data
docker cp elasticsearch:/usr/share/elasticsearch/plugins /mnt/d/Docker/docker_container_mount_data

最后docker run:
docker network create --subnet=172.20.0.0/24 elastic

docker run -d \
--name elasticsearch_one \
-p 9200:9200 \
-p 9300:9300 \
--ip=172.20.0.2 \
-v /mnt/d/Docker/docker_container_mount_data/elasticsearch/config:/usr/share/elasticsearch/config \
-v /mnt/d/Docker/docker_container_mount_data/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mnt/d/Docker/docker_container_mount_data/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
--privileged=true \
--net elastic \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
elasticsearch:7.17.2


-p 9200:9200 \
-p 9300:9300 \



-v /mnt/d/Docker/docker_container_mount_data/elasticsearch/config:/usr/share/elasticsearch/config \
-v /mnt/d/Docker/docker_container_mount_data/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mnt/d/Docker/docker_container_mount_data/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
创建kibana:

docker run \
--name kibana \
--net elastic \
--ip=172.20.0.3 \
-e "I18N_LOCALE=zh-CN" \
-p 5601:5601 \
-v /mnt/d/Docker/docker_container_mount_data/kibana/config:/usr/share/kibana/config \
-d kibana:7.17.2

docker cp
kibana:/usr/share/kibana/config
/mnt/d/Docker/docker_container_mount_data/kibana/config




nginx
docker run -d \
--net=elastic \
--ip=172.20.0.4 \
-p 80:80 \
-v /mnt/d/Docker/docker_container_mount_data/nginx/html:/usr/share/nginx/html \
-v /mnt/d/Docker/docker_container_mount_data/nginx/log/nginx:/var/log/nginx \
-v /mnt/d/Docker/docker_container_mount_data/nginx/etc/nginx:/etc/nginx \
--name=nginx \
nginx:stable

docker cp nginx:/usr/share/nginx/html /mnt/d/Docker/docker_container_mount_data/nginx
docker cp nginx:/var/log/nginx /mnt/d/Docker/docker_container_mount_data/nginx/log
docker cp nginx:/etc/nginx /mnt/d/Docker/docker_container_mount_data/nginx/etc


阅读全文 »

一、生成SSH密🔑

  1. WIn 进入当前用户目录下,创建.ssh文件夹
  2. Linux 进入用户家目录,创建.ssh文件夹

存在就不需要创建,(Win所有操作请在GItBash下执行)

执行下列命令:

  1. ssh-keygen -t rsa -C "GIT平台的用户邮箱"
    中间的rsa可以替换为其他密钥加密格式,具体需要查询GIT平台的支持情况(一下以GItee为例):

  1. 生成密钥过程:
    1. Enter file in which to save the key (/Users/baiyizhuo/.ssh/id_rsa):生成的证书名称(名称如果自定义需要在config中进行配置)
    2. Enter passphrase (empty for no passphrase):输入你的ssh证书密码
  2. 生成结束
1
2
-rw-------   1 baiyizhuo  staff  2602  7  6 17:55 id_rsa
-rw-r--r--   1 baiyizhuo  staff   571  7  6 17:55 id_rsa.pub
1. id_rsa:私钥
2. id_rsa.pub:公钥

二、配置Config文件

阅读全文 »