diff --git a/pinecone/index_connection.go b/pinecone/index_connection.go index b8cb27f..26c8eb4 100644 --- a/pinecone/index_connection.go +++ b/pinecone/index_connection.go @@ -4,15 +4,21 @@ import ( "context" "crypto/tls" "fmt" - "log" - "github.com/pinecone-io/go-pinecone/internal/gen/data" "github.com/pinecone-io/go-pinecone/internal/useragent" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/metadata" + "log" ) +// IndexConnection holds the parameters for a Pinecone IndexConnection object. +// +// Fields: +// - Namespace: The namespace where index operations will be performed. +// - additionalMetadata: Additional metadata to be sent with each RPC request. +// - dataClient: The gRPC client for the index. +// - grpcConn: The gRPC connection. type IndexConnection struct { Namespace string additionalMetadata map[string]string @@ -49,11 +55,102 @@ func newIndexConnection(in newIndexParameters) (*IndexConnection, error) { return &idx, nil } +// Close closes the grpc.ClientConn to a Pinecone index. +// +// Returns an error if the connection cannot be closed, otherwise returns nil. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection: %v", err) +// } +// +// err = idxConnection.Close() +// if err != nil { +// log.Fatalf("Failed to close index connection:", err) +// } func (idx *IndexConnection) Close() error { err := idx.grpcConn.Close() return err } +// UpsertVectors upserts vectors into a Pinecone index. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - in: The vectors to upsert. +// +// Returns the number of vectors upserted or an error if the request fails. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// metadataMap := map[string]interface{}{ +// "genre": "classical", +// } +// +// metadata, err := structpb.NewStruct(metadataMap) +// if err != nil { +// log.Fatalf("Failed to create metadata map:", err) +// } +// +// sparseValues := pinecone.SparseValues{ +// Indices: []uint32{0, 1}, +// Values: []float32{1.0, 2.0}, +// } +// +// vectors := []*pinecone.Vector{ +// {Id: "abc-1", +// Values: []float32{1.0, 2.0}, +// Metadata: metadata, +// SparseValues: &sparseValues, +// }, +// } +// +// count, err := idxConnection.UpsertVectors(ctx, vectors) +// if err != nil { +// log.Fatalf("Failed to upsert vectors:", err) +// } else { +// log.Fatalf("Successfully upserted %d vector(s)!\n", count) +// } func (idx *IndexConnection) UpsertVectors(ctx context.Context, in []*Vector) (uint32, error) { vectors := make([]*data.Vector, len(in)) for i, v := range in { @@ -72,11 +169,58 @@ func (idx *IndexConnection) UpsertVectors(ctx context.Context, in []*Vector) (ui return res.UpsertedCount, nil } +// FetchVectorsResponse holds the parameters for the FetchVectorsResponse object, +// which is returned by the FetchVectors method. +// +// Fields: +// - Vectors: The vectors fetched. +// - Usage: The usage information for the request. type FetchVectorsResponse struct { Vectors map[string]*Vector `json:"vectors,omitempty"` Usage *Usage `json:"usage,omitempty"` } +// FetchVectors fetches vectors by ID from a Pinecone index. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - ids: The unique IDs of the vectors to fetch. +// +// Returns a pointer to any fetched vectors or an error if the request fails. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// res, err := idxConnection.FetchVectors(ctx, []string{"abc-1"}) +// if err != nil { +// log.Fatalf("Failed to fetch vectors, error:", err) +// } +// if len(res.Vectors) != 0 { +// fmt.Println(res) +// } else { +// fmt.Println("No vectors found") +// } func (idx *IndexConnection) FetchVectors(ctx context.Context, ids []string) (*FetchVectorsResponse, error) { req := &data.FetchRequest{ Ids: ids, @@ -99,18 +243,85 @@ func (idx *IndexConnection) FetchVectors(ctx context.Context, ids []string) (*Fe }, nil } +// ListVectorsRequest holds the parameters for the ListVectorsRequest object, +// which is passed into the ListVectors method. +// +// Fields: +// - Prefix: The prefix by which to filter. If unspecified, +// an empty string will be used which will list all vector ids in the namespace +// - Limit: The maximum number of vectors to return. If unspecified, the server will use a default value. +// - PaginationToken: The token for paginating through results. type ListVectorsRequest struct { Prefix *string Limit *uint32 PaginationToken *string } +// ListVectorsResponse holds the parameters for the ListVectorsResponse object, +// which is returned by the ListVectors method. +// +// Fields: +// - VectorIds: The unique IDs of the returned vectors. +// - Usage: The usage information for the request. +// - NextPaginationToken: The token for paginating through results. type ListVectorsResponse struct { VectorIds []*string `json:"vector_ids,omitempty"` Usage *Usage `json:"usage,omitempty"` NextPaginationToken *string `json:"next_pagination_token,omitempty"` } +// ListVectors lists vectors in a Pinecone index. You can filter vectors by prefix, +// limit the number of vectors returned, and paginate through results. +// +// Note: ListVectors is only available for Serverless indexes. +// +// Returns a pointer to a ListVectorsResponse object or an error if the request fails. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - in: A ListVectorsRequest object with the parameters for the request. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// prefix := "abc" +// limit := uint32(10) +// +// res, err := idxConnection.ListVectors(ctx, &pinecone.ListVectorsRequest{ +// Prefix: &prefix, +// Limit: &limit, +// }) +// +// if err != nil { +// log.Fatalf("Failed to list vectors in index: %s. Error: %s\n", idx.Name, err) +// } +// +// if len(res.VectorIds) == 0 { +// fmt.Println("No vectors found") +// } else { +// fmt.Printf("Found %d vector(s)\n", len(res.VectorIds)) +// } func (idx *IndexConnection) ListVectors(ctx context.Context, in *ListVectorsRequest) (*ListVectorsResponse, error) { req := &data.ListRequest{ Prefix: in.Prefix, @@ -135,6 +346,16 @@ func (idx *IndexConnection) ListVectors(ctx context.Context, in *ListVectorsRequ }, nil } +// QueryByVectorValuesRequest holds the parameters for the QueryByVectorValuesRequest object, +// which is passed into the QueryByVectorValues method. +// +// Fields: +// - Vector: The query vector used to find similar vectors. +// - TopK: The number of vectors to return. +// - Filter: The filter to apply to your query. +// - IncludeValues: Whether to include the values of the vectors in the response. +// - IncludeMetadata: Whether to include the metadata associated with the vectors in the response. +// - SparseValues: The sparse values of the query vector, if applicable. type QueryByVectorValuesRequest struct { Vector []float32 TopK uint32 @@ -144,11 +365,84 @@ type QueryByVectorValuesRequest struct { SparseValues *SparseValues } +// QueryVectorsResponse holds the parameters for the QueryVectorsResponse object, +// which is returned by the QueryByVectorValues method. +// +// Fields: +// - Matches: The vectors that are most similar to the query vector. +// - Usage: The usage information for the request. type QueryVectorsResponse struct { Matches []*ScoredVector `json:"matches,omitempty"` Usage *Usage `json:"usage,omitempty"` } +// QueryByVectorValues queries a Pinecone index for vectors that are most similar to a provided query vector. +// +// Returns a pointer to a QueryVectorsResponse object or an error if the request fails. +// +// Note: To issue a hybrid query with both dense and sparse values, +// your index's similarity metric must be dot-product. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - in: A QueryByVectorValuesRequest object with the parameters for the request. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// queryVector := []float32{1.0, 2.0} +// topK := uint32(10) +// metadataMap := map[string]interface{}{ +// "genre": "classical", +// } +// metadataFilter, err := structpb.NewStruct(metadataMap) +// if err != nil { +// log.Fatalf("Failed to create metadata map:", err) +// } +// +// sparseValues := pinecone.SparseValues{ +// Indices: []uint32{0, 1}, +// Values: []float32{1.0, 2.0}, +// } +// +// res, err := idxConnection.QueryByVectorValues(ctx, &pinecone.QueryByVectorValuesRequest{ +// Vector: queryVector, +// TopK: topK, // number of vectors to be returned +// Filter: metadataFilter, +// SparseValues: &sparseValues, +// IncludeValues: true, +// IncludeMetadata: true, +// } +// ) +// +// if err != nil { +// log.Fatalf("Error encountered when querying by vector:", err) +// } else { +// for _, match := range res.Matches { +// fmt.Printf("Match vector `%s`, with score %f\n", match.Vector.Id, match.Score) +// } +// } func (idx *IndexConnection) QueryByVectorValues(ctx context.Context, in *QueryByVectorValuesRequest) (*QueryVectorsResponse, error) { req := &data.QueryRequest{ Namespace: idx.Namespace, @@ -163,6 +457,16 @@ func (idx *IndexConnection) QueryByVectorValues(ctx context.Context, in *QueryBy return idx.query(ctx, req) } +// QueryByVectorIdRequest holds the parameters for the QueryByVectorIdRequest object, +// which is passed into the QueryByVectorId method. +// +// Fields: +// - VectorId: The unique ID of the vector used to find similar vectors. +// - TopK: The number of vectors to return. +// - Filter: The filter to apply to your query. +// - IncludeValues: Whether to include the values of the vectors in the response. +// - IncludeMetadata: Whether to include the metadata associated with the vectors in the response. +// - SparseValues: The sparse values of the query vector, if applicable. type QueryByVectorIdRequest struct { VectorId string TopK uint32 @@ -172,6 +476,60 @@ type QueryByVectorIdRequest struct { SparseValues *SparseValues } +// QueryByVectorId uses a vector ID to query a Pinecone index and retrieve vectors that are most similar to the +// provided ID's underlying vector. +// +// Returns a pointer to a QueryVectorsResponse object or an error if the request fails. +// +// Note: QueryByVectorId executes a nearest neighbors search, +// meaning that unless TopK=1 in the QueryByVectorIdRequest object, +// it will return 2+ vectors. The vector with a score of 1.0 is the vector with the same ID as the query vector. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - in: A QueryByVectorIdRequest object with the parameters for the request. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// vectorId := "abc-1" +// topK := uint32(10) +// +// res, err := idxConnection.QueryByVectorId(ctx, &pinecone.QueryByVectorIdRequest{ +// VectorId: vectorId, +// TopK: topK, // number of vectors you want returned +// IncludeValues: true, +// IncludeMetadata: true, +// }) index +// +// if err != nil { +// log.Fatalf("Error encountered when querying by vector ID `%s`. Error: %s", id, err) +// } else { +// for _, match := range res.Matches { +// fmt.Printf("Match vector with ID `%s`, with score %f\n", match.Vector.Id, match.Score) +// } +// } func (idx *IndexConnection) QueryByVectorId(ctx context.Context, in *QueryByVectorIdRequest) (*QueryVectorsResponse, error) { req := &data.QueryRequest{ Id: in.VectorId, @@ -186,6 +544,47 @@ func (idx *IndexConnection) QueryByVectorId(ctx context.Context, in *QueryByVect return idx.query(ctx, req) } +// DeleteVectorsById deletes vectors by ID from a Pinecone index. +// +// Returns an error if the request fails, +// otherwise returns nil. This method will also return nil if the passed vector ID does not exist in the index or +// namespace. +// +// Note: You must instantiate an IndexWithNamespace connection in order to delete vectors by ID in namespaces other +// than the default. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - ids: IDs of the vectors you want to delete. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.IndexWithNamespace(idx.Host, "custom-namespace") +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// err = idxConnection.DeleteVectorsById(ctx, []string{id}) +// if err != nil { +// log.Fatalf("Failed to delete vector with ID: %s. Error: %s\n", id, err) +// } func (idx *IndexConnection) DeleteVectorsById(ctx context.Context, ids []string) error { req := data.DeleteRequest{ Ids: ids, @@ -195,6 +594,55 @@ func (idx *IndexConnection) DeleteVectorsById(ctx context.Context, ids []string) return idx.delete(ctx, &req) } +// DeleteVectorsByFilter deletes vectors from a Pinecone index, given a filter. +// +// Returns an error if the request fails, otherwise returns nil. +// +// Note: DeleteVectorsByFilter is only available on pods-based indexes. +// Additionally, you must instantiate an IndexWithNamespace connection in order to delete vectors in namespaces +// other than the default. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - filter: The filter to apply to the deletion. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// metadataFilter := map[string]interface{}{ +// "genre": "classical", +// } +// +// filter, err := structpb.NewStruct(metadataFilter) +// if err != nil { +// log.Fatalf("Failed to create metadata filter:", err) +// } +// +// err = idxConnection.DeleteVectorsByFilter(ctx, filter) +// if err != nil { +// log.Fatalf("Failed to delete vector(s) with filter: %+v. Error: %s\n", filter, err) +// } func (idx *IndexConnection) DeleteVectorsByFilter(ctx context.Context, filter *Filter) error { req := data.DeleteRequest{ Filter: filter, @@ -204,6 +652,44 @@ func (idx *IndexConnection) DeleteVectorsByFilter(ctx context.Context, filter *F return idx.delete(ctx, &req) } +// DeleteAllVectorsInNamespace deletes all vectors in a specific namespace. +// +// Returns an error if the request fails, otherwise returns nil. +// +// Note: You must instantiate an IndexWithNamespace connection in order to delete vectors by ID in namespaces other +// than the default. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// err = idxConnection.DeleteAllVectorsInNamespace(ctx) +// if err != nil { +// log.Fatalf("Failed to delete vectors in namespace: \"%s\". Error: %s", idxConnection.Namespace, err) +// } func (idx *IndexConnection) DeleteAllVectorsInNamespace(ctx context.Context) error { req := data.DeleteRequest{ Namespace: idx.Namespace, @@ -213,6 +699,14 @@ func (idx *IndexConnection) DeleteAllVectorsInNamespace(ctx context.Context) err return idx.delete(ctx, &req) } +// UpdateVectorRequest holds the parameters for the UpdateVectorRequest object, +// which is passed into the UpdateVector method. +// +// Fields: +// - Id: The unique ID of the vector to update. +// - Values: The values with which you want to update the vector. +// - SparseValues: The sparse values with which you want to update the vector. +// - Metadata: The metadata with which you want to update the vector. type UpdateVectorRequest struct { Id string Values []float32 @@ -220,6 +714,48 @@ type UpdateVectorRequest struct { Metadata *Metadata } +// UpdateVector updates a vector in a Pinecone index by ID. +// +// Returns an error if the request fails, returns nil otherwise. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - in: An UpdateVectorRequest object with the parameters for the request. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// id := "abc-1" +// +// err = idxConnection.UpdateVector(ctx, &pinecone.UpdateVectorRequest{ +// Id: id, +// Values: []float32{7.0, 8.0}, +// } +// ) +// if err != nil { +// log.Fatalf("Failed to update vector with ID %s. Error: %s", id, err) +// } func (idx *IndexConnection) UpdateVector(ctx context.Context, in *UpdateVectorRequest) error { req := &data.UpdateRequest{ Id: in.Id, @@ -233,6 +769,14 @@ func (idx *IndexConnection) UpdateVector(ctx context.Context, in *UpdateVectorRe return err } +// DescribeIndexStatsResponse holds the parameters for the DescribeIndexStatsResponse object, +// which is returned by the DescribeIndexStats method. +// +// Fields: +// - Dimension: The dimension of the index. +// - IndexFullness: The fullness level of the index. Note: only available on pods-based indexes. +// - TotalVectorCount: The total number of vectors in the index. +// - Namespaces: The namespace(s) in the index. type DescribeIndexStatsResponse struct { Dimension uint32 `json:"dimension"` IndexFullness float32 `json:"index_fullness"` @@ -240,10 +784,98 @@ type DescribeIndexStatsResponse struct { Namespaces map[string]*NamespaceSummary `json:"namespaces,omitempty"` } +// DescribeIndexStats returns statistics about a Pinecone index. +// +// Returns a pointer to a DescribeIndexStatsResponse object or an error if the request fails. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index:", err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// res, err := idxConnection.DescribeIndexStats(ctx) +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error: %s", idx.Name, err) +// } else { +// log.Fatalf("%+v", *res) +// } func (idx *IndexConnection) DescribeIndexStats(ctx context.Context) (*DescribeIndexStatsResponse, error) { return idx.DescribeIndexStatsFiltered(ctx, nil) } +// DescribeIndexStatsFiltered returns statistics about a Pinecone index, filtered by a given filter. +// +// Returns a pointer to a DescribeIndexStatsResponse object or an error if the request fails. +// +// Note: DescribeIndexStatsFiltered is only available on pods-based indexes. +// +// Parameters: +// - ctx: A context.Context object controls the request's lifetime, +// allowing for the request to be canceled or to timeout according to the context's deadline. +// - filter: The filter to apply to the request. +// +// Example: +// ctx := context.Background() +// +// clientParams := pinecone.NewClientParams{ +// ApiKey: "YOUR_API_KEY", +// SourceTag: "your_source_identifier", // optional +// } +// +// pc, err := pinecone.NewClient(clientParams) +// if err != nil { +// log.Fatalf("Failed to create Client: %v", err) +// } +// +// idx, err := pc.DescribeIndex(ctx, "your-index-name") +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err) +// } +// +// idxConnection, err := pc.Index(idx.Host) +// if err != nil { +// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err) +// } +// +// metadataFilter := map[string]interface{}{ +// "genre": "classical", +// } +// +// filter, err := structpb.NewStruct(metadataFilter) +// if err != nil { +// log.Fatalf("Failed to create filter %+v. Error: %s", metadataFilter, err) +// } +// +// res, err := idxConnection.DescribeIndexStatsFiltered(ctx, filter) +// if err != nil { +// log.Fatalf("Failed to describe index \"%s\". Error: %s", idx.Name, err) +// } else { +// for name, summary := range res.Namespaces { +// fmt.Printf("Namespace: \"%s\", has %d vector(s) that match the given filter\n", name, summary.VectorCount) +// } +// } func (idx *IndexConnection) DescribeIndexStatsFiltered(ctx context.Context, filter *Filter) (*DescribeIndexStatsResponse, error) { req := &data.DescribeIndexStatsRequest{ Filter: filter,