From 97c783d59990a09c6cf6a86e87ff6da56f750194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20L=C3=B3pez=20de=20Ancos?= Date: Tue, 29 Oct 2019 17:43:41 +0100 Subject: [PATCH] [#7] split elasticsearch into diferent files --- elastic/client.go | 36 ++++++++++++++ dao/elasticsearch.go => elastic/dao/search.go | 47 ++++--------------- 2 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 elastic/client.go rename dao/elasticsearch.go => elastic/dao/search.go (67%) diff --git a/elastic/client.go b/elastic/client.go new file mode 100644 index 0000000..aac35e8 --- /dev/null +++ b/elastic/client.go @@ -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 +} diff --git a/dao/elasticsearch.go b/elastic/dao/search.go similarity index 67% rename from dao/elasticsearch.go rename to elastic/dao/search.go index fab46c4..a9b36d2 100644 --- a/dao/elasticsearch.go +++ b/elastic/dao/search.go @@ -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 } @@ -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(),