Question

why the result of views is different in REST and Java? for example from REST it gives exact result it supposed to return, but from Java it will return whole document of specific key. I have this document:

{
  "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"
  }
}

when I use map function in my views:

function (doc, meta) {
   if( (doc.doctype && doc.doctype=="people") && doc.id=="person0"){
    emit(doc.id,doc.name);     
    }

}

The result in REST is:

{"total_rows":1,"rows":[
{"id":"person0","key":"person0","value":"Kasidit Treweek"}
]
}

But From Java it will return whole document with key "person0" like

{"person0":{"doctype": "people",.....}}

Here is my java code that I have simple implemented:

ViewResponse response = client.query(view, query);
java.util.Map<String, Object> map = response.getMap();
Was it helpful?

Solution

That is because you are using ViewResponseWithDocs, the getMap method does this and iterates over the view rows and constructing the map of 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);
}

What you want to do is this:

for (ViewRow viewRow : viewResponse) {
 viewRow.getKey();
 viewRow.getId();
 viewRow.getValue();
 //Logic for above information here
}

Also it seems your doc id and doc key are the same thing, so you don't need to emit it as the key is always emitted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top