Vector Search

      +
      Vector Search from the SDK, to enable AI integration, semantic search, and the RAG framework.

      This is currently implemented using xref: [Search Indexes], and can even be combined with traditional full text search queries.

      Prerequisites

      Couchbase Server 7.6.0 (7.6.2 for base64-encoded vectors) — or recent Capella instance.

      Single vector query

      In this first example we are performing a single vector query:

      val request = SearchRequest.vectorSearch(VectorSearch(VectorQuery("vector_field", vectorQuery)))
      
      val result: Try[SearchResult] = scope.search("vector-index", request)

      Let’s break this down. We create a SearchRequest, which can contain a traditional FTS query SearchQuery and/or the new VectorSearch. Here we are just using the latter.

      The VectorSearch allows us to perform one or more VectorQuery s.

      The VectorQuery itself takes the name of the document field that contains embedded vectors ("vector_field" here), plus actual vector query in the form of a float[].

      (Note that Couchbase itself is not involved in generating the vectors, and these will come from an external source such as an embeddings API.)

      Finally we execute the SearchRequest against the FTS index "travel-sample-index", which has previously been setup to vector index the "vector_field" field.

      This happens to be a scoped index so we are using scope.search(). If it was a global index we would use cluster.search() instead - see [Scoped vs Global Indexes].

      It returns the same SearchResult detailed earlier.

      Multiple vector queries

      You can run multiple vector queries together:

      val request = SearchRequest.searchQuery(SearchQuery.matchAll)
        .vectorSearch(VectorSearch(VectorQuery("vector_field", vectorQuery)))
      
      val result = scope.search("vector-and-fts-index", request)

      How the results are combined (ANDed or ORed) can be controlled with vectorSearchOptions().vectorQueryCombination().

      Combining FTS and vector queries

      You can combine a traditional FTS query with vector queries:

      val request = SearchRequest
        .vectorSearch(VectorSearch(Seq(
          VectorQuery("vector_field", vectorQuery).numCandidates(2).boost(0.3),
          VectorQuery("vector_field", anotherVectorQuery).numCandidates(5).boost(0.7))))
      
      val result = scope.search("vector-index", request)

      How the results are combined (ANDed or ORed) can be controlled with vectorSearchOptions().vectorQueryCombination().