1. elasticSearch配置类
复制代码
1
2
3
4
5
6
7
8
9@Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1",9200,"http") ) ); return client; }
2. 测试es基本操作命令
复制代码
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@Autowired private RestHighLevelClient restHighLevelClient; /** * 创建索引 * @throws IOException */ @Test void testCreateIndex() throws IOException { //1、创建索引请求 CreateIndexRequest request = new CreateIndexRequest("test_index"); // 2、客户端执行请求 CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse); } /** * 获取索引 * @throws IOException */ @Test void testExistIndex() throws IOException { GetIndexRequest request = new GetIndexRequest("test_index"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } /** * 删除索引 * @throws IOException */ @Test void testDeleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("test_index"); AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(response); } /** * 添加文档 * @throws IOException */ @Test void testAddDocument() throws IOException { UserEntity userEntity = new UserEntity("小米",3); //创建请求 IndexRequest request = new IndexRequest("test_index"); // 规则 put /kuang_index/_doc/1 request.id("1"); request.timeout(TimeValue.timeValueSeconds(1)); request.timeout("1s"); //添加数据 request.source(JSON.toJSONString(userEntity), XContentType.JSON); //客户端发送请求 IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response); } /** * 获取文档 */ @Test public void indexGetDocument() throws IOException { GetRequest getRequest = new GetRequest("test_index","1"); GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(getResponse.getSourceAsString()); } /** * 更新文档 */ @Test public void indexUpdateDocument() throws IOException { UpdateRequest updateRequest = new UpdateRequest("test_index","1"); updateRequest.timeout("1s"); UserEntity userEntity = new UserEntity("小仓",20); updateRequest.doc(JSON.toJSONString(userEntity),XContentType.JSON); UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT); System.out.println(updateResponse.status()); } /** * 删除文档 */ @Test public void indexDeleteDocument() throws IOException { DeleteRequest deleteRequest = new DeleteRequest("test_index", "1"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(deleteResponse.status()); } /** * 批量插入数据 */ @Test public void addList() throws IOException { BulkRequest bulkRequest = new BulkRequest(); ArrayList<UserEntity> list = new ArrayList<>(); list.add(new UserEntity("xiaoming",19)); list.add(new UserEntity("xiaoming2",16)); list.add(new UserEntity("xiaoming3",12)); list.add(new UserEntity("xiaoming4",29)); list.add(new UserEntity("xiaoming5",14)); list.add(new UserEntity("xiaoming6",17)); //批量请求 for (int i = 0; i < list.size(); i++) { bulkRequest.add( new IndexRequest("test1") .id(""+(i+1)) .source(JSON.toJSONString(list.get(i)),XContentType.JSON) ); BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT); } } /** * 查询数据 */ @Test public void searchList() throws IOException { SearchRequest searchRequest = new SearchRequest("test1"); //构建搜索条件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.highlighter(); //查询条件 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "xiaoming"); sourceBuilder.query(termQueryBuilder); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(searchResponse.getHits())); System.out.println("==========================="); for (SearchHit documentFields : searchResponse.getHits().getHits()) { System.out.println(documentFields.getSourceAsString()); } }
最后
以上就是重要黄豆最近收集整理的关于elasticsearch学习四:es基本操作命令的全部内容,更多相关elasticsearch学习四内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复