mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 07:54:17 +02:00
add search validate+explain (#31)
This commit is contained in:
@@ -70,6 +70,7 @@ type Config struct {
|
||||
Range string // search: -r
|
||||
TimestampFormat string // search: --timestamp-format
|
||||
Explain bool // search: -e
|
||||
Validate bool // search: --validate
|
||||
SortBy string // sort: -k
|
||||
Ascending bool // sort: -a
|
||||
Exclude string // cluster compare: -e (regexp)
|
||||
|
||||
@@ -18,6 +18,7 @@ package es
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
@@ -28,8 +29,10 @@ import (
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/core/search"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/indices/validatequery"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/sortorder"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -43,8 +46,11 @@ Execute an ES search.
|
||||
additional filters can be given as -F key=value
|
||||
*/
|
||||
func Search(conf *cfg.Config, queries []string) error {
|
||||
searchEs := conf.DefaultCluster.ES.Search().
|
||||
Index(conf.Index)
|
||||
if conf.Validate {
|
||||
return validateSearch(conf, queries)
|
||||
}
|
||||
|
||||
searchEs := conf.DefaultCluster.ES.Search().Index(conf.Index)
|
||||
|
||||
queryCaster, err := prepareQuery(conf, queries)
|
||||
if err != nil {
|
||||
@@ -57,9 +63,11 @@ func Search(conf *cfg.Config, queries []string) error {
|
||||
|
||||
searchEs = addSort(conf, searchEs)
|
||||
|
||||
switch conf.Tail {
|
||||
case true:
|
||||
switch {
|
||||
case conf.Tail:
|
||||
return searchTail(conf, searchEs)
|
||||
case conf.Explain:
|
||||
return explainSearch(conf, searchEs)
|
||||
default:
|
||||
if conf.To > MAXPAGE {
|
||||
return searchPit(conf, req)
|
||||
@@ -69,6 +77,79 @@ func Search(conf *cfg.Config, queries []string) error {
|
||||
}
|
||||
}
|
||||
|
||||
func explainSearch(conf *cfg.Config, search *search.Search) error {
|
||||
res, err := search.
|
||||
Explain(true).
|
||||
Size(1). // one's enough for explain
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to call explain search (esdsl): %s", esErrorString(err))
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal explain result: %s", err)
|
||||
}
|
||||
|
||||
if conf.Debug {
|
||||
repr.Println(res)
|
||||
value := gjson.Get(string(raw), "hits.hits.0._explanation")
|
||||
fmt.Println(value.String())
|
||||
}
|
||||
|
||||
if len(res.Hits.Hits) > 0 {
|
||||
ex := res.Hits.Hits[0].Explanation_
|
||||
fmt.Println(ex.Description)
|
||||
fmt.Println(ex.Value)
|
||||
|
||||
// recurse into explanation details (it's a tree)
|
||||
for _, ex := range ex.Details {
|
||||
explain(&ex, " ")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func explain(res *types.ExplanationDetail, indent string) {
|
||||
fmt.Println(indent + "- " + res.Description)
|
||||
for _, ex := range res.Details {
|
||||
fmt.Println(indent + " - " + ex.Description)
|
||||
fmt.Println(indent + fmt.Sprintf(" score: %f", res.Value))
|
||||
|
||||
explain(&ex, indent+" ")
|
||||
}
|
||||
}
|
||||
|
||||
func validateSearch(conf *cfg.Config, queries []string) error {
|
||||
validate := conf.DefaultCluster.ES.Indices.ValidateQuery()
|
||||
|
||||
queryCaster, err := prepareQuery(conf, queries)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req := &validatequery.Request{Query: queryCaster}
|
||||
|
||||
validate.Request(req)
|
||||
|
||||
res, err := validate.
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to validate search (esdsl): %s", esErrorString(err))
|
||||
}
|
||||
|
||||
slog.Debug("ES result", "search", res)
|
||||
|
||||
if res.Valid {
|
||||
fmt.Println(printer.Colorize(conf, "green", "valid"))
|
||||
} else {
|
||||
fmt.Println(printer.Colorize(conf, "red", "invalid"))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Debug(conf *cfg.Config) error {
|
||||
res, err := conf.DefaultCluster.ES.Search().
|
||||
Index(conf.Index).
|
||||
|
||||
Reference in New Issue
Block a user