-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
373 lines (343 loc) · 8.48 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/analysis/analyzer/custom"
"github.com/blevesearch/bleve/analysis/char/html"
"github.com/blevesearch/bleve/analysis/lang/fr"
"github.com/blevesearch/bleve/analysis/token/lowercase"
"github.com/blevesearch/bleve/analysis/token/stop"
"github.com/blevesearch/bleve/analysis/tokenizer/exception"
bleveuni "github.com/blevesearch/bleve/analysis/tokenizer/unicode"
"github.com/blevesearch/bleve/analysis/tokenmap"
"github.com/blevesearch/bleve/index/store/boltdb"
"github.com/blevesearch/bleve/index/upsidedown"
"github.com/pmezard/apec/jstruct"
"github.com/pquerna/ffjson/ffjson"
)
func loadOffers(store *Store) ([]*jstruct.JsonOffer, error) {
type offerResult struct {
Id string
Offer *jstruct.JsonOffer
Err error
}
ids, err := store.List()
if err != nil {
return nil, err
}
sort.Strings(ids)
pending := make(chan string, len(ids))
for _, id := range ids {
pending <- id
}
close(pending)
results := make(chan offerResult, len(ids))
running := &sync.WaitGroup{}
jobs := 4
for i := 0; i < jobs; i++ {
running.Add(1)
go func() {
defer running.Done()
for id := range pending {
offer, err := getStoreJsonOffer(store, id)
results <- offerResult{
Id: id,
Offer: offer,
Err: err,
}
}
}()
}
go func() {
running.Wait()
close(results)
}()
offers := []*jstruct.JsonOffer{}
for r := range results {
if r.Err != nil {
fmt.Printf("loading error for %s: %s\n", r.Id, r.Err)
continue
}
if r.Offer == nil {
continue
}
offers = append(offers, r.Offer)
}
return offers, nil
}
type Offer struct {
Account string
Id string `json:"id"`
HTML string `json:"html"`
Title string `json:"title"`
MinSalary int `json:"min_salary"`
MaxSalary int `json:"max_salary"`
Date time.Time `json:"date"`
URL string
Location string `json:"location"`
}
const (
ApecURL = "https://cadres.apec.fr/home/mes-offres/recherche-des-offres-demploi/" +
"liste-des-offres-demploi/detail-de-loffre-demploi.html?numIdOffre="
)
func convertOffer(offer *jstruct.JsonOffer) (*Offer, error) {
r := &Offer{
Account: offer.Account,
Id: offer.Id,
HTML: offer.HTML,
Title: offer.Title,
URL: ApecURL + offer.Id,
Location: offer.Location,
}
if r.Location == "" && len(offer.Locations) > 0 {
r.Location = offer.Locations[0].Name
}
min, max, err := parseSalary(offer.Salary)
if err != nil {
return nil, fmt.Errorf("cannot parse salary %q: %s", offer.Salary, err)
}
d, err := time.Parse("2006-01-02T15:04:05.000+0000", offer.Date)
if err != nil {
return nil, err
}
r.Date = d
r.MinSalary = min
r.MaxSalary = max
return r, nil
}
func getStoreJsonOffer(store *Store, id string) (*jstruct.JsonOffer, error) {
data, err := store.Get(id)
if err != nil {
return nil, err
}
if data == nil {
return nil, nil
}
js := &jstruct.JsonOffer{}
err = ffjson.Unmarshal(data, js)
return js, err
}
func getStoreOffer(store *Store, id string) (*Offer, error) {
js, err := getStoreJsonOffer(store, id)
if err != nil || js == nil {
return nil, err
}
return convertOffer(js)
}
func convertOffers(offers []*jstruct.JsonOffer) ([]*Offer, error) {
result := make([]*Offer, 0, len(offers))
for _, o := range offers {
r, err := convertOffer(o)
if err != nil {
return nil, err
}
result = append(result, r)
}
return result, nil
}
var (
indexExceptions = []string{
"c++",
"c#",
}
stopWords = []interface{}{
"h", // The H in H/F
"f", // The F in H/F
// Generic garbage
"mision",
"post",
"entrepris",
"suivi",
}
)
func NewOfferIndex(dir string) (bleve.Index, error) {
err := os.RemoveAll(dir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
parts := []string{}
for _, exc := range indexExceptions {
parts = append(parts, regexp.QuoteMeta(exc))
}
pattern := strings.Join(parts, "|")
pattern = "(?i)(?:" + pattern + ")"
m := bleve.NewIndexMapping()
apecTokenizer := "apec"
err = m.AddCustomTokenizer(apecTokenizer, map[string]interface{}{
"type": exception.Name,
"exceptions": []string{pattern},
"tokenizer": bleveuni.Name,
})
if err != nil {
return nil, err
}
apecTokens := "apec_tokens"
err = m.AddCustomTokenMap(apecTokens, map[string]interface{}{
"type": tokenmap.Name,
"tokens": stopWords,
})
if err != nil {
return nil, err
}
apecStop := "apec_stop"
err = m.AddCustomTokenFilter(apecStop, map[string]interface{}{
"type": stop.Name,
"stop_token_map": apecTokens,
})
if err != nil {
return nil, err
}
frTokens := []string{
lowercase.Name,
fr.ElisionName,
fr.StopName,
fr.LightStemmerName,
apecStop,
}
fr := map[string]interface{}{
"type": custom.Name,
"tokenizer": apecTokenizer,
"token_filters": frTokens,
}
frHtml := map[string]interface{}{
"type": custom.Name,
"char_filters": []string{
html.Name,
},
"tokenizer": apecTokenizer,
"token_filters": frTokens,
}
err = m.AddCustomAnalyzer("fr", fr)
if err != nil {
return nil, fmt.Errorf("failed to register analyzer fr: %s", err)
}
err = m.AddCustomAnalyzer("fr_html", frHtml)
if err != nil {
return nil, fmt.Errorf("failed to register analyzer fr_html: %s", err)
}
htmlFr := bleve.NewTextFieldMapping()
htmlFr.Store = false
htmlFr.IncludeInAll = false
htmlFr.IncludeTermVectors = false
htmlFr.Analyzer = "fr_html"
textFr := bleve.NewTextFieldMapping()
textFr.Store = false
textFr.IncludeInAll = false
textFr.IncludeTermVectors = false
textFr.Analyzer = "fr"
textAll := bleve.NewTextFieldMapping()
textAll.Store = false
textAll.IncludeInAll = true
textAll.IncludeTermVectors = false
date := bleve.NewDateTimeFieldMapping()
date.Index = false
date.Store = true
date.IncludeInAll = false
date.IncludeTermVectors = false
offer := bleve.NewDocumentStaticMapping()
offer.Dynamic = false
offer.AddFieldMappingsAt("html", htmlFr)
offer.AddFieldMappingsAt("title", textFr)
offer.AddFieldMappingsAt("date", date)
m.AddDocumentMapping("offer", offer)
m.DefaultMapping = offer
index, err := bleve.NewUsing(dir, m, upsidedown.Name, boltdb.Name,
map[string]interface{}{
"nosync": true,
})
if err != nil {
return nil, err
}
return index, nil
}
func OpenOfferIndex(path string) (bleve.Index, error) {
return bleve.OpenUsing(path, map[string]interface{}{
"nosync": false,
})
}
var (
indexCmd = app.Command("index", "index APEC offers")
indexMaxSize = indexCmd.Flag("max-count", "maximum number of items to index").
Short('n').Default("0").Int()
// Work around kingpin messing with boolean flags starting with --no-xxx (#54)
indexIndex = indexCmd.Flag("index", "enable indexing (use --no-index to disable it)").
Default("true").Bool()
indexMinQuota = indexCmd.Flag("min-quota",
"stop geocoding when call quota moves below supplied value").Default("500").Int()
indexDocId = indexCmd.Flag("id", "index only specified document").String()
)
func indexOffers(cfg *Config) error {
store, err := OpenStore(cfg.Store())
if err != nil {
return err
}
defer store.Close()
rawOffers, err := loadOffers(store)
if err != nil {
return err
}
if *indexDocId != "" {
kept := []*jstruct.JsonOffer{}
for _, o := range rawOffers {
if o.Id == *indexDocId {
kept = append(kept, o)
}
}
rawOffers = kept
}
offers, err := convertOffers(rawOffers)
if err != nil {
return err
}
if *indexMaxSize > 0 && len(offers) > *indexMaxSize {
offers = offers[:*indexMaxSize]
}
rejected := 0
geocodingKey := cfg.GeocodingKey()
if geocodingKey != "" {
geocoder, err := NewGeocoder(geocodingKey, cfg.Geocoder())
if err != nil {
return err
}
defer geocoder.Close()
rejected, err = geocodeOffers(store, geocoder, offers, *indexMinQuota)
if err != nil {
return err
}
fmt.Printf("%d rejected geocoding\n", rejected)
}
if *indexIndex {
index, err := NewOfferIndex(cfg.Index())
if err != nil {
return err
}
start := time.Now()
indexed := 0
for i, offer := range offers {
if (i+1)%500 == 0 {
now := time.Now()
elapsed := float64(now.Sub(start)) / float64(time.Second)
fmt.Printf("%d indexed, %.1f/s\n", i+1, float64(i+1)/elapsed)
}
err = index.Index(offer.Id, offer)
if err != nil {
return err
}
indexed += 1
}
err = index.Close()
if err != nil {
return err
}
end := time.Now()
fmt.Printf("%d/%d documents indexed in %.2fs\n", indexed, len(offers),
float64(end.Sub(start))/float64(time.Second))
}
return nil
}