使用 ektorp 在 couchdb 中应用 reduce 后获取分页视图数据

Fetch Paginated view data after applying reduce in couchdb with ektorp

您好,我想通过应用 reduce 和分页从 couchdb-view 获取数据。

我的观点是将 reduce 函数的结果作为复杂的键,如下所示

{"rows":[
         {"key":{"attribute":"Attribute1"},"value":20},
         {"key":{"attribute":"Attribute2"},"value":1}
         {"key":{"attribute":"Attribute3"},"value":1}
]}

我正在尝试使用 ektorp 从 couchdb 获取数据,检查以下代码

PageRequest pageRequest = PageRequest.firstPage(10);
ViewQuery query = new ViewQuery()
            .designDocId("_design/medesign")
            .viewName("viewname")
            .includeDocs(false)
            .reduce(true)
            .group(true);
Page<ViewResult> rs1 = db.queryForPage(query, pageRequest, ViewResult.class);

rs1.forEach(v -> {
            System.out.println(v.getSize());
        });

我收到以下错误

org.ektorp.DbAccessException: com.fasterxml.jackson.databind.JsonMappingException: 
Can not construct instance of org.ektorp.ViewResult: 
no int/Int-argument constructor/factory method to deserialize from Number value (20)
at [Source: N/A; line: -1, column: -1]

如果您想要分页缩减数据,CouchDB 不会提供分页详细信息。

带有分页包含文档的请求
group=false & reduce=false & include_docs=true

URL : http://localhost:5984/dn_anme/_design/design_name/_view/viewname?include_docs=true&reduce=false&skip=0&group=false&limit=2

回复:

{
   "total_rows":81,
   "offset":0,
   "rows":[
      {
         "id":"906a74b8019716f1240a7117580ec172",
         "key":{
            "attribute":"BuildArea"
         },
         "value":1,
         "doc":{
            "_id":"906a74b8019716f1240a7117580ec172",
            "_rev":"3-7e0a1da0c2260040f8a9787636385785",
            "country":"POL",            
            "recordStatus":"MATCHED"
         }
      },
      {
         "id":"906a74b8019716f1240a7117580eaefb",
         "key":{
            "attribute":"Area",
         },
         "value":1,
         "doc":{
            "_id":"906a74b8019716f1240a7117580eaefb",
            "_rev":"3-165ea3a3ed07ad8cce1f3e095cd476b5",
            "country":"POL",
            "recordStatus":"MATCHED"
         }
      }
   ]
}

请求减少 group=true& reduce=true& include_docs=false

URL : http://localhost:5984/dn_anme/_design/design_name/_view/viewname?include_docs=false&reduce=true&group=true&limit=2

回复:

{
   "rows":[
      {
         "key":[
            "BuildArea"
         ],
         "value":1
      },
      {
         "key":[
            "Area"
         ],
         "value":1
      }
   ]
}

两者的区别:
带有分页包含文档的请求提供页面数据 {"total_rows":81, "offset":0, rows":[{...},{...}]} 请求减少给 {"rows":[{...},{..}]}

如何获取分页归约数据:

Step 1: Request rows_per_page + 1 rows from the view
Step 2: if in response one extra records than page_size then there are more records
Step 3: calculate and update skip value and got to step 1 for next page
Note: adding skip is not good option for lots of records instead of that find start key and add start key, its good for better perforamance