为什么REST和Java中的视图结果不同?例如从REST中它给出了它应该返回的确切结果,但是从Java它将返回特定键的整个文档。 我有这个文件:
{
"doctype": "people",
"id": "person0",
"name": "Kasidit Treweek",
"homepage": "http://www.cohera.com/~Treweek",
"creditcard": "9941 9701 2489 4716",
"profile": {
"income": 20186.59,
"interest": [
{
"category": "category251"
}
],
"education": "Graduate School",
"business": "No"
}
}
当我在视图中使用地图功能时:
function (doc, meta) {
if( (doc.doctype && doc.doctype=="people") && doc.id=="person0"){
emit(doc.id,doc.name);
}
}
REST的结果是:
{"total_rows":1,"rows":[
{"id":"person0","key":"person0","value":"Kasidit Treweek"}
]
}
但是从Java开始,它将返回整个文档,其中包含键“person0”,如
{"person0":{"doctype": "people",.....}}
这是我简单实现的java代码:
ViewResponse response = client.query(view, query);
java.util.Map<String, Object> map = response.getMap();
答案 0 :(得分:1)
这是因为你正在使用ViewResponseWithDocs,getMap方法执行此操作并迭代视图行并构建id + document的地图。
@Override
public Map<String, Object> More ...getMap() {
if (map == null) {
map = new HashMap<String, Object>();
Iterator<ViewRow> itr = iterator();
while(itr.hasNext()) {
ViewRow cur = itr.next();
map.put(cur.getId(), cur.getDocument());
}
}
return Collections.unmodifiableMap(map);
}
你想要做的是:
for (ViewRow viewRow : viewResponse) {
viewRow.getKey();
viewRow.getId();
viewRow.getValue();
//Logic for above information here
}
此外,您的doc id和doc key似乎是相同的,因此您不需要发出它,因为始终会发出密钥。