-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (78 loc) · 2.27 KB
/
main.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
package main
import (
"fmt"
"os"
"time"
"github.com/anrid/attribute-filters/pkg/attribute"
"github.com/anrid/attribute-filters/pkg/elastic"
"github.com/spf13/pflag"
)
func main() {
attributesDir := pflag.StringP("attributes-data", "a", "", "dir with gzipped CSV files containing exported tables from the Item Attributes Postgres database (e.g. attributes.csv.gz)")
categoriesFile := pflag.String("categories-file", "", "categories file in JSON format")
keyword := pflag.StringP("keyword", "k", "", "keyword/phrase to search for")
categoryID := pflag.IntP("cid", "c", 0, "limit to category ID")
max := pflag.IntP("max", "m", 3, "return max X items")
pflag.Parse()
if *keyword == "" && *categoryID == 0 {
pflag.PrintDefaults()
os.Exit(-1)
}
cond := new(elastic.Conditions)
if *keyword != "" {
cond.Keyword = *keyword
}
if *categoryID > 0 {
cond.CategoryIDs = append(cond.CategoryIDs, *categoryID)
}
res, err := elastic.Query(elastic.QueryArgs{
C: cond,
Size: *max,
CategoryFacets: true,
AttributeFacets: true,
})
if err != nil {
panic(err)
}
hasResults := (len(res.Items) > 0 || len(res.CategoryFacets) > 0 || len(res.AttributeFacets) > 0)
lookupFilesAvalable := *attributesDir != "" && *categoriesFile != ""
if hasResults && lookupFilesAvalable {
db := attribute.NewDB()
err = db.LoadCategoriesJSON(*categoriesFile)
if err != nil {
panic(err)
}
err = db.ImportPostgresDatabase(attribute.ImportPostgresDatabaseArgs{Dir: *attributesDir})
if err != nil {
panic(err)
}
fmt.Printf("\nQuery results:\n\n")
tpl := "%03d. Item %s (score: %2.02f)\n" +
" - name : %s\n" +
" - category : %s\n" +
" - created : %s\n" +
" - status : %d\n"
for c, i := range res.Items {
created := time.UnixMilli(i.Created)
fmt.Printf(tpl,
c+1,
i.ID,
res.Scores[c],
i.Name,
db.FullCategoryName(i.CategoryID),
created.Format("2006-01-02 15:04:05"),
i.Status,
)
if len(i.Attributes) > 0 {
fmt.Printf(" - attributes:\n")
for _, pair := range i.Attributes {
fmt.Printf(" - [%-13s] %s\n", pair, db.AttributeOptionPairToString(pair))
}
}
fmt.Println("")
}
fmt.Println("")
} else {
fmt.Printf("Query result:\n%s\n", elastic.ToPrettyJSON(res))
}
}