View not displaying data

And I'm trying to retrieve all the documents with caseId="28" from the view below. The sample document is:
"CacheElementMap": {
"caseId": 28,
"pageNumber": 1,
"totalPages": 1,
"cacheType": "CaseIdMap",
"objectType": "CacheElementMap",
"key": "CacheElementMap|CASE_ID_MAP|CASE_ID:28"
}
View:
function (doc, meta) {
if (doc.objectType=="CacheElementMap")
{
emit(doc.caseId,doc);
}
}
Web call
http://localhost:8092/default/_design/dev_CacheElementMap/_view/metadata..."28"]
The call returns 0 rows. If I don't use a key, then all documents are retrieved.
Can somebody please point me in the right direction?
Thanks in advance.

I see that you just signed up in the forum . so welcome to the world of Couchbase.
1. In your emit statement do not emit the full doc in the value. Instead do emit(doc.caseId,null); or emit(doc.caseId,1); You'll duplicate data and your views will be huge if you emit the doc.
if you need to have the whole document included in the view results use this param in your view call. ?include_docs=true
2. In your view calls you do not need brackets just double quotes do this below.
http://localhost:8092/default/_design/dev_CacheElementMap/_view/metadata..."28"

Thanks for the recommendations. I've changed the view to:
function (doc, meta) {
emit(doc.caseId,null);
}
However, the original problem remains. If I use the key, the results are not coming back.
http://localhost:8092/default/_design/dev_XCacheElementMap/_view/metadat..."28"&include_docs=true returns 0 rows. If I don't use the key all the results are coming back.

1 Answer

« Back to question.

Hello,
This attribute is an integer so you should use:
?key=28
Also NEVER emit the document in your index. By doing that you simply double the size without adding any "benefits" to your application. It will be faster to get the document id using the view and do a client/get(id) from your application (using the SDK that use Memcached protocol)
Indexes are store on disk and cached using the "OS disk cache" where the data are store on disk but automatically cached in RAM by the database (using the built-in memcache process)
Regards

Hello Tug,
Thank you very much for explaining the index/view config. Now, back to the "find by key".
I tried providing a number without the quotes, and it's not working either. I tried both RESTful and Java SKD. The behavior is the same: once I provide the key, the data is not fetched.
Web: http://localhost:8092/default/_design/dev_XCacheElementMap/_view/metadat...
Output:
"rows":[
]
Java:
View view = _couchbaseClient.getView("CacheElementMap", "metadata_cache_v");
Query query = new Query();
query.setKey("28");
query.setIncludeDocs(true).setLimit(100);
query.setStale( Stale.FALSE );
ViewResponse result = _couchbaseClient.query(view, query);
System.out.println("Results=\n"+result);
Any ideas?
Thank you.

The view function you have is correct. I'm thinking it might be the json object itself.
how is the json stored. is it stored like this
{
"caseId": 28,
"pageNumber": 1,
"totalPages": 1,
"cacheType": "CaseIdMap",
"objectType": "CacheElementMap",
"key": "CacheElementMap|CASE_ID_MAP|CASE_ID:28"
}

OR
{
"CacheElementMap": {
"caseId": 28,
"pageNumber": 1,
"totalPages": 1,
"cacheType": "CaseIdMap",
"objectType": "CacheElementMap",
"key": "CacheElementMap|CASE_ID_MAP|CASE_ID:28"
}
}

The json is stored like:
{
"CacheElementMap": {
"caseId": 28,
"pageNumber": 1,
"totalPages": 1,
"cacheType": "CaseIdMap",
"objectType": "CacheElementMap",
"key": "CacheElementMap|CASE_ID_MAP|CASE_ID:28"
}
}
Thanks.

ok... you have to make the map stiff for object type like this.

function (doc, meta) {
if (doc.CacheElementMap.objectType=="CacheElementMap")
{
emit(doc.CacheElementMap.caseId,doc);
}

Thank you very much. It worked.
Thanks again.