-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
171 lines (138 loc) · 4.47 KB
/
db.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
package models
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path"
"strconv"
"github.com/tomekwlod/okpii/models"
modelsMysql "github.com/tomekwlod/okpii/models/mysql"
"github.com/tomekwlod/utils"
elastic "gopkg.in/olivere/elastic.v6"
)
const mappingfn = "mapping.json"
type Repository interface {
ExecuteQuery(q *elastic.BoolQuery) ([]map[string]interface{}, error)
Count(did int) int
FindOne(id, did int, ln string) (models.Expert, error)
MarkAsDeleted(id string) (err error)
UpdatePartially(id string, exp models.Expert) (err error)
// searches
BaseQuery(did int, country string, exclIDs []string) (*elastic.BoolQuery, error)
SimpleSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
ForeignSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
ShortSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
NoMiddleNameSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
OneMiddleNameSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
OneMiddleNameSearch2(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
MadnessSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
ThreeInitialsSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
TestSearch(fn, mn, ln, country, city string, did int, exclIDs []string) []map[string]interface{}
// index
RemoveData(did int) (int64, error)
IndexExperts(experts []*modelsMysql.Experts, batchInsert int) error
}
type DB struct {
*elastic.Client
}
type esConfig struct {
Addr string
Port int
UseSniffer bool
}
func ESClient() (*DB, error) {
host := os.Getenv("ES_HOST")
if host == "" {
host = "localhost"
}
port := os.Getenv("ES_PORT")
if port == "" {
port = "9201"
}
iport, _ := strconv.Atoi(port)
ec := esConfig{host, iport, false}
// Create ES client here; If no connection - nothing to do here
db, err := newESClient(ec)
if err != nil {
return nil, err
}
// Create mapping
err = createIndex(db, "experts")
if err != nil {
return nil, err
}
fmt.Printf("\nConnection to ElasticServer established %s:%s\n", host, port)
return &DB{db}, nil
}
func newESClient(ec esConfig) (client *elastic.Client, err error) {
// not sure
errorlog := log.New(os.Stdout, "ESAPP ", log.LstdFlags)
// ip plus port plus protocol
addr := "http://" + ec.Addr + ":" + strconv.Itoa(ec.Port)
var configs []elastic.ClientOptionFunc
configs = append(configs, elastic.SetURL(addr), elastic.SetErrorLog(errorlog))
configs = append(configs, elastic.SetSniff(ec.UseSniffer)) // this is very important when you use proxy above your ES instance; it may be though wanted for many ES nodes
// Obtain a client. You can also provide your own HTTP client here.
client, err = elastic.NewClient(configs...)
if err != nil {
return
}
// Trace request and response details like this
//client.SetTracer(log.New(os.Stdout, "", 0))
// Ping the Elasticsearch server to get info, code, and error if any
_, _, err = client.Ping(addr).Do(context.Background())
if err != nil {
return
}
// Getting the ES version number is quite common, so there's a shortcut
_, err = client.ElasticsearchVersion(addr)
if err != nil {
return
}
return
}
func createIndex(client *elastic.Client, index string) (err error) {
// Use the IndexExists service to check if a specified index exists.
exists, err := client.IndexExists(index).Do(context.Background())
if err != nil {
return
}
if exists {
return
}
fmt.Println("No mapping found. Creating one")
if os.Getenv("STATICPATH") == "" {
// in prod mode (with the docker) the STATICPATH won't be empty
// in dev mode set the default static path
os.Setenv("STATICPATH", "../../data/static")
}
filename := path.Join(os.Getenv("STATICPATH"), mappingfn)
// Create a new index
file, err := utils.ReadWholeFile(filename)
if err != nil {
return
}
ic, err := client.CreateIndex(index).Body(string(file)).Do(context.Background())
if err != nil {
return
}
if !ic.Acknowledged {
err = errors.New("Mapping couldn't be acknowledged")
return
}
return
}
func PrintESQuery(nss *elastic.SearchSource) {
sjson, err := nss.Source()
if err != nil {
panic(err)
}
data, err := json.MarshalIndent(sjson, "", " ")
if err != nil {
panic(err)
}
log.Printf("%s\n", string(data))
}