forked from go-mysql-org/go-mysql-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.go
52 lines (41 loc) · 1.2 KB
/
rule.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
package river
import (
"github.com/siddontang/go-mysql/schema"
)
// If you want to sync MySQL data into elasticsearch, you must set a rule to let use know how to do it.
// The mapping rule may thi: schema + table <-> index + document type.
// schema and table is for MySQL, index and document type is for Elasticsearch.
type Rule struct {
Schema string `toml:"schema"`
Table string `toml:"table"`
Index string `toml:"index"`
Type string `toml:"type"`
Parent string `toml:"parent"`
// Default, a MySQL table field name is mapped to Elasticsearch field name.
// Sometimes, you want to use different name, e.g, the MySQL file name is title,
// but in Elasticsearch, you want to name it my_title.
FieldMapping map[string]string `toml:"field"`
// MySQL table information
TableInfo *schema.Table
}
func newDefaultRule(schema string, table string) *Rule {
r := new(Rule)
r.Schema = schema
r.Table = table
r.Index = table
r.Type = table
r.FieldMapping = make(map[string]string)
return r
}
func (r *Rule) prepare() error {
if r.FieldMapping == nil {
r.FieldMapping = make(map[string]string)
}
if len(r.Index) == 0 {
r.Index = r.Table
}
if len(r.Type) == 0 {
r.Type = r.Index
}
return nil
}