/* 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" "strings" "codeberg.org/scip/esctl/pkg/cfg" "github.com/elastic/go-elasticsearch/v9/typedapi/core/search" "github.com/elastic/go-elasticsearch/v9/typedapi/esdsl" "github.com/elastic/go-elasticsearch/v9/typedapi/types" ) /* 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, q string) error { query := esdsl.NewBoolQuery(). Must(esdsl.NewMatchQuery("message", q)) filters := make([]types.QueryVariant, len(conf.Filter)) for idx, filter := range conf.Filter { parts := strings.Split(filter, "=") if len(parts) != 2 { return fmt.Errorf("invalid filter spec: %s, expecting key=value", filter) } filters[idx] = esdsl.NewTermQuery(parts[0], esdsl.NewFieldValue().String(parts[1])) } if len(filters) > 0 { query.Filter(filters...) } res, err := conf.ES.Search(). Index(conf.Index). Request(&search.Request{ Query: query.QueryCaster(), From: &conf.From, Size: &conf.To, }). Do(context.Background()) if err != nil { return fmt.Errorf("Error running search (esdsl): %s", err) } slog.Debug("ES result", "search", res) for _, hit := range res.Hits.Hits { fmt.Printf("%s\n", hit.Source_) } return nil }