mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 04:54:18 +02:00
shards, aliases and filtering (#18)
This commit is contained in:
@@ -20,11 +20,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"codeberg.org/scip/esctl/pkg/cfg"
|
||||
"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"
|
||||
)
|
||||
@@ -48,6 +50,24 @@ func IndexNames(conf *cfg.Config) ([]string, error) {
|
||||
return indices, nil
|
||||
}
|
||||
|
||||
func filterIndices(conf *cfg.Config, list indices.Response) indices.Response {
|
||||
if len(conf.Filter) == 0 {
|
||||
return list
|
||||
}
|
||||
|
||||
// we support just one filter here, for now
|
||||
filter := *regexp.MustCompile(conf.Filter[0])
|
||||
newlist := indices.Response{}
|
||||
|
||||
for _, index := range list {
|
||||
if filter.MatchString(*index.Index) {
|
||||
newlist = append(newlist, index)
|
||||
}
|
||||
}
|
||||
|
||||
return newlist
|
||||
}
|
||||
|
||||
func IndexList(conf *cfg.Config) error {
|
||||
cat := conf.DefaultCluster.ES.Cat.Indices().
|
||||
// we need to add custom request headers, required for older ES instances
|
||||
@@ -65,7 +85,9 @@ func IndexList(conf *cfg.Config) error {
|
||||
|
||||
slog.Debug("ES result", "indicies", res)
|
||||
|
||||
size := len(res)
|
||||
list := filterIndices(conf, res)
|
||||
|
||||
size := len(list)
|
||||
|
||||
if conf.MaxItems > 0 {
|
||||
if size > conf.MaxItems {
|
||||
@@ -76,7 +98,7 @@ func IndexList(conf *cfg.Config) error {
|
||||
table := NewTable(3, size)
|
||||
table.Addheaders("name", "size", "docscount")
|
||||
|
||||
for idx, index := range res {
|
||||
for idx, index := range list {
|
||||
name := Colorize(*index.Health, *index.Index)
|
||||
|
||||
table.entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
||||
@@ -95,10 +117,6 @@ func IndexList(conf *cfg.Config) error {
|
||||
}
|
||||
|
||||
func IndexShow(conf *cfg.Config, index string) error {
|
||||
if index == "" {
|
||||
return fmt.Errorf("no index specified")
|
||||
}
|
||||
|
||||
res, err := conf.DefaultCluster.ES.Indices.Get(index).
|
||||
// we need to add custom request headers, required for older ES instances
|
||||
Header("content-type", "application/json").
|
||||
@@ -188,10 +206,6 @@ func IndexCreate(conf *cfg.Config, index string, mappings []string) error {
|
||||
}
|
||||
|
||||
func IndexDelete(conf *cfg.Config, index string) error {
|
||||
if index == "" {
|
||||
return fmt.Errorf("no index specified")
|
||||
}
|
||||
|
||||
_, err := conf.DefaultCluster.ES.Indices.Delete(index).
|
||||
Header("content-type", "application/json").
|
||||
Header("accept", "application/json").
|
||||
@@ -218,16 +232,11 @@ func IndexClose(conf *cfg.Config, index string) error {
|
||||
}
|
||||
|
||||
func IndexAllocation(conf *cfg.Config, index string) error {
|
||||
if index == "" {
|
||||
return fmt.Errorf("no index specified")
|
||||
}
|
||||
|
||||
alloc := conf.DefaultCluster.ES.Cluster.AllocationExplain().
|
||||
res, err := conf.DefaultCluster.ES.Cluster.AllocationExplain().
|
||||
Index(index).
|
||||
Primary(conf.Primary).
|
||||
Shard(conf.Shards)
|
||||
|
||||
res, err := alloc.Header("content-type", "application/json").
|
||||
Shard(conf.Shards).
|
||||
Header("content-type", "application/json").
|
||||
Header("accept", "application/json").
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
@@ -265,3 +274,19 @@ 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))
|
||||
|
||||
_, err := conf.DefaultCluster.ES.Indices.PutSettings().
|
||||
Indices(index).
|
||||
Index(settings).
|
||||
Header("content-type", "application/json").
|
||||
Header("accept", "application/json").
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to modify index settings: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user