准备工作
新建一个空项目,专门存放我们的es代码模块
新建api测试module,配置项目环境为jdk1.8
初始化项目时导入es依赖
设置项目的es的pom依赖版本和本地一致
在properties的标签中添加<elasticsearch.version>7.6.1</elasticsearch.version>
配置es的配置类,注入到ioc容器中
新建ElasticSearchClientConfig类,增加@Configuration注解
注入bean
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2">package com.xiaoye.config;<br><br>import org.apache.http.HttpHost;<br>import org.elasticsearch.client.RestClient;<br>import org.elasticsearch.client.RestHighLevelClient;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br><br>/**<br> * @author 小也<br> * @create 2021/5/8 18:50<br> */<br></font><b style=""><font color="#f384ae">@Configuration</font></b><font color="#cfd0d2"><br>public class ElasticSearchClientConfig {<br><br> </font><b style=""><font color="#f1753f">@Bean<br> public RestHighLevelClient restHighLevelClient(){<br> return new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));<br> }</font></b><font color="#cfd0d2"><br><br>}</font><br></pre>
测试es高级客户端的api
创建索引
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void createIndex() throws IOException {<br> CreateIndexRequest request = new CreateIndexRequest("xiaoye");<br> CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);<br> System.out.println(response);<br> }</font><br></pre>
查看指定索引是否存在
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void indexIsExist() throws IOException {<br> GetIndexRequest request = new GetIndexRequest("xiaoye");<br> boolean response = client.indices().exists(request, RequestOptions.DEFAULT);<br> System.out.println(response);<br> }</font><br></pre>
删除指定索引
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void deleteIndex() throws IOException {<br> DeleteIndexRequest request = new DeleteIndexRequest("xiaoye");<br> AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);<br> System.out.println(response.isAcknowledged());<br> }</font><br></pre>
文档的CURD
导入fastjson依赖
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> <dependency><br> <groupId>com.alibaba</groupId><br> <artifactId>fastjson</artifactId> <br> <version>1.2.62</version><br> </dependency></font><br></pre>
先创建一个实体类
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2">package com.xiaoye.entity;<br><br>import lombok.AllArgsConstructor;<br>import lombok.Data;<br>import lombok.NoArgsConstructor;<br>import org.springframework.stereotype.Component; <br><br>/**<br> * @author 小也<br> * @create 2021/5/8 19:16<br> */<br>@Component // 注册到spring的容器中<br>@Data<br>@AllArgsConstructor<br>@NoArgsConstructor<br>public class User {<br> private String name;<br> private int age;<br>}</font><br></pre>
编写测试代码
添加记录
单一添加
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void addData() throws IOException {<br> //创建对象<br> User user = new User("小也", 20);<br><br> //获取索引对象<br> IndexRequest request = new IndexRequest("xiaoye");<br><br> //设置记录唯一标识已经超时时间<br> request.id("1");<br> request.timeout("1s");<br><br></font><b style=""><font color="#fdb813"> //把我们的对象记录以JSON格式放入请求中,es是以json格式存储记录的<br> request.source(JSON.toJSONString(user), XContentType.JSON);</font></b><font color="#cfd0d2"><br><br> //发送请求,获得返回值<br> IndexResponse response = </font><b style=""><font color="#f384ae">client.index</font></b><b style=""><font color="#f384ae">(request, RequestOptions.DEFAULT);</font></b><font color="#cfd0d2"><br><br> System.out.println(response.status());<br> System.out.println(response.toString());<br> }</font><br></pre>
批量添加
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void batchDate() throws IOException {<br></font><b style=""><font color="#f384ae"> //批量操作整合对象<br> BulkRequest request = new BulkRequest();</font></b><font color="#cfd0d2"><br> for (int i = 0; i < 10; i++) {<br> User user = new User("贾维斯" + i + "号", i);<br> request.add(new IndexRequest("xiaoye").id(String.valueOf(i)).source(JSON.toJSONString(user),XContentType.JSON));<br> }<br> BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);<br> //返回操作是否失败,返回false代表操作成功<br> System.out.println(response.hasFailures());<br> }</font><br></pre>
删除指定索引的记录
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void deleteDate() throws IOException {<br> DeleteRequest request = new DeleteRequest("xiaoye", "1");<br> DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);<br> System.out.println(response);<br> }</font><br></pre>
修改指定索引指定id的记录
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void updateDate() throws IOException {<br> UpdateRequest request = new UpdateRequest("xiaoye", "1");<br> User user = new User("xiaoye", 22);<br> request.doc(JSON.toJSONString(user),XContentType.JSON);<br> UpdateResponse response = client.update(request,RequestOptions.DEFAULT);<br> System.out.println(response.status());<br> }</font><br></pre>
查询指定索引的记录
简单查询
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void getDate() throws IOException {<br> GetRequest request = new GetRequest("xiaoye", "1");<br> GetResponse response = client.get(request,RequestOptions.DEFAULT);<br> System.out.println(response.getSource().toString());<br> }</font><br></pre>
条件查询
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void complexSearch() throws IOException {<br> SearchRequest index = new SearchRequest("xiaoye");<br><br> //构造整合者<br> SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();<br><br> //条件查询构造<br> MatchQueryBuilder matchAllQueryBuilder = QueryBuilders.matchQuery("name", "贾维斯");<br> searchSourceBuilder.query(matchAllQueryBuilder);<br>// TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "贾维斯1号");<br>// searchSourceBuilder.query(termQuery);<br><br> //构造高亮,字段为name<br> HighlightBuilder highlightBuilder = new HighlightBuilder();<br> highlightBuilder.field("name");<br> searchSourceBuilder.highlighter(highlightBuilder);<br><br> //构造整合者放入请求中<br> index.source(searchSourceBuilder);<br><br> SearchResponse response = client.search(index, RequestOptions.DEFAULT);<br><br> System.out.println(JSON.toJSONString(response.getHits()));<br><br> System.out.println("===============================");<br><br> for (SearchHit hit : response.getHits().getHits()) {<br> System.out.println(hit.getSourceAsString());<br><br> // 打印出高亮片段<br> HighlightField highlightField = hit.getHighlightFields().get("name");<br> for (Text fragment : highlightField.getFragments()) {<br> System.out.println(fragment.toString());<br> }<br> }<br> }</font><br></pre>