Providing efficient lookups

Views enable us to find documents based on any value or structure that resides in the document. In Filtering and extracting data we demonstrated how you can find the data and have Couchbase Server generate it in an index; this section describes how you can use query parameters to constrain the result set. For instance, imagine you know the date of a particular blog post. To find a single document based on that date, you can provide parameters which specify which items in a index Couchbase Server should return.

Imagine we want to find all blog posts with comments that were made between certain dates. In this case we have a map function in our view and we have generated an index for the view with numerous blog posts indexed. In the Ruby SDK we demonstrate below how we can query a view and pass in query parameters. In this case, we go back to the example index of blog post timestamps and titles that we created with our view. The index is as follows:

KeyValue
"2012/07/30 18:12:10"            "Move Today"
"2012/09/17 21:13:39"            "Bought New Fridge"
"2012/09/25 15:52:20"            "Paint Ball"
doc.recent_posts(:body => {:keys => ["2012/07/30 18:12:10"]})

Here we specify the blog post that we want to retrieve from the index by timestamp. Couchbase Server will return the blog item “Move Today” in response to this query. You can use query parameters to specify ranges of results you are looking for:

doc.recent_posts(:start_key => "2012/09/10 00:00:00",
                               :end_key => "2012/9/30 23:59:59")

In this case we specify the start of our range and end of our range with the parameters :start_key and :end_key respectively. The values we provide for our query parameters indicate we want to find any blog post from the start of the day on September 9th until the end of the day on September 30th. In this case, Couchbase Server will return the following result set based on the index and our query parameters:

KeyValue
"2012/09/17 21:13:39"            "Bought New Fridge"
"2012/09/25 15:52:20"            "Paint Ball"

There is alternate approach for finding documents which does not require indexing and querying via views. This approach would be based on storing one or more keys to a related object in a source document and then performing a retrieve on those keys. The advantage of this alternate approach is that response time will be significantly lower. The disadvantage of this approach is that it could potentially introduce too much contention for the source object if the object contains data that you expect to update frequently from different processes. In this later case where you expect numerous changes to a source document, it is preferable to model the document to be independent of related objects and use indexing and querying to retrieve the related object.

For more information about different ways to model related objects for future search and retrieval, see Modeling documents for retrieval and Using reference documents for lookups. For information about performing multiple-retrieves, see Retrieving multiple keys.