开发者学堂课程【ElasticSearch 入门精讲:分页 fromsize】学习笔记,与课程紧密连接,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/631/detail/9994
分页 fromsize
ES 查询详解之分页 from/size
核心代码:
setFrom(0).setSize(1)
/** *测试:检索类型,以及分页检索 @Test public void testsearchTypeAndsplitpage() { //案例1 :检索 bi gdata 索引库中, product type 中的字段 name 为 hive 的索引信息。 学习知识点:检素类 型,分页检索 SearchResponse response = client. prepareSearch(indices) //指定所关注的 type . setTypes (TYPE_ ,PRODUCT) //设定 searchType . setSearchType (SearchType . DFS_ QUERY _THEN_ FETCH) //设置分页(查询第一页 ,每页显示2条记录) ,公式:开始索引= ( 页码-1) *pagesize . setFrom((1 - 1) * 2) . setsize(2) //设置查询的条件 . setQuery (QueryBuilders. termQuery("name", "hive")) . get(); //从结果中显示所有满足条件的记录 SearchHits hits = response. getHits(); for (SearchHit hit : hits) { logger. info("检索到的 document 信息是: " + hit. getSourceAsstring()); } } @Test public void testWildcardQuery(){ //查询 SearchResponse response = client.prepareSearch(INDEX) .setTypes( TYPE) //.setQuery(QueryBuilders. wildcardQuery("name", "f*m?")) .setQuery(QueryBuilders. wildcardQuery name: "name", query:"s*")) .get(); //显示检索到的信息 SearchHits hits = response.getHits(); for (SearchHit hit : hits) { System. out.println(hit.getSourceAsString)); } } @Test public void testSplitPage(){ int currentPage = 2; int pageSize = 2; int startIndex = (currentPage- 1) *pageSize //分页查询 SearchResponse response = client.prepareSearch(INDEX) .setTypes( TYPE) .setQuery(QueryBuilders. wildcardQuery name: "name", query: "s*")) //设置分页信息 .setFrom(fromIndex) .setSize(pageSize) .get(); |