-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
79 lines (72 loc) · 1.39 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"io"
"os"
"strings"
"github.com/charmbracelet/log"
"github.com/mattn/go-isatty"
)
type obj map[string]any
func (d *dumper) createQuery() {
q := make(obj)
if !isatty.IsTerminal(os.Stdin.Fd()) {
in, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal("reading from stdin", "err", err)
}
log.Info("read query from stdin", "bytes", len(in))
err = json.Unmarshal(in, &q)
if err != nil {
log.Fatal("parsing query from stdin", "err", err)
}
}
if _, ok := q["_source"]; !ok && d.fields != "" {
exclude := false
if strings.HasPrefix(d.fields, "^") {
exclude = true
d.fields = d.fields[1:]
}
fieldsList := strings.Split(d.fields, ",")
if exclude {
q["_source"] = obj{
"excludes": fieldsList,
}
} else {
q["_source"] = fieldsList
}
}
if d.metadataOnly {
q["_source"] = false
}
if _, ok := q["size"]; !ok {
q["size"] = d.size
}
if _, ok := q["sort"]; !ok {
q["sort"] = []string{"_doc"}
}
if _, ok := q["query"]; !ok {
if d.queryString != "" {
q["query"] = obj{
"query_string": obj{
"query": d.queryString,
},
}
} else {
q["query"] = obj{
"match_all": obj{},
}
}
}
if d.random {
q["query"] = obj{
"function_score": obj{
"query": q["query"],
"random_score": obj{},
"boost_mode": "replace",
},
}
q["sort"] = []string{"_score"}
}
d.query = q
}