Skip to content
Start here

Query Vectors

client.Vectorize.Indexes.Query(ctx, indexName, params) (*IndexQueryResponse, error)
POST/accounts/{account_id}/vectorize/v2/indexes/{index_name}/query

Finds vectors closest to a given vector in an index.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
Vectorize WriteVectorize Read
ParametersExpand Collapse
indexName string
params IndexQueryParams
AccountID param.Field[string]

Path param: Identifier

maxLength32
Vector param.Field[[]float64]

Body param: The search vector that will be used to find the nearest neighbors.

Filter param.Field[unknown]optional

Body param: A metadata filter expression used to limit nearest neighbor results.

ReturnMetadata param.Field[IndexQueryParamsReturnMetadata]optional

Body param: Whether to return no metadata, indexed metadata or all metadata associated with the closest vectors.

const IndexQueryParamsReturnMetadataNone IndexQueryParamsReturnMetadata = "none"
const IndexQueryParamsReturnMetadataIndexed IndexQueryParamsReturnMetadata = "indexed"
const IndexQueryParamsReturnMetadataAll IndexQueryParamsReturnMetadata = "all"
ReturnValues param.Field[bool]optional

Body param: Whether to return the values associated with the closest vectors.

TopK param.Field[float64]optional

Body param: The number of nearest neighbors to find.

ReturnsExpand Collapse
type IndexQueryResponse struct{…}
Count int64optional

Specifies the count of vectors returned by the search

Matches []IndexQueryResponseMatchoptional

Array of vectors matched by the search

ID stringoptional

Identifier for a Vector

maxLength64
Metadata unknownoptional
Namespace stringoptional
Score float64optional

The score of the vector according to the index's distance metric

Values []float64optional

Query Vectors

package main

import (
  "context"
  "fmt"

  "github.com/cloudflare/cloudflare-go"
  "github.com/cloudflare/cloudflare-go/option"
  "github.com/cloudflare/cloudflare-go/vectorize"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  response, err := client.Vectorize.Indexes.Query(
    context.TODO(),
    "example-index",
    vectorize.IndexQueryParams{
      AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
      Vector: cloudflare.F([]float64{0.500000, 0.500000, 0.500000}),
    },
  )
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", response.Count)
}
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "count": 0,
    "matches": [
      {
        "id": "some-vector-id-023e105f4ecef8ad9ca31a8372d0c353",
        "metadata": {},
        "namespace": "namespace",
        "score": 0,
        "values": [
          0
        ]
      }
    ]
  },
  "success": true
}
Returns Examples
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "count": 0,
    "matches": [
      {
        "id": "some-vector-id-023e105f4ecef8ad9ca31a8372d0c353",
        "metadata": {},
        "namespace": "namespace",
        "score": 0,
        "values": [
          0
        ]
      }
    ]
  },
  "success": true
}