Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit f417ef3

Browse files
authored
Virtual table persistance - use standard elastic HTTP client (#903)
1 parent f53610f commit f417ef3

File tree

1 file changed

+10
-48
lines changed

1 file changed

+10
-48
lines changed

quesma/persistence/elastic.go

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package persistence
44

55
import (
6-
"bytes"
6+
"context"
77
"encoding/json"
88
"fmt"
99
"io"
@@ -15,12 +15,8 @@ import (
1515
)
1616

1717
type ElasticJSONDatabase struct {
18-
url string
19-
indexName string
20-
user string
21-
password string
22-
23-
httpClient *http.Client
18+
indexName string
19+
httpClient *elasticsearch.SimpleClient
2420
}
2521

2622
// This is a wrapper to make document a single field doc.
@@ -32,20 +28,15 @@ type Wrapper struct {
3228

3329
func NewElasticJSONDatabase(cfg config.ElasticsearchConfiguration, indexName string) *ElasticJSONDatabase {
3430

35-
httpClient := &http.Client{}
36-
3731
return &ElasticJSONDatabase{
38-
httpClient: httpClient,
39-
user: cfg.User,
40-
password: cfg.Password,
41-
url: cfg.Url.String(),
32+
httpClient: elasticsearch.NewSimpleClient(&cfg),
4233
indexName: indexName,
4334
}
4435
}
4536

4637
func (p *ElasticJSONDatabase) Put(key string, data string) error {
4738

48-
elasticsearchURL := fmt.Sprintf("%s/%s/_update/%s", p.url, p.indexName, key)
39+
elasticsearchURL := fmt.Sprintf("%s/_update/%s", p.indexName, key)
4940

5041
w := Wrapper{Content: data}
5142

@@ -58,14 +49,7 @@ func (p *ElasticJSONDatabase) Put(key string, data string) error {
5849
return err
5950
}
6051

61-
req, err := http.NewRequest("POST", elasticsearchURL, bytes.NewBuffer(jsonData))
62-
if err != nil {
63-
return err
64-
}
65-
66-
p.setupRequest(req)
67-
68-
resp, err := p.httpClient.Do(req)
52+
resp, err := p.httpClient.Request(context.Background(), "POST", elasticsearchURL, jsonData)
6953
if err != nil {
7054
return err
7155
}
@@ -85,23 +69,10 @@ func (p *ElasticJSONDatabase) Put(key string, data string) error {
8569
}
8670
}
8771

88-
func (p *ElasticJSONDatabase) setupRequest(req *http.Request) {
89-
elasticsearch.AddBasicAuthIfNeeded(req, p.user, p.password)
90-
req.Header.Set("Content-Type", "application/json")
91-
}
92-
9372
func (p *ElasticJSONDatabase) Get(key string) (string, bool, error) {
94-
url := fmt.Sprintf("%s/%s/_source/%s", p.url, p.indexName, key)
95-
96-
req, err := http.NewRequest("GET", url, nil)
97-
if err != nil {
98-
return "", false, err
99-
}
100-
101-
p.setupRequest(req)
102-
103-
resp, err := p.httpClient.Do(req)
73+
url := fmt.Sprintf("%s/_source/%s", p.indexName, key)
10474

75+
resp, err := p.httpClient.Request(context.Background(), "GET", url, nil)
10576
if err != nil {
10677
return "", false, err
10778
}
@@ -135,7 +106,7 @@ func (p *ElasticJSONDatabase) Get(key string) (string, bool, error) {
135106
func (p *ElasticJSONDatabase) List() ([]string, error) {
136107

137108
// Define the Elasticsearch endpoint and the index you want to query
138-
elasticsearchURL := fmt.Sprintf("%s/%s/_search", p.url, p.indexName)
109+
elasticsearchURL := fmt.Sprintf("%s/_search", p.indexName)
139110

140111
// Build the query to get only document IDs
141112
query := `{
@@ -146,17 +117,8 @@ func (p *ElasticJSONDatabase) List() ([]string, error) {
146117
}
147118
}`
148119

149-
// Create a new HTTP request
150-
req, err := http.NewRequest("GET", elasticsearchURL, bytes.NewBuffer([]byte(query)))
151-
if err != nil {
152-
log.Fatalf("Error creating HTTP request: %s", err)
153-
}
154-
155-
p.setupRequest(req)
120+
resp, err := p.httpClient.Request(context.Background(), "GET", elasticsearchURL, []byte(query))
156121

157-
// Use the default HTTP client to execute the request
158-
client := p.httpClient
159-
resp, err := client.Do(req)
160122
if err != nil {
161123
return nil, err
162124
}

0 commit comments

Comments
 (0)