(7). 查询 ⭐⭐⭐⭐
在大公司里面 索引库都是放在常量中的
package com.jsxs.utils; /** * @Author Jsxs * @Date 2023/6/27 20:06 * @PackageName:com.jsxs.utils * @ClassName: ESconst * @Description: TODO * @Version 1.0 */ public class EsConst { // 我们在工作的时候 尽量把常用的数据设置为常量。 public static final String INDEX="kuang_index"; }
// 查询 @Test void testSearch() throws IOException { // 1. 指定去哪个索引库中查找数据 ⭐ SearchRequest request = new SearchRequest(EsConst.INDEX); //----------------------------------------------------------------- // 2. 构建搜索的条件 ⭐⭐ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 精确匹配 - 我们可以使用QueryBuilders工具类来实现.. TermQueryBuilder query = QueryBuilders.termQuery("name", "jsxs1"); // 匹配所有 - MatchAllQueryBuilder query1 = QueryBuilders.matchAllQuery(); // 查询 sourceBuilder.query(query1); // 时间过时操作 sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //----------------------------------------------------------------- //3. 放入请求中 ⭐⭐⭐ request.source(sourceBuilder); // 4. 客户端进行请求 ⭐⭐⭐⭐ SearchResponse search = restHighLevelClient.search(request, DEFAULT); System.out.println(JSON.toJSONString(search.getHits())); System.out.println("-----------------"); // 打印所有的数据 ⭐⭐⭐⭐⭐ for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsMap()); } }
7.全部的代码
package com.jsxs; import com.alibaba.fastjson2.JSON; import com.jsxs.pojo.User; import com.jsxs.utils.EsConst; 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.DeleteRequest; 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.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexResponse; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import static org.elasticsearch.client.RequestOptions.*; @SpringBootTest class JsxsEsApi01ApplicationTests { @Resource private RestHighLevelClient restHighLevelClient; @Test void contextLoads() { } // 测试索引的创建 Request @Test void testCreateIndex() throws IOException { // 1.创建一个索引叫做 kuang_index的索引对象请求 CreateIndexRequest request = new CreateIndexRequest("kuang_index"); // 2.执行创建索引的请求 获得响应 CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, DEFAULT); System.out.println(createIndexResponse); } // 测试获取索引的请求 @Test void testExistIndex() throws IOException { // 获取一个索引名叫做kuang_index的索引库 GetIndexRequest request = new GetIndexRequest("kuang_index"); // 2.判断这个索引库是否存在? boolean exists = restHighLevelClient.indices().exists(request, DEFAULT); System.out.println(exists); } // 删除指定的索引 @Test void testDeleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("jsxs"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, DEFAULT); System.out.println(delete.isAcknowledged()); } // 测试添加文档 @Test void testAddDocument() throws IOException { // 1.创建添加的对象 User user = new User("吉士先生", 3); // 2.创建索引请求操作 IndexRequest request = new IndexRequest("kuang_index"); // 3.规则 put 添加文档 // /索引/表/id request.id("1"); // 设置索引的id request.timeout(TimeValue.timeValueSeconds(1)); //设置过期时间为1秒 // 4.将我们的数据放入请求 json request.source(JSON.toJSONString(user), XContentType.JSON); // 5.客户端发送请求 IndexResponse index = restHighLevelClient.index(request, DEFAULT); // 6. System.out.println(index.toString()); System.out.println(index.status()); } // 获取文档,判断是否存在 get /索引/doc/id @Test void testIsExists() throws IOException { GetRequest getRequest = new GetRequest("kuang_index", "1"); // 不获取返回的 _source 的上下文了。 getRequest.fetchSourceContext(new FetchSourceContext(false)); getRequest.storedFields("_none_"); // 客户端判断文档是否存在 boolean flag = restHighLevelClient.exists(getRequest, DEFAULT); System.out.println(flag); } // 获取文档 get /索引/doc/id @Test void testGetValue() throws IOException { GetRequest getRequest = new GetRequest("kuang_index", "1"); // 1.客户端请求获得数据 GetResponse getResponse = restHighLevelClient.get(getRequest, DEFAULT); // 2.打印我们的信息 System.out.println(getResponse.getSourceAsString()); } // 更新文档数据. @Test void testUpdateValue() throws IOException { // 1.更新索引为kuang_index的id为1的文档 UpdateRequest request = new UpdateRequest("kuang_index", "1"); // 2.设置超时的时间 request.timeout("1s"); User user = new User("aaaa", 111); // 3.第一个参数是: 转换为JSON对象,第二个参数是告诉别人我们是一个JSON类型 request.doc(JSON.toJSONString(user),XContentType.JSON); // 4.客户端请求修改得数据 UpdateResponse update = restHighLevelClient.update(request, DEFAULT); // 5.打印我们的信息 System.out.println(update.toString()); } // 删除文档的记录 @Test void testDeleteRequest() throws IOException { DeleteRequest request = new DeleteRequest("kuang_index", "1"); // 1.假如请求超过1秒的话,那么就不再链接 request.timeout("1s"); // 2.请求客户端 DeleteResponse delete = restHighLevelClient.delete(request, DEFAULT); // 3.打印信息 System.out.println(delete.toString()); } // 批量添加的操作 @Test void testBulkRequest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); // 1.设置超时的时间 bulkRequest.timeout("10s"); // 2.利用链表存储数据 ArrayList<User> users = new ArrayList<>(); users.add(new User("jsxs1",1)); users.add(new User("jsxs2",2)); users.add(new User("jsxs3",3)); users.add(new User("jsxs4",4)); users.add(new User("jsxs5",5)); users.add(new User("jsxs6",6)); users.add(new User("jsxs7",7)); users.add(new User("jsxs8",8)); users.add(new User("jsxs9",9)); users.add(new User("jsxs10",10)); // 3.批量添加的操作 for (int i = 0; i < users.size(); i++) { bulkRequest.add(new IndexRequest("kuang_index") .id(""+i+1) .source(JSON.toJSONString(users.get(i)),XContentType.JSON) ); } // 4.客户端请求 BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, DEFAULT); System.out.println(bulk.toString()); } // 查询 @Test void testSearch() throws IOException { // 1. 指定去哪个索引库中查找数据 ⭐ SearchRequest request = new SearchRequest(EsConst.INDEX); //----------------------------------------------------------------- // 2. 构建搜索的条件 ⭐⭐ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 精确匹配 - 我们可以使用QueryBuilders工具类来实现.. TermQueryBuilder query = QueryBuilders.termQuery("name", "jsxs1"); // 匹配所有 - MatchAllQueryBuilder query1 = QueryBuilders.matchAllQuery(); // 查询 sourceBuilder.query(query1); // 时间过时操作 sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //----------------------------------------------------------------- //3. 放入请求中 ⭐⭐⭐ request.source(sourceBuilder); // 4. 客户端进行请求 ⭐⭐⭐⭐ SearchResponse search = restHighLevelClient.search(request, DEFAULT); System.out.println(JSON.toJSONString(search.getHits())); System.out.println("-----------------"); // 打印所有的数据 ⭐⭐⭐⭐⭐ for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsMap()); } } }