/
repository.go
170 lines (157 loc) · 3.81 KB
/
repository.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
package catalog
import (
"context"
"encoding/json"
"errors"
"log"
elastic "gopkg.in/olivere/elastic.v5"
)
var (
ErrNotFound = errors.New("Entity not found")
)
type Repository interface {
Close()
PutProduct(ctx context.Context, p Product) error
GetProductByID(ctx context.Context, id string) (*Product, error)
ListProducts(ctx context.Context, skip uint64, take uint64) ([]Product, error)
ListProductsWithIDs(ctx context.Context, ids []string) ([]Product, error)
SearchProducts(ctx context.Context, query string, skip uint64, take uint64) ([]Product, error)
}
type elasticRepository struct {
client *elastic.Client
}
type productDocument struct {
Name string `json:"name"`
Description string `json:"description"`
Price float64 `json:"price"`
}
func NewElasticRepository(url string) (Repository, error) {
client, err := elastic.NewClient(
elastic.SetURL(url),
elastic.SetSniff(false),
)
if err != nil {
return nil, err
}
return &elasticRepository{client}, nil
}
func (r *elasticRepository) Close() {
}
func (r *elasticRepository) PutProduct(ctx context.Context, p Product) error {
_, err := r.client.Index().
Index("catalog").
Type("product").
Id(p.ID).
BodyJson(productDocument{
Name: p.Name,
Description: p.Description,
Price: p.Price,
}).
Do(ctx)
return err
}
func (r *elasticRepository) GetProductByID(ctx context.Context, id string) (*Product, error) {
res, err := r.client.Get().
Index("catalog").
Type("product").
Id(id).
Do(ctx)
if err != nil {
return nil, err
}
if !res.Found {
return nil, ErrNotFound
}
p := productDocument{}
if err = json.Unmarshal(*res.Source, &p); err != nil {
return nil, err
}
return &Product{
ID: id,
Name: p.Name,
Description: p.Description,
Price: p.Price,
}, err
}
func (r *elasticRepository) ListProducts(ctx context.Context, skip, take uint64) ([]Product, error) {
res, err := r.client.Search().
Index("catalog").
Type("product").
Query(elastic.NewMatchAllQuery()).
From(int(skip)).Size(int(take)).
Do(ctx)
if err != nil {
log.Println(err)
return nil, err
}
products := []Product{}
for _, hit := range res.Hits.Hits {
p := productDocument{}
if err = json.Unmarshal(*hit.Source, &p); err == nil {
products = append(products, Product{
ID: hit.Id,
Name: p.Name,
Description: p.Description,
Price: p.Price,
})
}
}
return products, err
}
func (r *elasticRepository) ListProductsWithIDs(ctx context.Context, ids []string) ([]Product, error) {
items := []*elastic.MultiGetItem{}
for _, id := range ids {
items = append(
items,
elastic.NewMultiGetItem().
Index("catalog").
Type("product").
Id(id),
)
}
res, err := r.client.MultiGet().
Add(items...).
Do(ctx)
if err != nil {
log.Println(err)
return nil, err
}
products := []Product{}
for _, doc := range res.Docs {
p := productDocument{}
if err = json.Unmarshal(*doc.Source, &p); err == nil {
products = append(products, Product{
ID: doc.Id,
Name: p.Name,
Description: p.Description,
Price: p.Price,
})
}
}
return products, nil
}
func (r *elasticRepository) SearchProducts(ctx context.Context, query string, skip, take uint64) ([]Product, error) {
res, err := r.client.Search().
Index("catalog").
Type("product").
Query(elastic.NewMultiMatchQuery(query, "name", "description")).
From(int(skip)).Size(int(take)).
Do(ctx)
if err != nil {
log.Println(err)
return nil, err
}
products := []Product{}
for _, hit := range res.Hits.Hits {
p := productDocument{}
if err = json.Unmarshal(*hit.Source, &p); err == nil {
products = append(products, Product{
ID: hit.Id,
Name: p.Name,
Description: p.Description,
Price: p.Price,
})
}
}
return products, err
}