/* 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 . */ package es import ( "context" "encoding/json" "errors" "fmt" "strings" "time" "codeberg.org/scip/esctl/pkg/cfg" "github.com/elastic/go-elasticsearch/v9/typedapi/core/deletebyquery" "github.com/elastic/go-elasticsearch/v9/typedapi/esdsl" "github.com/tidwall/gjson" ) // 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"}' func DocAdd(conf *cfg.Config, jsondoc string) error { data := map[string]any{} err := json.Unmarshal([]byte(jsondoc), &data) if err != nil { return fmt.Errorf("supplied document was not valid JSON: %s", err) } now := fmt.Sprintf("%d", time.Now().Unix()) 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", 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 } 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 { return fmt.Errorf("failed to delete doc in index %s: %s", conf.Index, err) } 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 { return fmt.Errorf("failed to delete docs in index %s: %s", conf.Index, err) } return nil }