diff --git a/weed/command/shell_s3vectors.go b/weed/command/shell_s3vectors.go index ec167dd719..cd24c91c90 100644 --- a/weed/command/shell_s3vectors.go +++ b/weed/command/shell_s3vectors.go @@ -74,16 +74,8 @@ func (c *commandS3Vectors) Do(args []string, env *shell.CommandEnv, writer io.Wr } ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() - backend := s3vectors.NewVectorDiscovery(env.MasterClient.ListCachedMasters, env.GrpcDialOption(), env.MasterClient.FilerGroup) - defer backend.Close() - server := s3vectors.NewServer(env, nil, backend) - server.SetRegion(s3tables.DefaultRegion) - server.SetAccountID(s3tables.DefaultAccountID) - catalog := lance.NewServer(env, nil) - server.SetDescribe(func(ctx context.Context, id []string) (string, map[string]string, error) { - r := (&http.Request{Method: http.MethodPost, Header: make(http.Header)}).WithContext(ctx) - return catalog.DescribeTableInternal(r, id) - }) + server, closeServer := shellVectorsServer(env) + defer closeServer() var response json.RawMessage if err := server.Execute(ctx, *operation, json.RawMessage(body), &response, s3_constants.AccountAdminId); err != nil { return err @@ -99,3 +91,18 @@ func (c *commandS3Vectors) Do(args []string, env *shell.CommandEnv, writer io.Wr } return nil } + +// shellVectorsServer is the S3 Vectors server the shell runs in process, on +// trusted cluster credentials, and how to close its engine connection. +func shellVectorsServer(env *shell.CommandEnv) (*s3vectors.Server, func()) { + backend := s3vectors.NewVectorDiscovery(env.MasterClient.ListCachedMasters, env.GrpcDialOption(), env.MasterClient.FilerGroup) + server := s3vectors.NewServer(env, nil, backend) + server.SetRegion(s3tables.DefaultRegion) + server.SetAccountID(s3tables.DefaultAccountID) + catalog := lance.NewServer(env, nil) + server.SetDescribe(func(ctx context.Context, id []string) (string, map[string]string, error) { + r := (&http.Request{Method: http.MethodPost, Header: make(http.Header)}).WithContext(ctx) + return catalog.DescribeTableInternal(r, id) + }) + return server, func() { backend.Close() } +} diff --git a/weed/command/shell_s3vectors_metadata.go b/weed/command/shell_s3vectors_metadata.go new file mode 100644 index 0000000000..ef0b129a95 --- /dev/null +++ b/weed/command/shell_s3vectors_metadata.go @@ -0,0 +1,92 @@ +package command + +import ( + "context" + "flag" + "fmt" + "io" + "text/tabwriter" + "time" + + "github.com/seaweedfs/seaweedfs/weed/pb/vector_pb" + "github.com/seaweedfs/seaweedfs/weed/shell" +) + +func init() { shell.Commands = append(shell.Commands, &commandS3VectorsIndexMetadata{}) } + +type commandS3VectorsIndexMetadata struct{} + +func (c *commandS3VectorsIndexMetadata) Name() string { return "s3vectors.index.metadata" } +func (c *commandS3VectorsIndexMetadata) HasTag(shell.CommandTag) bool { return false } +func (c *commandS3VectorsIndexMetadata) Help() string { + return `list, add or remove the indexed metadata keys of a vector index + +s3vectors.index.metadata -bucket embeddings -index articles +s3vectors.index.metadata -bucket embeddings -index articles -add author +s3vectors.index.metadata -bucket embeddings -index articles -remove author + +An indexed key keeps a scalar index of the string values a filter's $eq and +$in compare, so those filters stop reading every row's metadata. Query +results do not change. A key is first "requested"; the lance_metadata_index +worker job builds it ("building") and then "ready" queries use it. Removing +sets it "off" at once and the job drops it. Non-filterable keys cannot be +indexed, an index has at most 8 indexed keys, and adding is refused while a +registered vector engine does not fill indexed key columns on its writes. +` +} + +func (c *commandS3VectorsIndexMetadata) Do(args []string, env *shell.CommandEnv, writer io.Writer) error { + flags := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + bucket := flags.String("bucket", "", "vector bucket name") + index := flags.String("index", "", "index name") + add := flags.String("add", "", "metadata key to index") + remove := flags.String("remove", "", "metadata key to stop indexing") + if err := flags.Parse(args); err != nil { + return err + } + if *bucket == "" || *index == "" { + return fmt.Errorf("-bucket and -index are required") + } + if *add != "" && *remove != "" { + return fmt.Errorf("use either -add or -remove") + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + server, closeServer := shellVectorsServer(env) + defer closeServer() + var keys *vector_pb.MetadataKeysResponse + var err error + switch { + case *add != "": + keys, err = server.UpdateMetadataKey(ctx, *bucket, *index, *add, false) + case *remove != "": + keys, err = server.UpdateMetadataKey(ctx, *bucket, *index, *remove, true) + default: + keys, err = server.MetadataKeys(ctx, *bucket, *index) + } + if err != nil { + return err + } + return printMetadataKeys(writer, keys) +} + +func printMetadataKeys(writer io.Writer, keys *vector_pb.MetadataKeysResponse) error { + if len(keys.Keys) == 0 { + _, err := fmt.Fprintln(writer, "no indexed metadata keys") + return err + } + w := tabwriter.NewWriter(writer, 0, 0, 2, ' ', 0) + fmt.Fprintln(w, "KEY\tSTATE\tCOVERAGE\tSEGMENTS\tSIZE") + for _, k := range keys.Keys { + coverage := "-" + if k.Rows > 0 && k.State != "requested" { + coverage = fmt.Sprintf("%d%%", k.IndexedRows*100/k.Rows) + } + size := "-" + if k.IndexBytes > 0 { + size = fmt.Sprintf("%.1f MiB", float64(k.IndexBytes)/(1<<20)) + } + fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\n", k.Key, k.State, coverage, k.Segments, size) + } + return w.Flush() +} diff --git a/weed/command/shell_s3vectors_metadata_test.go b/weed/command/shell_s3vectors_metadata_test.go new file mode 100644 index 0000000000..991d84ae77 --- /dev/null +++ b/weed/command/shell_s3vectors_metadata_test.go @@ -0,0 +1,28 @@ +package command + +import ( + "bytes" + "strings" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/pb/vector_pb" +) + +func TestPrintMetadataKeys(t *testing.T) { + var out bytes.Buffer + if err := printMetadataKeys(&out, &vector_pb.MetadataKeysResponse{Keys: []*vector_pb.MetadataKey{ + {Key: "lang", State: "requested", Rows: 10}, + {Key: "author", State: "ready", Column: "_mk_0", IndexedRows: 75, Rows: 100, Segments: 2, IndexBytes: 3 << 20}, + }}); err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(out.String()), "\n") + if len(lines) != 3 || !strings.Contains(lines[1], "requested") || !strings.Contains(lines[2], "75%") || !strings.Contains(lines[2], "3.0 MiB") { + t.Fatalf("%q", out.String()) + } + out.Reset() + _ = printMetadataKeys(&out, &vector_pb.MetadataKeysResponse{}) + if !strings.Contains(out.String(), "no indexed metadata keys") { + t.Fatal(out.String()) + } +}