Skip to content

Commit

Permalink
perf: split index data by days (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
JieTrancender authored Aug 22, 2022
1 parent 97fba1f commit c4f3f7d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/nsqconsumer/outputs/elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package elasticsearch
import (
"context"
"encoding/json"
"fmt"
"net/http"
"sync"
"time"

"github.com/jehiah/go-strftime"
"github.com/marmotedu/iam/pkg/log"
"github.com/olivere/elastic/v7"

Expand Down Expand Up @@ -100,6 +102,11 @@ func (c *Client) Run(msgChan <-chan *message.Message) {
}
}

func (c *Client) indexName(topic string) string {
now := time.Now()
return strftime.Format(fmt.Sprintf("%s-%%&.%%m.%%d", topic), now)
}

func (c *Client) Publish(msgList []*message.Message) {
bulkReq := elastic.NewBulkService(c.client)
defer bulkReq.Reset()
Expand All @@ -112,7 +119,7 @@ func (c *Client) Publish(msgList []*message.Message) {
removeList = append(removeList, i)
continue
}
req := elastic.NewBulkIndexRequest().Index(m.GetTopic()).Doc(data)
req := elastic.NewBulkIndexRequest().Index(c.indexName(m.GetTopic())).Doc(data)
bulkReq = bulkReq.Add(req)
}

Expand Down

0 comments on commit c4f3f7d

Please sign in to comment.