2026-05-12 18:28:57 +02:00
|
|
|
/*
|
|
|
|
|
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 (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2026-05-22 13:50:17 +02:00
|
|
|
"errors"
|
2026-05-12 18:28:57 +02:00
|
|
|
"fmt"
|
2026-05-29 14:42:36 +02:00
|
|
|
"math/rand/v2"
|
2026-05-29 11:05:57 +02:00
|
|
|
"strings"
|
2026-05-12 18:28:57 +02:00
|
|
|
|
|
|
|
|
"codeberg.org/scip/esctl/pkg/cfg"
|
2026-05-29 11:05:57 +02:00
|
|
|
"github.com/elastic/go-elasticsearch/v9/typedapi/core/deletebyquery"
|
|
|
|
|
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
2026-05-22 13:50:17 +02:00
|
|
|
"github.com/tidwall/gjson"
|
2026-05-12 18:28:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// create index with:
|
|
|
|
|
//
|
|
|
|
|
// esctl index create foo [id:keyword user:text age:integer]
|
|
|
|
|
//
|
|
|
|
|
// then add a doc:
|
|
|
|
|
//
|
|
|
|
|
// esctl doc add foo2 '{"id":"d8d8d","user":"scip"}'
|
2026-05-22 13:50:17 +02:00
|
|
|
func DocAdd(conf *cfg.Config, jsondoc string) error {
|
2026-05-12 18:28:57 +02:00
|
|
|
data := map[string]any{}
|
|
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(jsondoc), &data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("supplied document was not valid JSON: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 14:42:36 +02:00
|
|
|
now := fmt.Sprintf("%d", rand.Int64())
|
2026-05-12 18:28:57 +02:00
|
|
|
|
2026-05-22 13:50:17 +02:00
|
|
|
res, err := conf.DefaultCluster.ES.Create(conf.Index, now).
|
2026-05-12 18:28:57 +02:00
|
|
|
Document(data).
|
|
|
|
|
Header("content-type", "application/json").
|
|
|
|
|
Header("accept", "application/json").
|
|
|
|
|
Do(context.Background())
|
|
|
|
|
if err != nil {
|
2026-06-01 12:30:41 +02:00
|
|
|
return fmt.Errorf("failed to create new doc in index %s: %s", conf.Index, esErrorString(err))
|
2026-05-12 18:28:57 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 13:50:17 +02:00
|
|
|
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 {
|
2026-06-01 12:30:41 +02:00
|
|
|
return fmt.Errorf("failed to retrieve doc in index %s: %s", conf.Index, esErrorString(err))
|
2026-05-22 13:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
2026-05-12 18:28:57 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-05-29 11:05:57 +02:00
|
|
|
|
|
|
|
|
func DocDelete(conf *cfg.Config, queries []string) error {
|
|
|
|
|
if len(queries) == 1 && strings.Contains(queries[0], "id=") {
|
|
|
|
|
id, _ := strings.CutPrefix(queries[0], "id=")
|
|
|
|
|
|
|
|
|
|
_, err := conf.DefaultCluster.ES.Delete(conf.Index, id).
|
|
|
|
|
Header("content-type", "application/json").
|
|
|
|
|
Header("accept", "application/json").
|
|
|
|
|
Do(context.Background())
|
|
|
|
|
if err != nil {
|
2026-06-01 12:30:41 +02:00
|
|
|
return fmt.Errorf("failed to delete doc in index %s: %s", conf.Index, esErrorString(err))
|
2026-05-29 11:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := &deletebyquery.Request{}
|
|
|
|
|
|
|
|
|
|
if len(queries) == 0 && conf.All {
|
|
|
|
|
req.Query = esdsl.NewMatchAllQuery().QueryCaster()
|
|
|
|
|
} else {
|
|
|
|
|
queryCaster, err := prepareQuery(conf, queries)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Query = queryCaster
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := conf.DefaultCluster.ES.DeleteByQuery(conf.Index).
|
|
|
|
|
Request(req).
|
|
|
|
|
Header("content-type", "application/json").
|
|
|
|
|
Header("accept", "application/json").
|
|
|
|
|
Do(context.Background())
|
|
|
|
|
if err != nil {
|
2026-06-01 12:30:41 +02:00
|
|
|
return fmt.Errorf("failed to delete docs in index %s: %s", conf.Index, esErrorString(err))
|
2026-05-29 11:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|