Skip to content

Latest commit

 

History

History
145 lines (116 loc) · 4.47 KB

File metadata and controls

145 lines (116 loc) · 4.47 KB
name search-go
summary Couchbase Full-Text Search, vector search, hybrid search, and geospatial search for Go
description Couchbase Full-Text Search, vector search, hybrid search, and geospatial search for Go
compatibility Go SDK 2.x (gocb/v2). Requires github.com/couchbase/gocb/v2.
metadata
last_verified min_server_version handoff
2026-05
7.0
condition skill
user asks about search concepts, index setup, or RAG patterns
search-concepts
condition skill
user asks about SQL++ queries
server-querying-go
condition skill
user asks about CAS, bulk ops, sub-document, or SDK patterns
sdk-patterns-go
condition skill
user asks about connection setup or SDK configuration
server-connection-go

Couchbase Search — Go

Prerequisites — Index must exist before querying

Create an FTS index before running text or vector queries. Use the templates as a starting point:

See shared/server/search-concepts.md for setup instructions.

Full-Text Search

import (
    "github.com/couchbase/gocb/v2"
    "github.com/couchbase/gocb/v2/search"
)

result, err := cluster.SearchQuery(
    "hotel-fts",
    search.NewMatchQuery("ocean view").Field("description"),
    &gocb.SearchOptions{Limit: 10, Fields: []string{"name", "city", "stars"}},
)
if err != nil { panic(err) }
for result.Next() {
    row := result.Row()
    var fields map[string]interface{}
    row.Fields(&fields)
    fmt.Printf("%s score=%.4f %v\n", row.ID, row.Score, fields)
}

Vector Search

queryVector := embeddingModel.Encode(question) // []float32

result, err := cluster.SearchQuery(
    "product-vector",
    search.NewVectorSearch(
        search.NewVectorQuery("embedding", queryVector).NumCandidates(5),
    ),
    &gocb.SearchOptions{Fields: []string{"name", "description"}},
)

Hybrid Search

import "github.com/couchbase/gocb/v2/search"

queryVector := embeddingModel.Encode(question) // []float32

result, err := cluster.SearchQuery(
    "product-vector",
    search.NewVectorSearch(
        search.NewVectorQuery("embedding", queryVector).NumCandidates(10),
    ).WithSearchQuery(search.NewMatchQuery("comfortable hotel").Field("description")).
        VectorQueryCombination(gocb.VectorQueryCombinationOr),
    &gocb.SearchOptions{Fields: []string{"name", "description"}, Limit: 5},
)

Geospatial Search

result, err := cluster.SearchQuery(
    "hotel-geo",
    search.NewGeoDistanceQuery("10km").Field("geo").Location(37.7749, -122.4194),
    &gocb.SearchOptions{Limit: 20, Fields: []string{"name", "geo"}},
)

RYOW Consistency

import "github.com/couchbase/gocb/v2"

upsertResult, err := collection.Upsert("hotel::new", map[string]interface{}{"name": "New Hotel"}, nil)
if err != nil { /* handle */ }

ms := gocb.NewMutationState(*upsertResult.MutationToken())

result, err := cluster.SearchQuery(
    "hotel-fts",
    search.NewMatchQuery("New Hotel").Field("name"),
    &gocb.SearchOptions{ConsistentWith: ms, Limit: 5},
)

Pagination

// Page 3 (0-indexed skip)
result, err := cluster.SearchQuery(
    "hotel-fts",
    search.NewMatchQuery("ocean view").Field("description"),
    &gocb.SearchOptions{Limit: 10, Skip: 20},
)

Common Query Types

search.NewMatchQuery("ocean view").Field("description")   // full-text with analysis
search.NewTermQuery("airline").Field("type")              // exact token
search.NewPrefixQuery("air").Field("name")                // prefix / autocomplete
search.NewFuzzyQuery("hotl").Fuzziness(1)                 // typo-tolerant
search.NewNumericRangeQuery().Min(3.0, true).Max(5.0, true).Field("stars") // range
search.NewConjunctionQuery(                               // AND
    search.NewMatchQuery("hotel").Field("type"),
    search.NewNumericRangeQuery().Min(4.0, true).Field("stars"),
)

SQL++ with FTS (Flex Index)

SELECT name, SEARCH_SCORE() AS score
FROM `travel-sample`.inventory.hotel AS h
WHERE SEARCH(h, {"query": {"match": "ocean view", "field": "description"}}, {"index": "hotel-fts"})
ORDER BY SEARCH_SCORE() DESC LIMIT 10;

Shared concepts: shared/server/search-concepts.md — prerequisites, hybrid search, RYOW, pagination, query types.