一、ElasticSearch的介绍
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。
二、ElasticSearch的安装与使用
1、在安装ElasticSearch之前要在电脑安装node.js的的环境
1.1、node.js的安装步骤
下载node.js:下载地址
开始安装:
双击下载下来的.msi文件,开始安装node.js
现已安装完成,以下查看是否安装成功
在键盘按下【win+r】键,输入cmd
打开安装完成后的目录如下
新版的node.js自带npm,安装node.js时会一起安装,npm的作用就是对node.js依赖的包进行管理,也可以理解为用来安装/卸载node.js需要装的东西
node.js的环境配置
1.在安装的目录下创建两个文件夹为node_cache,node_global。cache为缓存路径,global是全局安装,这里在安装目录下创建是因为,在以后执行npm install express [-g]时,会将安装的模板安装到[C:Users用户名AppDataRoamingnpm]路径中,会占用c盘空间
创建完两个空文件夹之后,打开cmd命令窗口,输入
1
2
3npm config set prefix "D:安装路径node_global" npm config set cache "D:安装路径node_cache"
设置好以后打开我的电脑-右键-属性-高级系统设置-高级-环境变量
在系统变量下新建【node_path】输入:D:javaloadnodenode_globalnode_modules
在用户变量下的path修改默认路径为D:javaloadnodenode_global
测试:配置完成后安装midule进行测试
1.2、node.js安装完成后安装ElasticSearch
ElasticSearch官网下载:下载地址
官网下载特别的慢可在百度云下载:
链接:百度云下载
提取码:0vqh
----下载完成后,对文件进行解压
----打开到elasticsearch下的bin目录
双击elasticearch.bat
这样显示安装完成,可以访问http://localhost:9200/这样就是安装成功
1.3安装ElasticSearch-head插件
先安装grunt
打开cmd输入npm install -g grunt-cli就可以了
查看版本号grunt -version
安装完成后,进入elasticSearch-head文件夹下,输入cmd执行命令:npm install进行安装,此处安装速度可能会很慢
安装完成以后执行npm run start 或者grunt server启动head插件,这样就安装成功了,也可以访问localhost:9100
修改es.yml中的参数
新增参数
1
2
3
4
5# 增加新的参数,这样head插件可以访问es http.cors.enabled: true http.cors.allow-origin: "*" @注意,设置参数的时候:后面要有空格!
重新启动es访问:http://localhost:9100就好了
1.4、安装ik分词器
将ik分词器解压在es的安装目录下的plugins就好了
1.5、安装kibana
解压kibana,进入config目录下,打开kibana.yml文件
在里面加上下面两句就好了
1
2
3i18n.locale: "zh-CN" elasticsearch.hosts: ["http://localhost:9200"]
启动Kibana,在kibana下的bin目录输入cmd,在输入.kibana.bat就可以启动成功了
访问地址http://localhost:5601
三、ElasticSearch的增删改查demo
1
2**1、创建本地连接ElasticSearch**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package com.es.config; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ElasticSearchClientConfig { @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder( new HttpHost("127.0.0.1",9200,"http"))); return client; } }
1
2二、创建pojo对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.es.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private Integer age; }
三、创建测试类
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256package com.es; import com.alibaba.fastjson.JSON; import com.es.pojo.User; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryAction; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** * es高级客户端测试 */ @SpringBootTest class EsDemoApplicationTests { @Autowired @Qualifier("restHighLevelClient") RestHighLevelClient client; @Test void contextLoads() { } //测试索引的创建 @Test void testCreateIndex() throws IOException { //创建索引请求 CreateIndexRequest request = new CreateIndexRequest("xu_index"); //执行请求 client.indices().create(request, RequestOptions.DEFAULT); System.out.println("123"); } //测试获取索引 @Test void testExistIndex() throws IOException { GetIndexRequest request = new GetIndexRequest("xu_index"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } //删除库 @Test void testDelete() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("xu"); //删除 AcknowledgedResponse delete = client.indices().delete(request,RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } //测试添加文档 @Test void testAddDocument() throws IOException { User user = new User("张三",17); IndexRequest request = new IndexRequest("xu_index"); request.id("1"); request.timeout("1s"); //将数据放入请求 request.source(JSON.toJSONString(user), XContentType.JSON); //客户端发送请求 IndexResponse indexResponse=client.index(request, RequestOptions.DEFAULT); System.out.println(indexResponse.toString()); System.out.println(indexResponse.status());//对应我们的命令返回的状态 } //获取文档,判断是否存在 @Test void testExists() throws IOException { GetRequest request = new GetRequest("xu_index","1"); //不获取返回的_soure的上下文 request.fetchSourceContext(new FetchSourceContext(false)); request.storedFields("_none_"); boolean exists = client.exists(request, RequestOptions.DEFAULT); System.out.println(exists); } //获取文档信息 @Test void testGetDocument() throws IOException { GetRequest request = new GetRequest("xu_index","1"); GetResponse response = client.get(request, RequestOptions.DEFAULT); System.out.println(response.getSourceAsString());//打印文档的内容 System.out.println(response); } //更新文档信息 @Test void testUpdate() throws IOException { UpdateRequest request = new UpdateRequest("xu_index","1"); request.timeout("1s"); User user = new User("李四",19); request.doc(JSON.toJSONString(user),XContentType.JSON); UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT); System.out.println(updateResponse.status()); } //删除文档记录 @Test void testDocDelete() throws IOException { /* DeleteResponse response = client.prepareDelete("company", "employee", "1").get(); System.out.println(response.getResult());*/ /*DeleteRequest request = new DeleteRequest("xu_index","2"); request.timeout("1s"); DeleteResponse deleteResponse = client.delete(request,RequestOptions.DEFAULT); System.out.println(deleteResponse.status());*/ } //批量插入数据 @Test void testBulkReuest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); List<User> userList = new ArrayList<>(); userList.add(new User("张三0", 18)); userList.add(new User("张三1", 18)); userList.add(new User("张三2", 18)); userList.add(new User("张三3", 18)); userList.add(new User("张三4", 18)); userList.add(new User("张三5", 18)); userList.add(new User("张三6", 18)); userList.add(new User("张三7", 18)); userList.add(new User("张三8", 18)); System.out.println(userList); for ( int i = 0;i<userList.size();i++){ bulkRequest.add( new IndexRequest("test2").id(""+(i+1)) .source(JSON.toJSONString(userList.get(i)),XContentType.JSON) ); } BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulkResponse.hasFailures());//判断是否失败 } //查询 @Test void testSearch() throws IOException { SearchRequest searchRequest = new SearchRequest("xu_index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //searchSourceBuilder.query(QueryBuilders.matchAllQuery());//查询所有 searchSourceBuilder.query(QueryBuilders.multiMatchQuery("0", "name"));//匹配查询 searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(searchResponse.getHits())); System.out.println("=============================="); System.out.println("总条数"+searchResponse.getHits().getTotalHits().value); for (SearchHit DFile: searchResponse.getHits().getHits() ) { System.out.println(DFile.getSourceAsMap()); } } //分页查询,排序 @Test void test1Search() throws IOException { //指定索引库 SearchRequest searchRequest = new SearchRequest("xu_index"); //构造查询对象 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //添加分页条件,从第几个开始,返回几个 //searchSourceBuilder.from(0).size(3); searchSourceBuilder.query(QueryBuilders.matchAllQuery());//查询所有 //searchSourceBuilder.query(QueryBuilders.multiMatchQuery("张三", "name"));//匹配查询 //按照score排序(默认倒序) //searchSourceBuilder.sort(SortBuilders.scoreSort().order(SortOrder.ASC)); //根据id排序(倒序) searchSourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.DESC)); //设置执行时间 searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //执行请求 searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(searchResponse.getHits())); System.out.println("=============================="); System.out.println("总条数"+searchResponse.getHits().getTotalHits().value); for (SearchHit DFile: searchResponse.getHits().getHits() ) { System.out.println(DFile.getSourceAsMap()); } } //批量增删改 @Test public void testCUD() throws IOException { //初始化 BulkRequest bulkRequest = new BulkRequest(); //批量增加 List<User> userList = new ArrayList<>(); userList.add(new User("张三0", 18)); userList.add(new User("张三1", 18)); userList.add(new User("张三2", 18)); userList.add(new User("张三3", 18)); userList.add(new User("张三4", 18)); userList.add(new User("张三5", 18)); userList.add(new User("张三6", 18)); userList.add(new User("张三7", 18)); userList.add(new User("张三8", 18)); System.out.println(userList); for ( int i = 0;i<userList.size();i++){ bulkRequest.add( new IndexRequest("test2").id(""+(i+1)) .source(JSON.toJSONString(userList.get(i)),XContentType.JSON) ); } BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulkResponse.hasFailures());//判断是否失败 } // }
最后
以上就是幸福狗最近收集整理的关于ElasticSearch的介绍和Windows下的安装与使用以及增删改查操作的全部内容,更多相关ElasticSearch内容请搜索靠谱客的其他文章。
发表评论 取消回复