-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elsearm.go
106 lines (96 loc) · 3.11 KB
/
elsearm.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
package elsearm
import (
"io"
"strings"
)
// IndexName returns an index name of the model.
// By default, it returns converted to snake case the struct name of model.
func IndexName(model interface{}) string {
indexName := (func() string {
searchable, ok := model.(CustomIndexNameModel)
if ok {
return searchable.GetIndexName()
}
return DefaultIndexName(model)
})()
return IndexNameWithAffix(indexName)
}
// SearchIndexName returns an index name of the model when searching.
// By default, it returns the same index name as the return value of IndexName.
func SearchIndexName(model interface{}) []string {
searchable, ok := model.(CustomSearchIndexNameModel)
if ok {
names := searchable.GetSearchIndexName()
for i, name := range names {
names[i] = IndexNameWithAffix(name)
}
return names
}
return []string{IndexName(model)}
}
// IndexNameWithAffix returns an index name appending prefix and suffix.
func IndexNameWithAffix(indexName string) string {
// Dynamic index name
// ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html
if !strings.Contains(indexName, "<") {
return globalConfig.IndexNamePrefix + indexName + globalConfig.IndexNameSuffix
}
replacer := strings.NewReplacer(
"<", "<"+globalConfig.IndexNamePrefix,
">", globalConfig.IndexNameSuffix+">",
)
return replacer.Replace(indexName)
}
// IndexNamesWithAffix returns index names appending prefix and suffix.
func IndexNamesWithAffix(indexNames []string) []string {
indexNamesWithAffix := make([]string, len(indexNames))
for i, indexName := range indexNames {
indexNamesWithAffix[i] = IndexNameWithAffix(indexName)
}
return indexNamesWithAffix
}
// DocumentID returns a document id of the model.
// By default, it returns value of id or ID field in the model. Otherwise, it returns an empty string.
func DocumentID(model interface{}) (string, error) {
searchable, ok := model.(CustomDocumentIdModel)
if ok {
return searchable.GetDocumentID()
}
return DefaultDocumentID(model), nil
}
// DocumentBody transforms the model into a data structure that is stored in Elasticsearch.
// By default, it execute json.Marshal.
func DocumentBody(model interface{}) (io.Reader, error) {
searchable, ok := model.(CustomDocumentBodyModel)
if ok {
return searchable.GetDocumentBody()
}
return DefaultDocumentBody(model)
}
// MustDocumentBody is similar to DocumentBody.
// It will panic if the DocumentBody returns an error.
func MustDocumentBody(model interface{}) io.Reader {
reader, err := DocumentBody(model)
if err != nil {
panic(err)
}
return reader
}
// ParseDocument parses and applies the value to the model.
// By default, it execute json.Unmarshal.
func ParseDocument(model interface{}, reader io.Reader) error {
searchable, ok := model.(CustomDocumentBodyModel)
if ok {
return searchable.ParseDocument(reader)
}
return DefaultParseDocument(model, reader)
}
// SetDocumentID set the DocumentID to the model.
// By default, no executed.
func SetDocumentID(model interface{}, id string) error {
searchable, ok := model.(AutomaticIDModel)
if ok {
return searchable.SetDocumentID(id)
}
return nil
}