Couchbase Server supports a number of mechanisms for selecting information returned by the view. Key selection is made after the view results (including the reduction function) are executed, and after the items in the view output have been sorted.
When specifying keys to the selection mechanism, the key must be expressed in the form of a JSON value. For example, when specifying a single key, a string must be quoted ("string").
When specifying the key selection through a parameter, the keys must match the format of the keys emitted by the view. Compound keys, for example where an array or hash has been used in the emitted key structure, the supplied selection value should also be an array or a hash.
The following selection types are supported:
Explicit Key
An explicit key can be specified using the parameter
key. The view query will only return
results where the key in the view output, and the value
supplied to the key parameter match
identically.
For example, if you supply the value "tomato" only records matching exactly "tomato" will be selected and returned. Keys with values such as "tomatoes" will not be returned.
Key List
A list of keys to be output can be specified by supplying an
array of values using the keys parameter.
In this instance, each item in the specified array will be
used as explicit match to the view result key, with each
array value being combined with a logical
or.
For example, if the value specified to the
keys parameter was
["tomato","avocado"], then all results
with a key of 'tomato' or 'avocado'
will be returned.
When using this query option, the output results are not sorted by key. This is because key sorting of these values would require collating and sorting all the rows before returning the requested information.
In the event of using a compound key, each compound key must be specified in the query. For example:
keys=[["tomato",20],["avocado",20]]Key Range
A key range, consisting of a startkey and
endkey. These options can be used
individually, or together, as follows:
startkey only
Output does not start until the first occurrence of
startkey, or a value greater than the
specifid value, is seen. Output will then continue until
the end of the view.
endkey only
Output starts with the first view result, and continues
until the last occurrence of endkey, or
until the emitted value is greater than the computed
lexical value of endkey.
startkey and endkey
Output of values does not start until
startkey is seen, and stops when the
last occurrence of endkey is
identified.
When using endkey, the
inclusive_end option specifies whether
output stops after the last occurrence of the specified
endkey (the default). If set to false,
output stops on the last result before the specified
endkey is seen.
The matching algorithm works on partial values, which can be used to an advantage when searching for ranges of keys. See Section 9.8.2.2, “Partial Selection and Key Ranges”
If you are generating a compound key within your view, for
example when outputting a date split into individualy year,
month, day elements, then the selection value must exactly
match the format and size of your compound key. The value of
key or keys must exactly
match the output key structure.
For example, with the view data:
{"total_rows":5693,"rows":[ {"id":"1310653019.12667","key":[2011,7,14,14,16,59],"value":null}, {"id":"1310662045.29534","key":[2011,7,14,16,47,25],"value":null}, {"id":"1310668923.16667","key":[2011,7,14,18,42,3],"value":null}, {"id":"1310675373.9877","key":[2011,7,14,20,29,33],"value":null}, {"id":"1310684917.60772","key":[2011,7,14,23,8,37],"value":null}, {"id":"1310693478.30841","key":[2011,7,15,1,31,18],"value":null}, {"id":"1310694625.02857","key":[2011,7,15,1,50,25],"value":null}, {"id":"1310705375.53361","key":[2011,7,15,4,49,35],"value":null}, {"id":"1310715999.09958","key":[2011,7,15,7,46,39],"value":null}, {"id":"1310716023.73212","key":[2011,7,15,7,47,3],"value":null} ] }
Using the key selection mechanism you must
specify the entire key value, i.e.:
?key=[2011,7,15,7,47,3]If you specify a value, such as only the date:
?key=[2011,7,15]The view will return no records, since there is no exact key match. Instead, you must use a range that encompasses the information range you want to output:
?startkey=[2011,7,15,0,0,0]&endkey=[2011,7,15,99,99,99]This will output all records within the specified range for the specified date. For more information, see Section 9.8.2.3, “Partial Selection with Compound Keys”.
Matching of the key value has a precedence from right to left
for the key value and the supplied startkey
and/or endkey. Partial strings may therefore
be specified and return specific information.
For example, given the view data:
"a", "aa", "bb", "bbb", "c", "cc", "ccc" "dddd"
Specifying a startkey parameter with the
value "aa" will return the last seven records, including "aa":
"aa", "bb", "bbb", "c", "cc", "ccc", "dddd"
Specifying a partial string to startkey will
trigger output of the selected values as soon as the first
value or value greather than the specified value is
identified. For strings, this partial match (from left to
right) is identified. For example, specifying a
startkey of "d" will return:
"dddd"
This is because the first match is identified as soon as the a
key from a view row matches the supplied
startkey value from right to
left. The supplied single character matches the
first character of the view output.
When comparing larger strings and compound values the same
matching algorithm is used. For example, searching a database
of ingredients and specifying a startkey of
"almond" will return all the ingredients, including "almond",
"almonds", and "almond essence".
To match all of the records for a given word or value across
the entire range, you can use the null value in the
endkey parameter. For example, to search for
all records that start only with the word "almond", you
specify a startkey of "almond", and an endkey
of "almond\u02ad" (i.e. with the last latin character at the
end). If you are using Unicode strings, you may want to use
"\uefff".
startkey="almond"&endkey="almond\u02ad"
The precedence in this example is that output starts when
'almond' is seen, and stops when the emitted data is lexically
greater than the supplied endkey. Although a
record with the value "almond\02ad" will never be seen, the
emitted data will eventually be lexically greater than
"almond\02ad" and output will stop.
In effect, a range specified in this way acts as a prefix with all the data being output that match the specified prefix.
Compound keys, such as arrays or hashes, can also be specified in the view output, and the matching precedence can be used to provide complex selection ranges. For example, if time data is emitted in the following format:
[year,month,day,hour,minute]Then precise date (and time) ranges can be selected by specifying the date and time in the generate data. For example, to get information between 1st April 2011, 00:00 and 30th September 2011, 23:59:
?startkey=[2011,4,1,0,0]&endkey=[2011,9,30,23,59]
The flexible structure and nature of the
startkey and endkey values
enable selection through a variety of range specifications.
For example, you can obtain all of the data from the beginning
of the year until the 5th March using:
?startkey=[2011]&endkey=[2011,3,5,23,59]You can also examine data from a specific date through to the end of the month:
?startkey=[2011,3,16]&endkey=[2011,3,99]
In the above example, the value for the day
element of the array is an impossible value, but the matching
algorithm will identify when the emitted value is lexically
greater than the supplied endkey value, and
information selected for output will be stopped.
A limitation of this structure is that it is not possible to ignore the earlier array values. For example, to select information from 10am to 2pm each day, you cannot use this parameter set:
?startkey=[null,null,null,10,0]&endkey=[null,null,null,14,0]In addition, because selection is made by a outputting a range of values based on the start and end key, you cannot specify range values for the date portion of the query:
?startkey=[0,0,0,10,0]&endkey=[9999,99,99,14,0]This will instead output all the values from the first day at 10am to the last day at 2pm.
For more information and examples on formatting and querying this data, see Section 9.9.7, “Date and Time Selection”.