-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
80 lines (62 loc) · 2.3 KB
/
api.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
package elasticsearch
import (
"context"
"encoding/json"
"errors"
"github.com/ONSdigital/dp-dimension-search-builder/models"
"github.com/ONSdigital/dp-elasticsearch/v2/elasticsearch"
"github.com/ONSdigital/dp-net/http"
"github.com/ONSdigital/log.go/v2/log"
)
// ErrorUnexpectedStatusCode represents the error message to be returned when
// the status received from elastic is not as expected
var ErrorUnexpectedStatusCode = errors.New("unexpected status code from api")
// API aggregates a client and URL and other common data for accessing the API
type API struct {
clienter http.Clienter
elasticSearchClient *elasticsearch.Client
}
// NewElasticSearchAPI creates an ElasticSearchAPI object
func NewElasticSearchAPI(clienter http.Clienter, elasticSearchClient *elasticsearch.Client) *API {
return &API{
clienter: clienter,
elasticSearchClient: elasticSearchClient,
}
}
// CreateSearchIndex creates a new index in elastic search
func (api *API) CreateSearchIndex(ctx context.Context, instanceID, dimension string) (int, error) {
indexName := instanceID + "_" + dimension
indexMappings := GetMappingsJSON()
status, err := api.elasticSearchClient.CreateIndex(ctx, indexName, indexMappings)
if err != nil {
return status, err
}
return status, nil
}
// DeleteSearchIndex removes an index from elastic search
func (api *API) DeleteSearchIndex(ctx context.Context, instanceID, dimension string) (int, error) {
indexName := instanceID + "_" + dimension
status, err := api.elasticSearchClient.DeleteIndex(ctx, indexName)
if err != nil {
return status, err
}
return status, nil
}
// AddDimensionOption adds a document to an elastic search index
func (api *API) AddDimensionOption(ctx context.Context, instanceID, dimension string, dimensionOption models.DimensionOption) (int, error) {
log.Info(ctx, "adding dimension option", log.Data{"dimension_option": dimensionOption})
if dimensionOption.Code == "" {
return 0, errors.New("missing dimension option code")
}
indexName := instanceID + "_" + dimension
documentID := dimensionOption.Code
document, err := json.Marshal(dimensionOption)
if err != nil {
return 0, err
}
status, err := api.elasticSearchClient.AddDocument(ctx, indexName, "_doc", documentID, document)
if err != nil {
return status, err
}
return status, nil
}