Skip to content

Commit

Permalink
[gdgtoledo#7] split elasticsearch into diferent files
Browse files Browse the repository at this point in the history
  • Loading branch information
javierlopezdeancos committed Oct 29, 2019
1 parent 645ed68 commit 97c783d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
36 changes: 36 additions & 0 deletions elastic/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package elastic

import (
es "github.com/elastic/go-elasticsearch/v8"
log "github.com/sirupsen/logrus"
)

var esInstance *es.Client

// GetClient returns a client connected to the running elasticseach cluster
func GetClient() (*es.Client, error) {
if esInstance != nil {
return esInstance, nil
}

cfg := es.Config{
Addresses: []string{
"http://elasticsearch:9200",
"http://elasticsearch2:9200",
"http://elasticsearch3:9200",
},
}
esClient, err := es.NewClient(cfg)
if err != nil {
log.WithFields(log.Fields{
"config": cfg,
"error": err,
}).Error("Could not obtain an Elasticsearch client")

return nil, err
}

esInstance = esClient

return esInstance, nil
}
47 changes: 8 additions & 39 deletions dao/elasticsearch.go → elastic/dao/search.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,21 @@
package dao
package elastic

import (
"bytes"
"encoding/json"
"fmt"

es "github.com/elastic/go-elasticsearch/v8"
"github.com/gdgtoledo/linneo/elastic"
"github.com/gdgtoledo/linneo/plants"
log "github.com/sirupsen/logrus"
)

var esInstance *es.Client

// SearchResult wraps a search result
type SearchResult map[string]interface{}

// getElasticsearchClient returns a client connected to the running elasticseach cluster
func getElasticsearchClient() (*es.Client, error) {
if esInstance != nil {
return esInstance, nil
}

cfg := es.Config{
Addresses: []string{
"http://elasticsearch:9200",
"http://elasticsearch2:9200",
"http://elasticsearch3:9200",
},
}
esClient, err := es.NewClient(cfg)
if err != nil {
log.WithFields(log.Fields{
"config": cfg,
"error": err,
}).Error("Could not obtain an Elasticsearch client")

return nil, err
}

esInstance = esClient

return esInstance, nil
}

// Search executes a query in the proper index
func Search(indexName string, query map[string]interface{}) (SearchResult, error) {
result := SearchResult{}
func Search(query plants.SearchQueryByIndexName) (plants.SearchQueryByIndexNameResult, error) {
result := plants.SearchQueryByIndexNameResult{}

esClient, err := elastic.GetClient()

esClient, err := getElasticsearchClient()
if err != nil {
return result, err
}
Expand All @@ -65,7 +34,7 @@ func Search(indexName string, query map[string]interface{}) (SearchResult, error
}).Debug("Elasticsearch query")

res, err := esClient.Search(
esClient.Search.WithIndex(indexName),
esClient.Search.WithIndex(query.IndexName),
esClient.Search.WithBody(&buf),
esClient.Search.WithTrackTotalHits(true),
esClient.Search.WithPretty(),
Expand Down

0 comments on commit 97c783d

Please sign in to comment.