A newer version of this documentation is available.

View Latest

Full Text Search (FTS) using the C (libcouchbase) SDK with Couchbase Server

You can use the Full Text Search service (FTS) to create queryable full-text indexes in Couchbase Server. This page describes how to use Full Text Search using the C SDK.

Couchbase offers Full-Text search support, allowing you to search for documents that contain certain words or phrases. In the C SDK, you can search full-text indexes by using the row-based lcb_fts_query() API (#include <libcouchbase/cbft.h> first, though!)

The FTS feature is a developer preview in Couchbase Server 4.5. As such, the support in the SDK is still uncomitted and the API is subject to change (although no major changes are expected as of this writing).

To use the lcb_fts_query function, first create an lcb_CMDFTS structure, and populate it with a query. The query should be specified as a JSON-encoded string and can be generated using any JSON encoder:

    std::string encodedQuery(
        "{\"query\":{\"match\":\"hoppy\"},\"indexName\":\"beer-search\",\"size\":10}");
    lcb_CMDFTS cmd = { 0 };
    cmd.callback = rowCallback;
    cmd.query = encodedQuery.c_str();
    cmd.nquery = encodedQuery.size();
See a fully compilable example using FTS from the C SDK: https://github.com/couchbaselabs/devguide-examples/blob/server-4.5/c/fts-basic.cc
static void rowCallback(lcb_t, int, const lcb_RESPFTS *resp)
{
    if (resp->rflags & LCB_RESP_F_FINAL) {
        printf("Status: %d\n", resp->rc);
        printf("Meta: %.*s\n", (int)resp->nrow, resp->row);
        if (resp->htresp) {
            printf("HTTP Response: %.*s\n", (int)resp->htresp->nbody, resp->htresp->body);
        }
    } else {
        printf("Row: %.*s\n", (int)resp->nrow, resp->row);
    }
}

Note that the indexName must be present in the query.

The callback (i.e. lcb_CMDFTS::callback) is invoked once for each query hit, and one more time (with the LCB_RESP_F_FINAL flag set in the lcb_RESPFTS::rflags field) with the query response metadata. The metadata contains things such as response facets

Refer to Queries for more information about the encoded query format.