Created
January 12, 2017 15:27
-
-
Save anonymous/ef0a526784ae5c4e2306d1033ec90467 to your computer and use it in GitHub Desktop.
Elastic Search client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"strconv" | |
) | |
// ESClient - structure for holding connection info to elasticsearch | |
type ESClient struct { | |
server string | |
port int | |
} | |
func main() { | |
client := GetNewESClient(server, port) | |
response, err := SearchTemplate(client, templateName) | |
if err != nil { | |
fmt.Println(err) | |
} | |
//handle response | |
} | |
// GetNewESClient - Creates a new client | |
func GetNewESClient(server string, port int) *ESClient { | |
return &ESClient{ | |
server, | |
port} | |
} | |
// SearchTemplate - Post to search template api, returns hits | |
func SearchTemplate(esc *ESClient, templateName string) (*SearchTemplateResponse, error) { | |
url := esc.server + ":" + strconv.Itoa(esc.port) + "/_search/template" | |
postData := []byte(`{"id": "` + templateName + `"}`) | |
req, err := http.NewRequest("POST", url, bytes.NewBuffer(postData)) | |
req.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
defer resp.Body.Close() | |
target := SearchTemplateResponse{} | |
json.NewDecoder(resp.Body).Decode(&target) | |
return &target, nil | |
} | |
//GetHostPath - Get the host and path formatted | |
func GetHostPath(source Source) (host, path string) { | |
host = source.SourceHost | |
if len(host) == 0 { | |
host = source.Host | |
} | |
path = source.Path | |
if len(source.Method) > 0 { | |
path = fmt.Sprintf("Method %s() called in %s, line %d", source.Method, source.File, source.Line) | |
} | |
return | |
} | |
// Source - Struct for json return | |
type Source struct { | |
Message string `json:"message"` | |
Version string `json:"@version"` | |
Timestamp string `json:"@timestamp"` | |
Path string `json:"path"` | |
Host string `json:"host"` | |
SourceHost string `json:"sourcehost"` | |
File string `json:"file"` | |
Line int `json:"line"` | |
Method string `json:"method"` | |
Etype string `json:"type"` | |
Datetime string `json:"datetime"` | |
ErrorType string `json:"error_type"` | |
TimestampSubmitted string `json:"timestamp_submitted"` | |
Developer string `json:"developer"` | |
} | |
// Hit - Struct for json return | |
type Hit struct { | |
Index string `json:"_index"` | |
Etype string `json:"_type"` | |
ID string `json:"_id"` | |
Score float32 `json:"_score"` | |
Source Source `json:"_source"` | |
} | |
// Hits - Struct for json return | |
type Hits struct { | |
Total int `json:"total"` | |
MaxScore float32 `json:"max_score"` | |
Hits []Hit `json:"hits"` | |
} | |
// Shards - Struct for json return | |
type Shards struct { | |
Total int `json:"total"` | |
Successful int `json:"successful"` | |
Failed int `json:"failed"` | |
} | |
// SearchTemplateResponse - structure to hold json data of template hits | |
type SearchTemplateResponse struct { | |
Took int `json:"took"` | |
TimedOut bool `json:"timed_out"` | |
Shards Shards `json:"_shards"` | |
Hits Hits `json:"hits"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment