forked from stackmutt/whoisd
/
elasticsearch.go
169 lines (138 loc) · 3.79 KB
/
elasticsearch.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
// Copyright 2017 Openprovider Authors. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.
package storage
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
)
// ElasticsearchRecord - standard record (struct) for elasticsearch storage package
type ElasticsearchRecord struct {
Host string
Port int
Index string
Type string
}
// Search data in the storage
func (elastic *ElasticsearchRecord) Search(name string, query string) (map[string][]string, error) {
result, err := elastic.searchRaw(elastic.Type, name, query)
if err != nil {
return nil, err
}
if len(result) > 0 {
return result[0], nil
}
data := make(map[string][]string)
return data, nil
}
// SearchRelated - search data in the storage from related type or table
func (elastic *ElasticsearchRecord) SearchRelated(
typeTable string, name string, query string) (map[string][]string, error) {
result, err := elastic.searchRaw(typeTable, name, query)
if err != nil {
return nil, err
}
if len(result) > 0 {
return result[0], nil
}
data := make(map[string][]string)
return data, nil
}
// SearchMultiple - search multiple records of data in the storage
func (elastic *ElasticsearchRecord) SearchMultiple(
typeTable string, name string, query string) (map[string][]string, error) {
result, err := elastic.searchRaw(typeTable, name, query)
if err != nil {
return nil, err
}
data := make(map[string][]string)
if len(result) > 0 {
for _, item := range result {
for key, value := range item {
data[key] = append(data[key], value...)
}
}
return data, nil
}
return data, nil
}
// search raw data in the storage
func (elastic *ElasticsearchRecord) searchRaw(
typeTable string, name string, query string) ([]map[string][]string, error) {
if len(typeTable) == 0 || len(name) == 0 || len(query) == 0 {
return nil, errors.New("Incomplete request, request parameters could not be empty")
}
var items []map[string][]string
url := "http://" + elastic.Host + ":" + strconv.Itoa(elastic.Port) +
"/" + elastic.Index + "/" + typeTable
request := url + "/_search?q=" + name + ":" + query + ""
response, err := http.Get(request)
if err != nil {
return items, err
}
jsondata, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return items, err
}
type DataRecord struct {
Took int
Hits struct {
Total int
Hits []map[string]interface{}
}
}
result := new(DataRecord)
if err := json.Unmarshal(jsondata, result); err != nil {
return items, err
}
if result.Hits.Total > 0 {
for _, record := range result.Hits.Hits {
element := transformData(record)
items = append(items, element)
}
}
return items, nil
}
// Transformation data to requested format
func transformData(record map[string]interface{}) map[string][]string {
element := make(map[string][]string)
// Check data for simple and array fields
// Convert all data to []string
for index, value := range record["_source"].(map[string]interface{}) {
// Check for array field
if arrayFields, ok := value.(map[string]interface{}); ok {
// Array field must be converted to <name.key> format
for key, val := range arrayFields {
var item []string
if array, ok := val.([]interface{}); ok {
for _, v := range array {
item = append(item, toString(v)...)
}
} else {
item = toString(val)
}
element[index+"."+key] = item
}
} else {
// Simple field used as <name> and converted to []string
element[index] = toString(value)
}
}
return element
}
func toString(value interface{}) []string {
var item []string
if value != nil {
if v, ok := value.(string); ok {
item = []string{v}
}
if v, ok := value.(float64); ok {
item = []string{strconv.FormatFloat(v, 'f', -1, 64)}
}
}
return item
}