-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
125 lines (110 loc) · 2.97 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
package es
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v5"
"github.com/Bnei-Baruch/archive-backend/bindata"
"github.com/Bnei-Baruch/archive-backend/consts"
)
type Scope struct {
ContentUnitUID string
FileUID string
CollectionUID string
TagUID string
SourceUID string
PersonUID string
PublisherUID string
}
type Index interface {
ReindexAll() error
Add(scope Scope) error
Update(scope Scope) error
Delete(scope Scope) error
CreateIndex() error
DeleteIndex() error
RefreshIndex() error
}
type BaseIndex struct {
namespace string
baseName string
db *sql.DB
esc *elastic.Client
}
func IndexName(namespace string, name string, lang string) string {
return fmt.Sprintf("%s_%s_%s", namespace, name, lang)
}
func (index *BaseIndex) indexName(lang string) string {
if index.namespace == "" || index.baseName == "" {
panic("Index namespace and baseName should be set.")
}
return IndexName(index.namespace, index.baseName, lang)
}
func (index *BaseIndex) CreateIndex() error {
for _, lang := range consts.ALL_KNOWN_LANGS {
name := index.indexName(lang)
definition := fmt.Sprintf("data/es/mappings/%s/%s-%s.json", index.baseName, index.baseName, lang)
// Read mappings and create index
mappings, err := bindata.Asset(definition)
if err != nil {
return errors.Wrapf(err, "Failed loading mapping %s", definition)
}
var bodyJson map[string]interface{}
if err = json.Unmarshal(mappings, &bodyJson); err != nil {
return errors.Wrap(err, "json.Unmarshal")
}
// Delete index if it's already exists.
if err = index.deleteIndexByLang(lang); err != nil {
return err
}
// Create index.
res, err := index.esc.CreateIndex(name).BodyJson(bodyJson).Do(context.TODO())
if err != nil {
return errors.Wrap(err, "Create index")
}
if !res.Acknowledged {
return errors.Errorf("Index creation wasn't acknowledged: %s", name)
}
}
return nil
}
func (index *BaseIndex) DeleteIndex() error {
for _, lang := range consts.ALL_KNOWN_LANGS {
if err := index.deleteIndexByLang(lang); err != nil {
return err
}
}
return nil
}
func (index *BaseIndex) deleteIndexByLang(lang string) error {
i18nName := index.indexName(lang)
exists, err := index.esc.IndexExists(i18nName).Do(context.TODO())
if err != nil {
return err
}
if exists {
res, err := index.esc.DeleteIndex(i18nName).Do(context.TODO())
if err != nil {
return errors.Wrap(err, "Delete index")
}
if !res.Acknowledged {
return errors.Errorf("Index deletion wasn't acknowledged: %s", i18nName)
}
}
return nil
}
func (index *BaseIndex) RefreshIndex() error {
for _, lang := range consts.ALL_KNOWN_LANGS {
if err := index.RefreshIndexByLang(lang); err != nil {
return err
}
}
return nil
}
func (index *BaseIndex) RefreshIndexByLang(lang string) error {
_, err := index.esc.Refresh(index.indexName(lang)).Do(context.TODO())
// fmt.Printf("\n\n\nShards: %+v \n\n\n", shards)
return err
}