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

@@ -28,8 +28,11 @@ Command tree:
list
set
status
debug
doc
add
delete
show
help
index
alias
@@ -40,6 +43,7 @@ Command tree:
close
create
delete
fields
list
modify
show

24
TODO.md
View File

@@ -4,8 +4,30 @@
- Fix index names custom completion
https://github.com/urfave/cli/issues/2332
https://github.com/urfave/cli/issues/2333
- index show: add more details, see screenshots
- add shard explain, aka:
get /_cluster/allocation/explain {"index":"yourindex", "primary": true, "shard":0}
- add validate:
> GET /mock/_validate/query?rewrite=true {"from":0,"query":{"bool":{"must":[{"match_all":{}}]}},"size":20,"sort":[{"name":{"order":"desc"}}]}
{
"valid": false
}
- add explain to search (maybe option -e)
> GET /mock/_explain/1780043878 {"query":{"bool":{"must":[{"match_all":{}}]}}}
{
"_index": "mock",
"_id": "1780043878",
"matched": true,
"explanation": {
"value": 1.0,
"description": "*:*",
"details": []
}
}

View File

@@ -42,6 +42,7 @@ func Index(conf *cfg.Config) *cli.Command {
IndexAllocation(conf),
IndexModify(conf),
IndexAlias(conf),
IndexFields(conf),
},
}
}
@@ -157,6 +158,8 @@ func IndexCreate(conf *cfg.Config) *cli.Command {
Name: "create",
Aliases: []string{"+"},
Usage: "create a new index",
UsageText: `create <name> <fieldmapping:type>...
Valid field mapping types: integer, text, date, keyword`,
Flags: []cli.Flag{
&cli.BoolFlag{
@@ -250,3 +253,20 @@ func IndexModify(conf *cfg.Config) *cli.Command {
},
}
}
func IndexFields(conf *cfg.Config) *cli.Command {
return &cli.Command{
Name: "fields",
Usage: "show info about field capabilities",
UsageText: "index fields <index>",
Action: func(ctx context.Context, cmd *cli.Command) error {
index := cmd.Args().Get(0)
if index == "" {
return errors.New("no index specified")
}
return es.IndexFields(conf, index)
},
}
}

View File

@@ -100,6 +100,19 @@ func Search(conf *cfg.Config) *cli.Command {
Destination: &conf.TimestampFormat,
Value: "strict_date_hour_minute",
},
&cli.StringFlag{
Name: "sort-by",
Usage: "sort by a field",
Destination: &conf.SortBy,
Value: "@timestamp",
Aliases: []string{"k"},
},
&cli.BoolFlag{
Name: "ascending",
Usage: "sort in ascending order (default: descending)",
Destination: &conf.Ascending,
Aliases: []string{"a"},
},
&cli.BoolFlag{
Name: "help-jsonpath",
Usage: "show jsonPath help",

View File

@@ -34,7 +34,7 @@ import (
)
const (
Version string = `v0.0.14`
Version string = `v0.0.15`
)
var (
@@ -67,6 +67,9 @@ type Config struct {
Or bool // search: -O
Range string // search: -r
TimestampFormat string // search: --timestamp-format
Explain bool // search: -e
SortBy string // sort: -k
Ascending bool // sort: -a
Exclude string // cluster compare: -e (regexp)
All, Verbose bool // cluster status: -a -v
Persistent, Transient, Default bool // -p -t -D cluster settings set

View File

@@ -23,7 +23,7 @@ import (
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"codeberg.org/scip/esctl/pkg/printer"
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
)
@@ -62,7 +62,7 @@ func CcrStatus(conf *cfg.Config, leader, follower string) error {
res, err := cat.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get indicies on %s: %s", alias, err)
return fmt.Errorf("failed to get indicies on %s: %s", alias, esErrorString(err))
}
indices[alias] = make(map[string]*types.IndicesRecord, len(res))
@@ -93,7 +93,7 @@ func CcrRemoteInfo(conf *cfg.Config, index string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to retrieve follower info: %s", err)
return fmt.Errorf("failed to retrieve follower info: %s", esErrorString(err))
}
slog.Debug("ccr remote info", "info", res)

View File

@@ -22,7 +22,7 @@ import (
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"codeberg.org/scip/esctl/pkg/printer"
)
func getRemoteName(conf *cfg.Config) (string, error) {
@@ -31,7 +31,7 @@ func getRemoteName(conf *cfg.Config) (string, error) {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return "", fmt.Errorf("failed to retrieve follower info: %s", err)
return "", fmt.Errorf("failed to retrieve follower info: %s", esErrorString(err))
}
remote := ""
@@ -41,7 +41,7 @@ func getRemoteName(conf *cfg.Config) (string, error) {
}
if remote == "" {
return "", fmt.Errorf("cluster doesn't have a follower: %s", err)
return "", fmt.Errorf("cluster doesn't have a follower")
}
return remote, nil
@@ -96,7 +96,7 @@ func CcrFollowerResume(conf *cfg.Config, index string) error {
_, err := create.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to resume ccr following: %s", err)
return fmt.Errorf("failed to resume ccr following: %s", esErrorString(err))
}
return nil
@@ -110,7 +110,7 @@ func CcrFollowerPause(conf *cfg.Config, index string) error {
_, err := create.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to pause ccr following: %s", err)
return fmt.Errorf("failed to pause ccr following: %s", esErrorString(err))
}
return nil
@@ -124,7 +124,7 @@ func CcrFollowerUnfollow(conf *cfg.Config, index string) error {
_, err := create.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to unfollow index: %s", err)
return fmt.Errorf("failed to unfollow index: %s", esErrorString(err))
}
return nil
@@ -149,7 +149,7 @@ func CcrFollowerAdd(conf *cfg.Config, index string) error {
_, err = create.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to create follower index: %s", err)
return fmt.Errorf("failed to create follower index: %s", esErrorString(err))
}
return nil
@@ -161,7 +161,7 @@ func CcrFollowerShow(conf *cfg.Config, index string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to retrieve follower index info: %s", err)
return fmt.Errorf("failed to retrieve follower index info: %s", esErrorString(err))
}
slog.Debug("ES result", "follower stats", res.Indices)

View File

@@ -23,7 +23,7 @@ import (
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"codeberg.org/scip/esctl/pkg/printer"
"github.com/urfave/cli/v3"
)
@@ -63,7 +63,7 @@ func ClusterSettingsList(conf *cfg.Config) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get cluster settings: %s", err)
return fmt.Errorf("failed to get cluster settings: %s", esErrorString(err))
}
table := printer.NewTable(conf, 2, 0)
@@ -135,7 +135,7 @@ func ClusterSettingsSet(conf *cfg.Config, args cli.Args) error {
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to set settings: %s", err)
return fmt.Errorf("failed to set settings: %s", esErrorString(err))
}
return nil
@@ -154,7 +154,7 @@ func ClusterSettingsSetSingle(conf *cfg.Config, setting, value string) error {
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to set %s: %s", setting, err)
return fmt.Errorf("failed to set %s: %s", setting, esErrorString(err))
}
return nil

View File

@@ -41,7 +41,7 @@ func checkClusterIsLeader(conf *cfg.Config, leader string) bool {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
fmt.Printf("failed to get ccr stats from %s: %s", leader, err)
fmt.Printf("failed to get ccr stats from %s: %s", leader, esErrorString(err))
return false
}
@@ -68,7 +68,7 @@ func checkClusterStatus(conf *cfg.Config, leader, follower string) bool {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
fmt.Printf("failed to get health from %s: %s", cluster, err)
fmt.Printf("failed to get health from %s: %s", cluster, esErrorString(err))
return false
}
@@ -128,7 +128,7 @@ func findIlmErrors(conf *cfg.Config, leader, follower string) bool {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
fmt.Printf("failed to get ilm status from %s: %s", cluster, err)
fmt.Printf("failed to get ilm status from %s: %s", cluster, esErrorString(err))
return false
}

View File

@@ -53,7 +53,7 @@ func DocAdd(conf *cfg.Config, jsondoc string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to create new doc in index %s: %s", conf.Index, err)
return fmt.Errorf("failed to create new doc in index %s: %s", conf.Index, esErrorString(err))
}
fmt.Println(res.Id_)
@@ -67,7 +67,7 @@ func DocShow(conf *cfg.Config, id string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to retrieve doc in index %s: %s", conf.Index, err)
return fmt.Errorf("failed to retrieve doc in index %s: %s", conf.Index, esErrorString(err))
}
if !res.Found {
@@ -100,7 +100,7 @@ func DocDelete(conf *cfg.Config, queries []string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to delete doc in index %s: %s", conf.Index, err)
return fmt.Errorf("failed to delete doc in index %s: %s", conf.Index, esErrorString(err))
}
return nil
@@ -125,7 +125,7 @@ func DocDelete(conf *cfg.Config, queries []string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to delete docs in index %s: %s", conf.Index, err)
return fmt.Errorf("failed to delete docs in index %s: %s", conf.Index, esErrorString(err))
}
return nil

40
pkg/es/errors.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2026 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package es
import (
"fmt"
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
)
func esErrorString(err error) string {
msg := err.Error()
switch e := err.(type) {
case *types.ElasticsearchError:
causes := ""
for _, cause := range e.ErrorCause.RootCause {
causes += fmt.Sprintf("%s\n", *cause.Reason)
}
msg = *e.ErrorCause.Reason + ": " + causes
}
return msg
}

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

View File

@@ -37,7 +37,7 @@ func IndexAliasCreate(conf *cfg.Config, index, alias string) error {
slog.Debug("create alias", "result", res)
if err != nil {
return fmt.Errorf("failed to create index alias: %s", err)
return fmt.Errorf("failed to create index alias: %s", esErrorString(err))
}
return nil
@@ -58,7 +58,7 @@ func IndexAliasList(conf *cfg.Config) error {
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to list index aliases: %s", err)
return fmt.Errorf("failed to list index aliases: %s", esErrorString(err))
}
slog.Debug("aliases list", "result", res)
@@ -108,7 +108,7 @@ func IndexAliasDelete(conf *cfg.Config, index, alias string) error {
slog.Debug("delete alias", "result", res)
if err != nil {
return fmt.Errorf("failed to delete index alias: %s", err)
return fmt.Errorf("failed to delete index alias: %s", esErrorString(err))
}
return nil

View File

@@ -22,14 +22,14 @@ import (
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"codeberg.org/scip/esctl/pkg/printer"
)
func NodeList(conf *cfg.Config) error {
// get nodes
nodes, err := conf.DefaultCluster.ES.Cat.Nodes().Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get nodes: %s", err)
return fmt.Errorf("failed to get nodes: %s", esErrorString(err))
}
slog.Debug("ES result", "nodes", nodes)

View File

@@ -112,7 +112,7 @@ func Repl(conf *cfg.Config) error {
err = CallAPI(conf, parts[0], parts[1], data)
if err != nil {
fmt.Printf("failed to call API: %s\n", err)
fmt.Printf("failed to call API: %s\n", esErrorString(err))
}
reader.SetPrompt("> ")

View File

@@ -21,7 +21,6 @@ import (
"fmt"
"log"
"log/slog"
"strings"
"time"
"codeberg.org/scip/esctl/pkg/cfg"
@@ -56,18 +55,18 @@ func Search(conf *cfg.Config, queries []string) error {
searchEs.Request(req)
searchEs = addSort(conf, searchEs)
switch conf.Tail {
case true:
return searchTail(conf, searchEs)
case false:
default:
if conf.To > MAXPAGE {
return searchPit(conf, req)
} else {
return searchOnce(conf, searchEs)
}
}
return nil
}
func Debug(conf *cfg.Config) error {
@@ -95,11 +94,7 @@ func searchOnce(conf *cfg.Config, search *search.Search) error {
Size(conf.To).
Do(context.Background())
if err != nil {
if strings.Contains(err.Error(), "reason: all shards failed") {
return nil
}
return fmt.Errorf("failed to run search (esdsl): %s", err)
return fmt.Errorf("failed to run search (esdsl): %s", esErrorString(err))
}
slog.Debug("ES result", "search", res)
@@ -134,14 +129,12 @@ func searchPit(conf *cfg.Config, req *search.Request) error {
AddSortOption("_shard_doc", esdsl.NewFieldSort(sortorder.Asc))).
Size(conf.To)
search = addSort(conf, search)
for {
res, err := search.Do(ctx)
if err != nil {
if strings.Contains(err.Error(), "reason: all shards failed") {
return nil
}
return fmt.Errorf("failed to run search (esdsl pit): %s", err)
return fmt.Errorf("failed to run search (esdsl pit): %s", esErrorString(err))
}
if len(res.Hits.Hits) == 0 {
@@ -173,11 +166,7 @@ func searchTail(conf *cfg.Config, search *search.Search) error {
for {
res, err := search.Do(context.Background())
if err != nil {
if strings.Contains(err.Error(), "reason: all shards failed") {
return nil
}
return fmt.Errorf("failed to run search (esdsl): %s", err)
return fmt.Errorf("failed to run search (esdsl): %s", esErrorString(err))
}
slog.Debug("ES result", "search", res)

View File

@@ -17,15 +17,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package es
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"codeberg.org/scip/esctl/pkg/cfg"
"github.com/elastic/go-elasticsearch/v9/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/operator"
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/sortorder"
)
const (
@@ -236,3 +239,42 @@ func addFilters(conf *cfg.Config) ([]types.QueryVariant, error) {
return filters, nil
}
// check if the conf.SortBy field (default: @timestamp) is searchable
// by using the field capabilities API.
func addSort(conf *cfg.Config, search *search.Search) *search.Search {
res, err := conf.DefaultCluster.ES.FieldCaps().
Index(conf.Index).
Fields(conf.SortBy).
Do(context.Background())
if err != nil {
// whatever it was, do not add Sort()
slog.Debug("get field capabilities", "field", conf.SortBy, "error", esErrorString(err))
return search
}
field, exists := res.Fields[conf.SortBy]
if exists {
// good, the field exists
for _, cap := range field {
if cap.Searchable {
// ok, ES would be willing to sort by this field
order := esdsl.NewFieldSort(sortorder.Desc)
if conf.Ascending {
order = esdsl.NewFieldSort(sortorder.Asc)
}
search = search.Sort(
esdsl.NewSortOptions().AddSortOption(conf.SortBy, order),
)
break
}
}
}
return search
}

View File

@@ -88,7 +88,7 @@ func ShardList(conf *cfg.Config) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get shards: %s", err)
return fmt.Errorf("failed to get shards: %s", esErrorString(err))
}
shardlist := filterShards(conf, res)
@@ -140,7 +140,7 @@ func ShardShow(conf *cfg.Config, index string) error {
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get shards: %s", err)
return fmt.Errorf("failed to get shards: %s", esErrorString(err))
}
slog.Debug("ES result", "shards", res)

View File

@@ -25,7 +25,7 @@ import (
"strings"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"codeberg.org/scip/esctl/pkg/printer"
)
var (
@@ -45,7 +45,7 @@ func SnapshotList(conf *cfg.Config) error {
// get partial indicies
ires, err := conf.DefaultCluster.ES.Cat.Indices().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))
}
indicies := map[string]int{}
@@ -58,7 +58,7 @@ func SnapshotList(conf *cfg.Config) error {
// get snapshots
sres, err := conf.DefaultCluster.ES.Cat.Snapshots().Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get snapshots: %s", err)
return fmt.Errorf("failed to get snapshots: %s", esErrorString(err))
}
slog.Debug("ES result", "indicies", sres)
@@ -108,7 +108,7 @@ func SnapshotList(conf *cfg.Config) error {
func SnapshotShow(conf *cfg.Config, snapshot string) error {
res, err := conf.DefaultCluster.ES.Snapshot.Get("*", snapshot).Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get snapshot: %s", err)
return fmt.Errorf("failed to get snapshot: %s", esErrorString(err))
}
slog.Debug("ES result", "snapshot", res)