Browse Source

Made search top level, added size and from as params

master
Christopher Ramey 7 months ago
parent
commit
2f1dfe06a6
  1. 48
      index.go
  2. 4
      main.go
  3. 64
      search.go

48
index.go

@ -6,8 +6,6 @@ import (
"fmt"
"io/ioutil"
"os"
elastic "github.com/olivere/elastic/v7"
"encoding/json"
"strings"
)
@ -40,52 +38,6 @@ func handleIndexCommand() {
fmt.Printf("%s\n", index)
}
case "search", "sea", "s":
cmd := flag.NewFlagSet("index search", flag.ExitOnError)
cmd.SetOutput(os.Stdout)
cmd.Usage = func() {
fmt.Printf("Usage: %s index %s [args] <index> <search term(s)>\n",
os.Args[0], subcmd)
cmd.PrintDefaults()
}
cfgpath := cmd.String("config", "", "path to configuration")
server := cmd.String("server", "", "server name to use")
fields := cmd.String("fields", "*", "fields to include in search")
defop := cmd.String("defop", "AND", "default search operator")
cmd.Parse(os.Args[3:])
if *defop != "AND" && *defop != "OR" {
fmt.Fprintf(os.Stderr, "default operator must be AND or OR\n")
cmd.Usage()
os.Exit(1)
}
if len(cmd.Args()) < 2 {
fmt.Fprintf(os.Stderr, "index name and query required for index search\n")
cmd.Usage()
os.Exit(1)
}
cfg := LoadConfig(*cfgpath)
es := cfg.GetClient(*server)
res, err := es.Search().Index(cmd.Arg(0)).StoredField(*fields).Query(
elastic.NewQueryStringQuery(cmd.Arg(1)).DefaultOperator(*defop),
).Do(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
os.Exit(1)
}
for _, r := range res.Hits.Hits {
b, err := json.Marshal(r.Fields)
if err != nil {
fmt.Fprintf(os.Stderr, "marshal error: %s\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "%s: %s\n", r.Id, string(b))
}
fmt.Fprintf(os.Stdout, "Total: %d\n", res.Hits.TotalHits.Value);
case "delete", "del", "d":
cmd := flag.NewFlagSet("index delete", flag.ExitOnError)
cmd.SetOutput(os.Stdout)

4
main.go

@ -21,6 +21,9 @@ func main() {
case "alias", "a":
handleAliasCommand()
case "search", "sea", "s":
handleSearchCommand()
case "--help":
printDefaultUsage()
os.Exit(0)
@ -41,4 +44,5 @@ func printDefaultUsage() {
fmt.Printf("valid commands:\n")
fmt.Printf(" index create, delete, refresh, or list indexes\n")
fmt.Printf(" alias create, delete, replace, or list aliases\n")
fmt.Printf(" search search an index or alias\n")
}

64
search.go

@ -0,0 +1,64 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
elastic "github.com/olivere/elastic/v7"
)
func handleSearchCommand() {
cmd := flag.NewFlagSet("search", flag.ExitOnError)
cmd.SetOutput(os.Stdout)
cmd.Usage = func() {
fmt.Printf("Usage: %s search <index> <search term(s)>\n", os.Args[0])
cmd.PrintDefaults()
}
cfgpath := cmd.String("config", "", "path to configuration")
server := cmd.String("server", "", "server name to use")
fields := cmd.String("fields", "*", "fields to include in search")
size := cmd.Int("size", 10, "maximum number of results returned")
from := cmd.Int("from", 0, "count to start search results from")
defop := cmd.String("defop", "AND", "default search operator")
cmd.Parse(os.Args[2:])
if *defop != "AND" && *defop != "OR" {
fmt.Fprintf(os.Stderr, "default operator must be AND or OR\n")
cmd.Usage()
os.Exit(1)
}
if len(cmd.Args()) < 2 {
fmt.Fprintf(os.Stderr, "index name and query required for search\n")
cmd.Usage()
os.Exit(1)
}
cfg := LoadConfig(*cfgpath)
es := cfg.GetClient(*server)
res, err := es.Search().
Index(cmd.Arg(0)).
StoredField(*fields).
Size(*size).
From(*from).
Query(elastic.NewQueryStringQuery(cmd.Arg(1)).DefaultOperator(*defop)).
Do(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
os.Exit(1)
}
for _, r := range res.Hits.Hits {
b, err := json.Marshal(r.Fields)
if err != nil {
fmt.Fprintf(os.Stderr, "marshal error: %s\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "%s: %s\n", r.Id, string(b))
}
fmt.Fprintf(os.Stdout, "Total: %d\n", res.Hits.TotalHits.Value)
}
Loading…
Cancel
Save