add sort support to search, add index fields command, fix error messages (#25)

This commit is contained in:
T. von Dein
2026-06-01 12:30:41 +02:00
parent f1877c6903
commit 504db9835d
19 changed files with 233 additions and 63 deletions

View File

@@ -40,7 +40,7 @@ func IndexNames(conf *cfg.Config) ([]string, error) {
Do(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get indicies: %s", err)
return nil, fmt.Errorf("failed to get indicies: %s", esErrorString(err))
}
indices := make([]string, len(res))
@@ -81,7 +81,7 @@ func IndexList(conf *cfg.Config) error {
res, err := cat.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get indicies: %s", err)
return fmt.Errorf("failed to get indicies: %s", esErrorString(err))
}
slog.Debug("ES result", "indicies", res)
@@ -124,7 +124,7 @@ func IndexShow(conf *cfg.Config, index string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get index: %s", err)
return fmt.Errorf("failed to get index: %s", esErrorString(err))
}
slog.Debug("ES result", "index", res)
@@ -208,7 +208,7 @@ func IndexCreate(conf *cfg.Config, index string, mappings []string) error {
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to create index: %s", err)
return fmt.Errorf("failed to create index: %s", esErrorString(err))
}
return nil
@@ -220,7 +220,7 @@ func IndexDelete(conf *cfg.Config, index string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to delete index: %s", err)
return fmt.Errorf("failed to delete index: %s", esErrorString(err))
}
return nil
@@ -234,7 +234,7 @@ func IndexClose(conf *cfg.Config, index string) error {
_, err := create.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to close index: %s", err)
return fmt.Errorf("failed to close index: %s", esErrorString(err))
}
return nil
@@ -249,7 +249,7 @@ func IndexAllocation(conf *cfg.Config, index string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get index allocation explain: %s", err)
return fmt.Errorf("failed to get index allocation explain: %s", esErrorString(err))
}
slog.Debug("ES result", "index", res)
@@ -294,7 +294,44 @@ func IndexModify(conf *cfg.Config, index string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to modify index settings: %s", err)
return fmt.Errorf("failed to modify index settings: %s", esErrorString(err))
}
return nil
}
func IndexFields(conf *cfg.Config, index string) error {
res, err := conf.DefaultCluster.ES.FieldCaps().
Index(index).
Fields("*").
Header("content-type", "application/json").
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to retrieve field capabilties: %s", esErrorString(err))
}
table := printer.NewTable(conf, 5, len(res.Fields))
table.Addheaders("field", "type", "searchable", "aggretable", "metadata")
idx := 0
for name, field := range res.Fields {
for fieldtype, caps := range field {
// fields only have 1 type, so this one is it
table.Entries[idx] = []string{name, fieldtype,
fmt.Sprintf("%t", caps.Searchable),
fmt.Sprintf("%t", caps.Aggregatable),
fmt.Sprintf("%t", *caps.MetadataField)}
break
}
idx++
}
table.Sort()
if err := table.Print(); err != nil {
return err
}
return nil