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