mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
shell: s3vectors.index.metadata lists, adds and removes indexed keys
s3vectors.index.metadata -bucket b -index i [-add key | -remove key] prints each indexed metadata key with its state, the share of rows its scalar index covers, its segments and size. The shell's in-process S3 Vectors server setup moves into shellVectorsServer for both commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
This commit is contained in:
1 parent
9c62e4dd04
commit
17bfb67688
3 files changed
+137
-10
No files matched your search
@@ -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() }
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user