2.4.3.查看文档
/*** 查看文档* @throws IOException*/@Testvoid queryDoc() throws IOException {//创建请求对象GetRequest,并指定idGetRequest request = new GetRequest("lagou","item","1");//执行查询GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);System.out.println(response);String source = response.getSourceAsString();Product product = gson.fromJson(source, Product.class);System.out.println(product);}
2.4.4.修改文档
/*** 修改文档* @throws IOException*/@Testvoid updateDoc() throws IOException {Product product = new Product();product.setBrand("华为");product.setCategory("手机");product.setId(1L);product.setImages("http://image.huawei.com/1.jpg");product.setTitle("华为P50就是棒");product.setPrice(88.99d);//创建请求对象GetRequest,并指定idIndexRequest request = new IndexRequest("lagou","item",product.getId().toString());String source = gson.toJson(product);request.source(source,XContentType.JSON);//执行查询IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);System.out.println(response);}
2.4.5.删除文档
/*** 删除文档* @throws IOException*/@Testvoid deleteDoc() throws IOException {//创建请求对象GetRequest,并指定idDeleteRequest request = new DeleteRequest("lagou","item","1");//执行查询DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);System.out.println(response);}
2.5.搜索数据 2.5.1.查询所有
/*** 匹配所有* @throws IOException*/@Testvoid matchAll() throws IOException {//创建搜索对象SearchRequest request = new SearchRequest();//查询构建工具SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//添加查询条件,通过QueryBuilders获取各种查询sourceBuilder.query(QueryBuilders.matchAllQuery());request.source(sourceBuilder);//执行查询SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);//获取查询结果SearchHits hits = search.getHits();//获取文件数组SearchHit[] searchHits = hits.getHits();List productList = new ArrayList<>();for (SearchHit searchHit : searchHits) {String source = searchHit.getSourceAsString();Product product = gson.fromJson(source, Product.class);productList.add(product);}System.out.println(productList);}
注意,上面的代码中,搜索条件是通过 .query(.()) 来添加的 。这个 query() 方法接受的参数是:接口类型 。
这个接口提供了很多实现类,分别对应我们在之前中学习的不同类型的查询,例如:term查询、match 查询、range查询、查询等,如图:
因此,我们如果要使用各种不同查询,其实仅仅是传递给 .query() 方法的参数不同而 已 。而这些实现类不需要我们去 new,官方提供了工厂帮我们构建各种实现类:
2.5.2.关键字搜索match
封装基础查询方法:
/*** 基础查询方法*//查询构建工具*SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();*//添加查询条件,通过QueryBuilders获取各种查询*sourceBuilder.query(QueryBuilders.matchAllQuery());*/void basicQuery(SearchSourceBuilder sourceBuilder) throws IOException {//创建搜索对象SearchRequest request = new SearchRequest();request.source(sourceBuilder);//执行查询SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);//获取查询结果SearchHits hits = search.getHits();//获取文件数组SearchHit[] searchHits = hits.getHits();List productList = new ArrayList<>();for (SearchHit searchHit : searchHits) {String source = searchHit.getSourceAsString();Product product = gson.fromJson(source, Product.class);productList.add(product);}System.out.println(productList);}
- foxmail-qq代发 python邮件入门,通过互联网服务器发送邮件
- 四 网络编程懒人入门:快速理解TCP和UDP的差异
- 五 网络编程懒人入门:快速理解为什么说UDP有时比TCP更有优势
- webpack 从入门到放弃!
- 知识图谱入门 【三】- 知识抽取
- python入门速通基础
- 推荐|机器学习入门方法和资料合集
- 1923: 【入门】墨汁
- Pr 入门教程:如何添加焦点拉动效果?
- Arduino基础入门之十 红外遥控1位数码管的显示