mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 06:24:18 +02:00
57
cmd/index.go
57
cmd/index.go
@@ -36,11 +36,11 @@ func Index(conf *cfg.Config) *cli.Command {
|
||||
Commands: []*cli.Command{
|
||||
IndexList(conf),
|
||||
IndexShow(conf),
|
||||
IndexCreate(conf),
|
||||
IndexCreate(conf, false),
|
||||
IndexCreate(conf, true),
|
||||
IndexDelete(conf),
|
||||
IndexClose(conf),
|
||||
IndexAllocation(conf),
|
||||
IndexModify(conf),
|
||||
IndexFields(conf),
|
||||
IndexIlm(conf),
|
||||
|
||||
@@ -143,12 +143,20 @@ func IndexShow(conf *cfg.Config) *cli.Command {
|
||||
}
|
||||
}
|
||||
|
||||
func IndexCreate(conf *cfg.Config) *cli.Command {
|
||||
func IndexCreate(conf *cfg.Config, modify bool) *cli.Command {
|
||||
name := "create"
|
||||
usage := "create a new index"
|
||||
|
||||
if modify {
|
||||
name = "modify"
|
||||
usage = "modify anindex"
|
||||
}
|
||||
|
||||
return &cli.Command{
|
||||
Name: "create",
|
||||
Name: name,
|
||||
Aliases: []string{"+"},
|
||||
Usage: "create a new index",
|
||||
UsageText: `create <name> <fieldmapping:type>...
|
||||
Usage: usage,
|
||||
UsageText: name + ` <name> <fieldmapping:type>...
|
||||
Valid field mapping types: integer, text, date, keyword`,
|
||||
|
||||
Flags: []cli.Flag{
|
||||
@@ -171,6 +179,12 @@ Valid field mapping types: integer, text, date, keyword`,
|
||||
Aliases: []string{"r"},
|
||||
Value: 1,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "ilm-policy",
|
||||
Usage: "ilm policy to apply",
|
||||
Destination: &conf.Policy,
|
||||
Aliases: []string{"p"},
|
||||
},
|
||||
},
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
@@ -229,37 +243,6 @@ func IndexClose(conf *cfg.Config) *cli.Command {
|
||||
}
|
||||
}
|
||||
|
||||
func IndexModify(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "modify",
|
||||
Usage: "modify an index",
|
||||
UsageText: "modify <index[,index,...]|_all>",
|
||||
|
||||
ShellComplete: func(ctx context.Context, cmd *cli.Command) {
|
||||
complete(cmd, Cindex)
|
||||
},
|
||||
|
||||
Flags: []cli.Flag{
|
||||
&cli.IntFlag{
|
||||
Name: "replicas",
|
||||
Usage: "number of replicas",
|
||||
Destination: &conf.Replicas,
|
||||
Aliases: []string{"r"},
|
||||
Value: 1,
|
||||
},
|
||||
},
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
index := cmd.Args().Get(0)
|
||||
if index == "" {
|
||||
return errors.New("no index specified")
|
||||
}
|
||||
|
||||
return es.IndexModify(conf, index)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func IndexFields(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "fields",
|
||||
|
||||
@@ -169,6 +169,12 @@ https://www.elastic.co/docs/reference/elasticsearch/index-settings
|
||||
Destination: &conf.Aliases,
|
||||
Aliases: []string{"A"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "ilm-policy",
|
||||
Usage: "ilm policy to apply",
|
||||
Destination: &conf.Policy,
|
||||
Aliases: []string{"p"},
|
||||
},
|
||||
},
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Version string = `v0.0.21`
|
||||
Version string = `v0.0.22`
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -51,6 +51,7 @@ type Config struct {
|
||||
Failed, Partials bool // index: flags
|
||||
Shards, Replicas int // index create+allocation: -s -r
|
||||
Wait bool // index create: -w
|
||||
Policy string // index create: -p
|
||||
Primary bool // index allocation: -p
|
||||
Searchable bool // index fields: -s
|
||||
Aggretable bool // index fields: -a
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
|
||||
"codeberg.org/scip/esctl/pkg/cfg"
|
||||
"codeberg.org/scip/esctl/pkg/printer"
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/indices"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/healthstatus"
|
||||
@@ -131,6 +132,9 @@ func IndexShow(conf *cfg.Config, indexpattern string) error {
|
||||
return fmt.Errorf("failed to get index: %s", esErrorString(err))
|
||||
}
|
||||
|
||||
slog.Debug("index show", "index", res)
|
||||
repr.Println(res)
|
||||
|
||||
for name, index := range res {
|
||||
fields := make([]string, len(index.Mappings.Properties))
|
||||
idx := 0
|
||||
@@ -139,7 +143,7 @@ func IndexShow(conf *cfg.Config, indexpattern string) error {
|
||||
idx++
|
||||
}
|
||||
|
||||
table := printer.NewTable(conf, 2, 5)
|
||||
table := printer.NewTable(conf, 2, 7)
|
||||
table.Addheaders("index property", "value")
|
||||
|
||||
ts, err := strconv.ParseInt(index.Settings.Index.CreationDate.(string), 10, 64)
|
||||
@@ -155,9 +159,17 @@ func IndexShow(conf *cfg.Config, indexpattern string) error {
|
||||
{"shards", *index.Settings.Index.NumberOfShards},
|
||||
{"created", created.Format("2006-01-02 15:04:05")},
|
||||
{"uuid", *index.Settings.Index.Uuid},
|
||||
{"version", *index.Settings.Index.Version.Created},
|
||||
{"fields", strings.Join(fields, ",")},
|
||||
}
|
||||
|
||||
if index.Settings.Index.Lifecycle != nil {
|
||||
table.Entries = append(table.Entries, [][]string{
|
||||
{"ilm policy", *index.Settings.Index.Lifecycle.Name},
|
||||
{"ilm rollover alias", *index.Settings.Index.Lifecycle.RolloverAlias},
|
||||
}...)
|
||||
}
|
||||
|
||||
if err := table.Print(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -178,10 +190,10 @@ func IndexCreate(conf *cfg.Config, index string, mappings []string) error {
|
||||
}
|
||||
|
||||
if conf.Shards > 0 {
|
||||
settings = settings.NumberOfShards(strconv.Itoa(conf.Shards))
|
||||
settings.NumberOfShards(strconv.Itoa(conf.Shards))
|
||||
}
|
||||
if conf.Replicas > 0 {
|
||||
settings = settings.NumberOfReplicas(strconv.Itoa(conf.Replicas))
|
||||
settings.NumberOfReplicas(strconv.Itoa(conf.Replicas))
|
||||
}
|
||||
|
||||
if len(mappings) > 0 {
|
||||
@@ -208,6 +220,13 @@ func IndexCreate(conf *cfg.Config, index string, mappings []string) error {
|
||||
create.Mappings(maps)
|
||||
}
|
||||
|
||||
if conf.Policy != "" {
|
||||
settings.Lifecycle(
|
||||
esdsl.NewIndexSettingsLifecycle().
|
||||
Name(conf.Policy).
|
||||
RolloverAlias(index))
|
||||
}
|
||||
|
||||
_, err := create.Settings(settings).
|
||||
Do(context.Background())
|
||||
|
||||
@@ -281,6 +300,7 @@ func IndexAllocation(conf *cfg.Config, index string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func IndexModify(conf *cfg.Config, index string) error {
|
||||
settings := esdsl.NewIndexSettings().NumberOfReplicas(strconv.Itoa(conf.Replicas))
|
||||
|
||||
@@ -294,6 +314,7 @@ func IndexModify(conf *cfg.Config, index string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
func IndexFields(conf *cfg.Config, index string) error {
|
||||
res, err := conf.DefaultCluster.ES().FieldCaps().
|
||||
|
||||
@@ -157,11 +157,17 @@ func IndexTemplateCreate(conf *cfg.Config, name string, mappings []string) error
|
||||
create := conf.DefaultCluster.ES().Indices.PutIndexTemplate(name)
|
||||
|
||||
if conf.Shards > 0 {
|
||||
settings = settings.NumberOfShards(strconv.Itoa(conf.Shards))
|
||||
settings.NumberOfShards(strconv.Itoa(conf.Shards))
|
||||
}
|
||||
|
||||
if conf.Replicas > 0 {
|
||||
settings = settings.NumberOfReplicas(strconv.Itoa(conf.Replicas))
|
||||
settings.NumberOfReplicas(strconv.Itoa(conf.Replicas))
|
||||
}
|
||||
|
||||
if conf.Policy != "" {
|
||||
settings.Lifecycle(
|
||||
esdsl.NewIndexSettingsLifecycle().
|
||||
Name(conf.Policy))
|
||||
}
|
||||
|
||||
if conf.Stream {
|
||||
@@ -284,6 +290,10 @@ func IndexTemplateModify(conf *cfg.Config, name string, mappings []string) error
|
||||
*settings.Mode = conf.Mode
|
||||
}
|
||||
|
||||
if conf.Policy != "" {
|
||||
settings.Lifecycle.Name = &conf.Policy
|
||||
}
|
||||
|
||||
if len(mappings) > 0 {
|
||||
typemaps, err := modMappings(mappings)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user