Skip to content

Commit

Permalink
es and go
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanmca committed Jan 5, 2022
1 parent 07f4e34 commit 1b89482
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 15 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module sampple.com

go 1.17
110 changes: 110 additions & 0 deletions src/main/go/es_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)

type configuration struct {
Query string
Server_fqdn []string
Request_Timeout_in_seconds int64
}

type esQuery struct {
queryUrl string
responseFilename string
requestTimeout int64
}

func getEsQueryList(conf configuration) []esQuery {

esRequests := make([]esQuery, len(conf.Server_fqdn))

currentTime := time.Now()
for i := 0; i < len(conf.Server_fqdn); i++ {

filename := strings.Split(conf.Server_fqdn[i], ".")[0] + "_" + currentTime.Format("20060102150405") + ".json"
esRequests[i] = esQuery{
queryUrl: "https://" + conf.Server_fqdn[i] + "/" + conf.Query,
requestTimeout: conf.Request_Timeout_in_seconds,
responseFilename: filename,
}
}
return esRequests
}

func getConfiguration() configuration {
file, err_file_open := os.Open("es_query_conf.json")

if err_file_open != nil {
fmt.Println("error:", err_file_open)
}
defer file.Close()

decoder := json.NewDecoder(file)
conf := configuration{}

err := decoder.Decode(&conf)
if err != nil {
fmt.Println("error:", err)
}
return conf
}

func getHttpResponse(esRequestInfo esQuery, respStatus chan<- string) {

fmt.Println("HTTP GET:", esRequestInfo.queryUrl)
resp, err := http.Get(esRequestInfo.queryUrl)
if err != nil {
//panic(err)
respStatus <- "Error connecting to host"
return
}
defer resp.Body.Close()

// fmt.Println("Response status:", resp.Status)

out, err := os.Create(esRequestInfo.responseFilename)
if err != nil {
// panic?
fmt.Println("error while writing file: ", esRequestInfo.responseFilename)
}
defer out.Close()
io.Copy(out, resp.Body)
respStatus <- resp.Status
}

func getResponseData(esRequestInfo esQuery, respStatus <-chan string, completionStatus chan<- string) {
select {
case result := <-respStatus:
fmt.Println(result)
break
case <-time.After(time.Duration(esRequestInfo.requestTimeout) * time.Second):
fmt.Println("timedout: ", esRequestInfo)
}
completionStatus <- "completed"
}

func main() {
conf := getConfiguration()
esRequests := getEsQueryList(conf)

responseStatusChannel := make(chan string, len(esRequests))
waitForCompletionChannel := make(chan string, len(esRequests))

for _, esRequest := range esRequests {
go getHttpResponse(esRequest, responseStatusChannel)
go getResponseData(esRequest, responseStatusChannel, waitForCompletionChannel)
}

for i := 0; i < len(esRequests); i++ {
<-waitForCompletionChannel
}
fmt.Println("completed")
}
9 changes: 9 additions & 0 deletions src/main/go/es_query_conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"query": "values",
"server_fqdn": [
"gobyexample.com",
"gobyexample.com",
"gobyexample.com"
],
"request_timeout_in_seconds": 60
}
5 changes: 5 additions & 0 deletions src/main/go/test/Adder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func Add(x, y int) int {
return x + y
}
16 changes: 16 additions & 0 deletions src/main/go/test/Adder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "testing"

func TestAdder(t *testing.T) {
actual := Add(3, 2)
expected := 5
if actual != expected {
t.Errorf("got %q and want %q", actual, expected)
}
actual = Add(3, 5)
expected = 9
if actual != expected {
t.Errorf("got %d and want %d", actual, expected)
}
}
32 changes: 19 additions & 13 deletions src/main/md/Tools/Search/most_useful_es_query.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Most often used adming URL
## Most often used admin URL
* http://localhost:9200/_stats
*
* /_cluster/health
* /index/_count
* /_cat/indices

## How to find elasticsearch node from kibana

* https://kibana_host/_nodes/stats
* curl -XGET https://one_of_the_host/index_name/_search_
# Most often used URL
* *ElasticSearch Get can accept body*

## Most often used URI Search
* ```ElasticSearch Get can accept body```*
* /_search
* /_cluster/health
* /index/_count
* /_cat/indices
* /gb/_mapping/tweet - mapping for type tweet that is under index -gb
* GET /gb/tweet/_validate/query query
* GET /gb/tweet/_validate/query?explain
Expand All @@ -21,17 +21,23 @@
* GET /old_index/_search?search_type=scan&scroll=1m
* DELETE /index_one,index_two or DELETE /index_* or DELETE /_all
* PUT /my_index_v1/_alias/my_index and/or GET /*/_alias/my_index

## URI Search window of time

* http://pi4.pushshift.io/rc/_search/?pretty&sort=created_utc:desc&size=250&q=created_utc:[now-1h%20TO%20now] 6

## Most often used Query Search
* To count the number of documents in the cluster
* ```{"query": { "match_all": {} }}```
* ```{"query": { "match_all": {} }}```
* To count the number of documents in the cluster
* ```{ "query" : { "match" : { "last_name" : "Smith" } }}```
* ```{ "query" : { "match" : { "last_name" : "Smith" } }}```
* To find all employees with a last name of Smith, but we want only employees who are older than 30
* ```{ "query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } 1 } }, "query" : { "match" : { "last_name" : "smith" 2 } } } }}```
* ```{ "query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } 1 } }, "query" : { "match" : { "last_name" : "smith" 2 } } } }}```
* To Full-Text Search - search for all employees who enjoy rock climbing - relevance
* ```{ "query" : { "match" : { "about" : "rock climbing" }}}``
* ```{ "query" : { "match" : { "about" : "rock climbing" }}}``
* To Full-Text Search - search for all employees who enjoy rock climbing - mandatory
* ```{ "query" : {"match_phrase" : {"about" : "rock climbing"}}}```
* Highlight - produces html <em> around matched words
* ```{ "query" : {"match_phrase" : {"about" : "rock climbing"}}}```
* Highlight - produces html <em> around matched words
```{ "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields" : { "about" : {} } } }```
* Sample aggregation query
```{ "aggs": { "all_interests": { "terms": { "field": "interests" } } } }```
Expand Down
7 changes: 5 additions & 2 deletions src/main/md/golang/27_golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ func (c *SafeCounter) Value(key string) int {

## Golang errors

* c, python, java := true, false, "no!"; python := "false!"
* c, python, java := true, false, "no!"; python := "false!"
1. ./prog.go:9:9: no new variables on left side of :=
1. ./prog.go:9:9: cannot use "false!" (type untyped string) as type bool in assignment
1. ./prog.go:9:9: cannot use "false!" (type untyped string) as type bool in assignment
* gore> lstArray = lstArray[1:]
cannot use lstArray[1:] (type []int) as type [3]int in assignment
* lstArray := []int{1, 2, 3} -- slices - would make above work

0 comments on commit 1b89482

Please sign in to comment.