-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
89 lines (79 loc) · 1.76 KB
/
search.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
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"sort"
"strings"
"github.com/blevesearch/bleve"
"github.com/pmezard/apec/jstruct"
)
type offersByDate []*jstruct.JsonOffer
func (s offersByDate) Len() int {
return len(s)
}
func (s offersByDate) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s offersByDate) Less(i, j int) bool {
return s[i].Date < s[j].Date
}
func formatDate(s string) string {
parts := strings.SplitN(s, "T", 2)
return parts[0]
}
func printOffers(store *Store, ids []string) error {
// Load offer documents
offers := []*jstruct.JsonOffer{}
for _, id := range ids {
offer, err := getStoreJsonOffer(store, id)
if err != nil || offer == nil {
continue
}
offers = append(offers, offer)
}
// Sort by ascending publication date
sorted := offersByDate(offers)
sort.Sort(sorted)
for _, offer := range sorted {
fmt.Printf("%s %s %s %s (%s)\n", offer.Id, offer.Title, offer.Salary,
offer.Account, formatDate(offer.Date))
fmt.Printf(" https://cadres.apec.fr/offres-emploi-cadres/offre.html?numIdOffre=%s\n",
offer.Id)
}
return nil
}
var (
searchCmd = app.Command("search", "search APEC index")
searchQuery = searchCmd.Arg("query", "search query").Required().String()
)
func search(cfg *Config) error {
store, err := OpenStore(cfg.Store())
if err != nil {
return err
}
index, err := bleve.Open(cfg.Index())
if err != nil {
return err
}
defer index.Close()
q, err := makeSearchQuery(*searchQuery, nil)
if err != nil {
return err
}
rq := bleve.NewSearchRequest(q)
rq.Size = 100
ids := []string{}
for {
res, err := index.Search(rq)
if err != nil {
return err
}
for _, doc := range res.Hits {
ids = append(ids, doc.ID)
}
if len(res.Hits) < rq.Size {
break
}
rq.From += rq.Size
}
return printOffers(store, ids)
}