mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 15:14:18 +02:00
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
|
|
/*
|
||
|
|
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"
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 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, index, 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())
|
||
|
|
|
||
|
|
_, err = conf.DefaultCluster.ES.Create(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 nil
|
||
|
|
}
|