Reputation: 226
This is one of the sample document which I saved in my bucket,
{
"id": "639882607203778560",
"text": "How does Andy Reid describe the no WR touchdown stat?",
"name": "chiefs",
"createdAt": 1441394876000,
}
I need to fetch the documents for given name and the date range. So this is the view I created for it,
function (doc, meta) {
if (doc._class == "com.link.data" && doc.createdAt && doc.name) {
emit([doc.createdAt,doc.name], null);
}
}
This will give me the all documents for given date range but it doesn't filter based on name. I have all the documents with other names also. How can I achieve this? Also what is the correct implementation for java?
This is the my current impl and I want to do this without using N1ql.
query.setRange(ComplexKey.of(1441376400000L, name), ComplexKey.of(1441396800000L,name));
I tried to add range as a startKey and endKey.Then put the name as a key in couchbase UI and it doesn't work.
Upvotes: 1
Views: 413
Reputation: 309
"given" means exact match? If you want to get result with exact matched name with date range, try emit() with:
emit([doc.name, doc.createdAt], null);
the key array itself, 1st arg in emit() function, is the sort order.
Upvotes: 0
Reputation: 8157
Disclaimer: I dont have a lot of experience with compound/complex keys in CB.
I believe what you're asking for cannot be done with a single view: Sorting on date and then filtering on a specific name. You can sort on range and then group-by name, but you'd still get all the various names in the bucket (as you've already noticed).
What you can do is use two separate views and then intersect the results: get the doc-ids with the name you want, get the docs in the range you want and find intersections in your java code. Since views are only 'eventually consistent', your results are just as good as with a single view request, so the only thing you're wasting here is bandwidth and a little time, but not result-precision.
Upvotes: 1