elasticstream/source/elastic/search.go

62 lines
1.3 KiB
Go

package elastic
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/elastic/go-elasticsearch/esapi"
"github.com/elastic/go-elasticsearch/v8"
)
type SearchResponse struct {
Hits struct {
Total struct {
Value int `json:"value"`
} `json:"total"`
Hits []struct {
Index string `json:"_index"`
ID string `json:"_id"`
Source map[string]interface{} `json:"_source"`
} `json:"hits"`
} `json:"hits"`
}
// search is calling Elastic Search search API
func search(client *elasticsearch.Client, index string, offset, size *int) (*SearchResponse, error) {
query := fmt.Sprintf(`{
"query": {
"match_all": {}
}
}`)
// Create the search request
req := esapi.SearchRequest{
Index: []string{index},
Body: strings.NewReader(query),
From: offset,
Size: size,
}
// Perform the request
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
res, err := req.Do(ctx, client)
if err != nil {
return nil, fmt.Errorf("error getting response: %s", err)
}
defer res.Body.Close()
if res.IsError() {
return nil, fmt.Errorf("res.IsError() error: %s", res.String())
}
result := &SearchResponse{}
if err := json.NewDecoder(res.Body).Decode(result); err != nil {
return nil, fmt.Errorf("error parsing the response body: %s", err)
}
return result, nil
}