/* 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" "fmt" "log/slog" "codeberg.org/scip/esctl/pkg/cfg" "github.com/tidwall/gjson" ) /* Execute an ES search. q is the actual search query given as arg to the 'search' cmd additional filters can be given as -F key=value */ func Search(conf *cfg.Config, queries []string) error { search := conf.DefaultCluster.ES.Search(). Index(conf.Index) if len(queries) > 0 { req, err := prepareQuery(conf, queries) if err != nil { return err } search.Request(req) } res, err := search.Do(context.Background()) if err != nil { return fmt.Errorf("failed to run search (esdsl): %s", err) } slog.Debug("ES result", "search", res) for _, hit := range res.Hits.Hits { docjson := fmt.Sprintf(`{"id":%s, "score":%0.4f, "index":"%s", "source":%s}`, *hit.Id_, *hit.Score_, hit.Index_, hit.Source_) if conf.Path != "" { value := gjson.Get(docjson, conf.Path) fmt.Println(value.String()) } else { fmt.Println(docjson) } } return nil }