方法

通过http 9200访问
使用ES-Rest-Clients

单独创建模块

在这里插入图片描述
在这里插入图片描述

添加maven

1
2
3
4
5
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>

在这里插入图片描述

编写配置文件

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
package com.jhj.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @Description GulimallElasticSearchConfig
* @Author jhj
* @Date 2022/6/1310:50
**/
@Configuration
public class GulimallElasticSearchConfig {

@Value("${es.ip}") //在yml中配置
private String ip;
@Value("${es.port}") //在yml中配置
private Integer port;

public static final RequestOptions COMMON_OPTIONS;

static {

RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer" + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}

@Bean
public RestHighLevelClient esRestClient() {

RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost(this.ip, this.port, "http"))
);
return client;
}
}

使用

详细可以参考官方文档

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
package com.jhj.gulimall.search;

import com.alibaba.fastjson.JSON;
import com.jhj.gulimall.search.config.GulimallElasticSearchConfig;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallSearchApplicationTests {


class User{

private String username;
private String gender;
private Integer age;

public void setUsername(String username) {

this.username = username;
}

public void setGender(String gender) {

this.gender = gender;
}

public void setAge(Integer age) {

this.age = age;
}
}
@Resource
private RestHighLevelClient restHighLevelClient;
@Test
/**
* 保存 /更新
*/
public void saveTest() throws IOException {

IndexRequest users = new IndexRequest("users");
//不设置会自动生成
users.id("1");
//1. 键值对
// users.source("userName","zhangsan","age",18);
//2. 对象 json转 推荐使用
User user = new User();
String s = JSON.toJSONString(user);
user.setUsername("靳豪杰");
user.setAge(22);
user.setGender("男");
users.source(s, XContentType.JSON);
//执行操作
IndexResponse index = restHighLevelClient.index(users, GulimallElasticSearchConfig.COMMON_OPTIONS);
System.out.println(index);
}

@Test
/**
* 搜索
*/
public void searchTest() throws IOException {

//创建请求
SearchRequest searchRequest = new SearchRequest();
//指定索引 库
searchRequest.indices("users");
//指定DSL 检索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchRequest.source(searchSourceBuilder);
//执行
restHighLevelClient.search(searchRequest,GulimallElasticSearchConfig.COMMON_OPTIONS);
}

}

作者声明

1
如有问题,欢迎指正!