在进行查询之前要先给ll索引中插入数据:
POST /ll/product/1 { "id":1, "title": "小米手机Mix", "category": "手机", "brand": "小米", "price": 2899.00, "images": "http://image.ll.com/12479122.jpg" } POST /ll/product/2 { "id":2, "title": "坚果手机R1", "category": "手机", "brand": "锤子", "price": 3699.00, "images": "http://image.ll.com/12479122.jpg" } POST /ll/product/3 { "id":3, "title": "华为META20", "category": "手机", "brand": "华为", "price": 4499.00, "images": "http://image.ll.com/12479122.jpg" } POST /ll/product/4 { "id":4, "title": "小米Pro", "category": "手机", "brand": "小米", "price": 4299.00, "images": "http://image.ll.com/12479122.jpg" } POST /ll/product/5 { "id":5, "title": "荣耀V20", "category": "手机", "brand": "华为", "price": 2799.00, "images": "http://image.ll.com/12479122.jpg" }
将以上代码复制到kibana的控制台上运行
运行完后可以到打开head插件查看信息
数据准备完成后我们就可以开始查询了
1.查询所有索引的数据 match_all
@Test//搜索数据 public void search() throws IOException{ SearchRequest request = new SearchRequest();//搜索对象 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//构建工具 //绑定类型 match_all,math,bool,term,fuzzy,filter sourceBuilder.query(QueryBuilders.matchAllQuery()); //执行查询操作 request.source(sourceBuilder); //执行查询搜索 SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT); //解析响应的结果数据 SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit : hits) { String json = hit.getSourceAsString();//转为json格式 Producter producter = gson.fromJson(json, Producter.class);//转为java对象 System.err.println(producter); } }
2.关键字查询 match
@Test//matchquery public void matchQuery()throws Exception{ SearchRequest request = new SearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchQuery("title", "小米")); request.source(sourceBuilder); SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit : hits) { String json = hit.getSourceAsString();//转为json格式 Producter producter = gson.fromJson(json, Producter.class);//转为java对象 System.err.println(producter); } }
通过上述两个查询我们发现,查询数据的时候会有很多冗余的部分,所以我们可以将这一部分代码进行封装,在查询的时候调用这个方法,下面的查询我都会调用封装的方法
public void basicQuery(SearchSourceBuilder sourceBuilder) throws Exception{ SearchRequest request = new SearchRequest(); request.source(sourceBuilder); SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT); //解析响应的结果数据 SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit : hits) { String json = hit.getSourceAsString();//转为json格式 Producter producter = gson.fromJson(json, Producter.class);//转为java对象 System.err.println(producter); } }
3.范围查询 range
@Test//范围查询 public void rangeQuery() throws Exception{ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.rangeQuery("price").gte(3000).lt(4300)); basicQuery(sourceBuilder); }
4.过滤查询 source
@Test//过滤查询 public void sourceQuery() throws Exception{ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchAllQuery()); //添加过滤条件 sourceBuilder.fetchSource(new String[]{"id","title","price"},null); basicQuery(sourceBuilder); }
5.排序 sort
@Test//排序 order public void orderQuery() throws Exception{ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchAllQuery()); //添加排序条件 sourceBuilder.sort("price", SortOrder.DESC);//desc降序 asc升序 basicQuery(sourceBuilder); }
6. 分页 page
@Test//分页 from当前页第一条数据的索引 size当前页的大小 public void pageQuery() throws Exception{ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchAllQuery()); //添加分页条件 int page = 2;//第二页的数据 int size = 3;//每页展示三条数据 sourceBuilder.from((page-1)*size); sourceBuilder.size(size); basicQuery(sourceBuilder); }