How filter the data in couchbase in composite key such that it should not print "previous key result data" if do not have any result of "particular key sequence"


#1

I want to search the data with four composite keys in such a way that each key is searched independently. for example:-

  1. My view bucket Format:-
    function (doc, meta) {
    emit([doc.Language , doc.Nationality , doc.check_in , doc.occupancy] , [ doc.cost , doc.val ] );
    }

  2. My search query format in java:-
    query.setRangeStart(ComplexKey.of(language , nationality , getCheckIn(start_date) , new Integer( occupancy) ));
    query.setRangeEnd(ComplexKey.of(language , nationality , getCheckIn(end_date) , new Integer( occupupancy) ));

Whenever i search for something if i do not have the results for particular key sequence then “it prints the result of previous keys sequence” instead of 0 . So if i have such query…

In couchbase i have 10 results for query(English , India , 2015-02-18) and I have 0 results for query(English , India , 2015-02-18, 2).

So the issue is whenever i search for query(English , India , 2015-02-18, 2) in java using above given query it gives me 10 results instead of 0 results And i want output should be 0 for those query if i do not have the result in couchbase. Please let me know if there is any other alternative? or can i do it using setKeys or not and how ??

In short :
If i do not have the result for “particular key sequence” it should print zero result instead of printing “previous key results” . I want to filter the data just as we do in sql using where clause… how to achieve that?


#2

In a view you can only grab the ranges, so in your example you are not able to where just on nationality alone… for example you can only search for “all nationalities in language X” and so on… You are always going downwards.
If you want to search for all of those 4 independently you need to have 4 different views.


#3

But i do not want to create two different views because of memory issues So can it be possible without creating different views? or if can we write some script in view bucket which can help me ?


#4

Note that indexes are stored on disk and not in memory so you are not going to hit memory limits on that front.

You can use the SDK to create views and design documents programmatically if that was your question as well?

Are you sure you can’t use more views? To me for your use case this seems the most feasible way. You can also cache the view results in documents and then query it through key/value, that would reduce the query latency if that’s what you are fearing. The only real bottleneck could be cpu time if you have a high insert rate, but I’d recommend to try it out first and look for more complicated ways if you see it’s not working as expected.