2024-05-20 16:11:50 +08:00
|
|
|
package es7
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-12-13 15:01:40 +08:00
|
|
|
"time"
|
|
|
|
|
2024-05-20 16:11:50 +08:00
|
|
|
elastic "github.com/elastic/go-elasticsearch/v7"
|
|
|
|
"github.com/elastic/go-elasticsearch/v7/esapi"
|
2024-12-13 15:01:40 +08:00
|
|
|
"github.com/loveuer/esgo2dump/internal/tool"
|
2025-02-05 18:07:53 +08:00
|
|
|
"github.com/loveuer/esgo2dump/pkg/model"
|
2024-06-21 15:52:34 +08:00
|
|
|
"github.com/samber/lo"
|
2024-05-20 16:11:50 +08:00
|
|
|
)
|
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
type streamer struct {
|
|
|
|
ctx context.Context
|
|
|
|
client *elastic.Client
|
|
|
|
index string
|
|
|
|
scroll string
|
2024-05-20 16:11:50 +08:00
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
// ReadData implements model.IO.
|
|
|
|
func (s *streamer) ReadData(limit int, query map[string]any, fields []string, sort []string) ([]map[string]any, error) {
|
2024-07-15 14:07:43 +08:00
|
|
|
var (
|
2025-02-05 18:07:53 +08:00
|
|
|
err error
|
|
|
|
qs []func(*esapi.SearchRequest)
|
|
|
|
resp *esapi.Response
|
|
|
|
result = new(model.ESResponseV7[map[string]any])
|
2024-07-15 14:07:43 +08:00
|
|
|
)
|
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if s.scroll != "" {
|
|
|
|
if resp, err = s.client.Scroll(
|
|
|
|
s.client.Scroll.WithContext(tool.TimeoutCtx(s.ctx)),
|
|
|
|
s.client.Scroll.WithScrollID(s.scroll),
|
|
|
|
s.client.Scroll.WithScroll(35*time.Second),
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
2024-07-15 14:07:43 +08:00
|
|
|
}
|
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
goto HandleResp
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
qs = []func(*esapi.SearchRequest){
|
|
|
|
s.client.Search.WithContext(tool.TimeoutCtx(s.ctx)),
|
|
|
|
s.client.Search.WithIndex(s.index),
|
|
|
|
s.client.Search.WithSize(limit),
|
|
|
|
s.client.Search.WithScroll(35 * time.Second),
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if len(fields) > 0 {
|
|
|
|
qs = append(qs, s.client.Search.WithSourceIncludes(fields...))
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if len(sort) > 0 {
|
|
|
|
qs = append(qs, s.client.Search.WithSort(sort...))
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if len(query) > 0 {
|
|
|
|
queryBs, err := json.Marshal(map[string]any{"query": query})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
qs = append(qs, s.client.Search.WithBody(bytes.NewReader(queryBs)))
|
|
|
|
}
|
2024-12-13 15:01:40 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if resp, err = s.client.Search(qs...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
HandleResp:
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("resp status=%d, resp=%s", resp.StatusCode, resp.String())
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
s.scroll = result.ScrollId
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
return lo.Map(
|
|
|
|
result.Hits.Hits,
|
|
|
|
func(item *model.ESSource[map[string]any], _ int) map[string]any {
|
|
|
|
return item.Content
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
// WriteData implements model.IO.
|
|
|
|
func (s *streamer) WriteData([]map[string]any) (int, error) {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
2024-07-15 14:07:43 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
func NewStreamer(ctx context.Context, client *elastic.Client, index string) (model.IO[map[string]any], error) {
|
|
|
|
s := &streamer{ctx: ctx, client: client, index: index}
|
|
|
|
return s, nil
|
2024-07-15 14:07:43 +08:00
|
|
|
}
|