add doc show, enhance search, add jsonpath to search and doc show

This commit is contained in:
T. von Dein
2026-05-22 13:50:17 +02:00
parent 89e2498aad
commit 98a577f68a
10 changed files with 348 additions and 52 deletions

View File

@@ -19,10 +19,12 @@ package es
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"codeberg.org/scip/esctl/pkg/cfg"
"github.com/tidwall/gjson"
)
// create index with:
@@ -32,7 +34,7 @@ import (
// then add a doc:
//
// esctl doc add foo2 '{"id":"d8d8d","user":"scip"}'
func DocAdd(conf *cfg.Config, index, jsondoc string) error {
func DocAdd(conf *cfg.Config, jsondoc string) error {
data := map[string]any{}
err := json.Unmarshal([]byte(jsondoc), &data)
@@ -42,14 +44,46 @@ func DocAdd(conf *cfg.Config, index, jsondoc string) error {
now := fmt.Sprintf("%d", time.Now().Unix())
_, err = conf.DefaultCluster.ES.Create(index, now).
res, err := conf.DefaultCluster.ES.Create(conf.Index, now).
Document(data).
Header("content-type", "application/json").
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to create new doc in index %s: %s", index, err)
return fmt.Errorf("failed to create new doc in index %s: %s", conf.Index, err)
}
fmt.Println(res.Id_)
return nil
}
func DocShow(conf *cfg.Config, id string) error {
res, err := conf.DefaultCluster.ES.Get(conf.Index, id).
Header("content-type", "application/json").
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to retrieve doc in index %s: %s", conf.Index, err)
}
if !res.Found {
return errors.New("no document with that id found")
}
docjson := fmt.Sprintf(`{"id":%s, "index":"%s", "source":%s}`,
res.Id_,
res.Index_,
res.Source_)
if conf.Path != "" {
value := gjson.Get(docjson, conf.Path)
fmt.Println(value.String())
} else {
fmt.Println(docjson)
}
fmt.Println()
return nil
}