A newer version of this documentation is available.

View Latest

Full Text Search (FTS) Using the Go SDK with Couchbase Server

You can use the Full Text Search service (FTS) to create queryable full-text indexes in Couchbase Server.

Couchbase offers Full-text search support, allowing you to search for documents that contain certain words or phrases. In the Go SDK you can search full-text indexes by using the SearchQuery object and executing it with the Bucket.ExecuteSearchQuery() API.

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).

The following example shows how to send a simple Search query (note that in this example the cbft package also has to be imported independently):

import (

// ...
query := gocb.NewSearchQuery("travel-search", cbft.NewTermQuery("office"))
res, _ := bucket.ExecuteSearchQuery(query)
for _, hit := range res.Hits() {
        fmt.Printf("%s\n", hit.Id)

The Bucket.ExecuteSearchQuery() method returns a SearchResultsinterface which provides access to all of the information returned by the query.

Other search result data may be accessed using the various other available methods on the SearchResults interface.

res, _ := bucket.ExecuteSearchQuery(query)
for _, facet := range res.Facets() {
        // ...
fmt.Printf("Total Hits: %d", res.Status().Total)

Query Types

Query types may be found inside the gocb/cbft package. The package contains query classes corresponding to those enumerated in Types of Queries. Query object should be instantiated by using the associated New*Query functions, passing the search term (usually a string) as the first argument, followed by some query modifiers.

It is important to distinguish between query options and general search options. Some options affect the search process in general (such as the Limit, indicating how many results to return) while others only affect a specific query (such as Fuzziness for a given query). Because multiple queries can be combined in a single search operation, query specific options can be specified only in the query object itself, while search options are specified through methods on the SearchQuery object itself.

Query Facets

Query facets may also be added to the general search parameters by using the AddFacet method. The AddFacet method accepts a facet name as well as a facet definiton You can create facets by instantiating a Facet object found in the gocb.cbft package.

query := gocb.NewSearchQuery("travel-search", cbft.NewTermQuery("office"))
query.AddFacet("countries", cbft.NewTermFacet("country", 5))
res, _ := bucket.ExecuteSearchQuery(query)
fmt.Printf("Total Countries: %d", res.Facets()["countries"].Total)