mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 08:04:17 +02:00
Merge branch 'main' of ssh://codeberg.org/scip/esctl
This commit is contained in:
@@ -21,9 +21,12 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -87,3 +90,43 @@ func DocShow(conf *cfg.Config, id string) error {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -44,24 +44,26 @@ Execute an ES search.
|
||||
additional filters can be given as -F key=value
|
||||
*/
|
||||
func Search(conf *cfg.Config, queries []string) error {
|
||||
search := conf.DefaultCluster.ES.Search().
|
||||
searchEs := conf.DefaultCluster.ES.Search().
|
||||
Index(conf.Index)
|
||||
|
||||
req, err := prepareQuery(conf, queries)
|
||||
queryCaster, err := prepareQuery(conf, queries)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
search.Request(req)
|
||||
req := &search.Request{Query: queryCaster}
|
||||
|
||||
searchEs.Request(req)
|
||||
|
||||
switch conf.Tail {
|
||||
case true:
|
||||
return searchTail(conf, search)
|
||||
return searchTail(conf, searchEs)
|
||||
case false:
|
||||
if conf.To > MAXPAGE {
|
||||
return searchPit(conf, req)
|
||||
} else {
|
||||
return searchOnce(conf, search)
|
||||
return searchOnce(conf, searchEs)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import (
|
||||
"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"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/operator"
|
||||
@@ -119,7 +118,7 @@ func mkMatchQueries(queries []string, op operator.Operator) ([]types.QueryVarian
|
||||
}
|
||||
|
||||
// Prepare user search query and turn it into a proper search request
|
||||
func prepareQuery(conf *cfg.Config, queries []string) (*search.Request, error) {
|
||||
func prepareQuery(conf *cfg.Config, queries []string) (*types.Query, error) {
|
||||
// logical operator for simple and multimatch queries
|
||||
op := operator.Operator{Name: "AND"}
|
||||
if conf.Or {
|
||||
@@ -170,10 +169,7 @@ func prepareQuery(conf *cfg.Config, queries []string) (*search.Request, error) {
|
||||
query.Filter(filters...)
|
||||
}
|
||||
|
||||
// this being fed into ES.Serach().Req()
|
||||
return &search.Request{
|
||||
Query: query.QueryCaster(),
|
||||
}, nil
|
||||
return query.QueryCaster(), nil
|
||||
}
|
||||
|
||||
// Build a slice of filters, to be fed into query.Filter() including static ones
|
||||
|
||||
Reference in New Issue
Block a user