Indexing Metadata Information
- reference
Couchbase Server allows indexing on selected metadata fields, for example the expiration and CAS properties. This improves performance of queries involving predicates on the metadata fields, such as expired documents or recently modified documents.
Overview
The META() function enables you to return the metadata for a keyspace or document.
To index a selected metadata field, you must use a nested expression containing the META()
function and the required property, for example META().id
.
The property name must be separated from the META()
function by a dot (.
) and only the following metadata properties can be indexed.
If you attempt to build an index on a metadata field that is not indexable, an error is returned.
- cas
-
Value representing the current state of an item which changes every time the item is modified. For details, refer to Concurrent Document Mutations.
- expiration
-
Value representing a document’s expiration date. A value of 0 (zero) means no expiration date. For details, refer to KV Operations.
Note that this property gives correct results only when used in a Covered Index.
- id
-
Value representing a document’s unique ID number.
The META()
function does not require a keyspace parameter when creating an index, since it implicitly uses the keyspace being indexed.
Examples
CREATE INDEX idx_airline_expire ON `travel-sample`.inventory.airline (META().expiration);
SELECT META().id, META().expiration
FROM `travel-sample`.inventory.airline
WHERE META().expiration = 0
ORDER BY META().id
LIMIT 2;
[
{
"expiration": 0,
"id": "airline_10"
},
{
"expiration": 0,
"id": "airline_10123"
}
]
CREATE INDEX idx_hotel_id ON `travel-sample`.inventory.hotel (META().id);
SELECT name, META().id
FROM `travel-sample`.inventory.hotel
WHERE META().id > "g"
LIMIT 2;
[
{
"id": "hotel_10025",
"name": "Medway Youth Hostel"
},
{
"id": "hotel_10026",
"name": "The Balmoral Guesthouse"
}
]
CREATE INDEX idx_hotel_cas ON `travel-sample`.inventory.hotel (META().cas);
SELECT name, META().cas
FROM `travel-sample`.inventory.hotel
ORDER BY META().cas DESC
LIMIT 2;
[
{
"cas": 1612962459766947800,
"name": "The George Hotel"
},
{
"cas": 1612962459645378600,
"name": "Texas Spring"
}
]