Skip to content

Commit d58805c

Browse files
committed
fixing pagination
1 parent 9184a1b commit d58805c

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

handlers/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (h *Handler) Paginate(paginator paginations.Pagination) (paginations.Pagina
3535
})
3636

3737
var result []interface{}
38-
adapter := adapter.NewElasticsearchAdapter(h.Context, h.Elasticsearch, paginator.Model, query)
38+
adapter := adapter.NewElasticsearchAdapter(h.Context, h.Elasticsearch, paginator.Model, paginator.UseCounter, paginator.Counter, query)
3939
paginator.Paginate(adapter)
4040
paginator.Pager.Results(&result)
4141
next := paginator.Page + 1

paginations/adapter/elasticsearch.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ type (
1414
context context.Context
1515
client *elastic.Client
1616
index string
17+
counter uint64
18+
useCounter bool
1719
pageQuery *elastic.BoolQuery
1820
totalQuery *elastic.BoolQuery
1921
}
2022
)
2123

22-
func NewElasticsearchAdapter(context context.Context, client *elastic.Client, index string, query *elastic.BoolQuery) paginator.Adapter {
24+
func NewElasticsearchAdapter(context context.Context, client *elastic.Client, index string, useCounter bool, counter uint64, query *elastic.BoolQuery) paginator.Adapter {
2325
totalQuery := elastic.NewBoolQuery()
2426
*totalQuery = *query
2527

2628
return &ElasticsearchAdapter{
2729
context: context,
2830
client: client,
2931
index: index,
32+
useCounter: useCounter,
33+
counter: counter,
3034
pageQuery: query,
3135
totalQuery: totalQuery,
3236
}
@@ -43,9 +47,12 @@ func (es *ElasticsearchAdapter) Nums() (int64, error) {
4347
}
4448

4549
func (es *ElasticsearchAdapter) Slice(offset int, length int, data interface{}) error {
46-
es.pageQuery.Must(elastic.NewRangeQuery("Counter").From(offset).To(length + offset))
50+
if es.useCounter {
51+
es.pageQuery.Must(elastic.NewRangeQuery("Counter").From(es.counter).To(es.counter + uint64(length)))
52+
offset = 0
53+
}
4754

48-
result, err := es.client.Search().Index(es.index).IgnoreUnavailable(true).Query(es.pageQuery).Size(length).Do(es.context)
55+
result, err := es.client.Search().Index(es.index).IgnoreUnavailable(true).Query(es.pageQuery).From(offset).Size(length).Do(es.context)
4956
if err != nil {
5057
log.Printf("%s", err.Error())
5158
return nil

paginations/pagination.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ type (
1414
}
1515

1616
Pagination struct {
17-
Limit int
18-
Page int
19-
Filters []Filter
20-
Search string
21-
Pager paginator.Paginator
22-
Model string
17+
Limit int
18+
Page int
19+
UseCounter bool
20+
Counter uint64
21+
Filters []Filter
22+
Search string
23+
Pager paginator.Paginator
24+
Model string
2325
}
2426

2527
PaginationMeta struct {
@@ -50,6 +52,11 @@ func (p *Pagination) Handle(pagination *grpcs.Pagination) {
5052
}
5153
}
5254

55+
if pagination.Counter > 0 {
56+
p.UseCounter = true
57+
p.Counter = pagination.Counter
58+
}
59+
5360
p.Limit = int(pagination.Limit)
5461
p.Page = int(pagination.Page)
5562
}

protos/builds/pagination.pb.go

Lines changed: 22 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protos/pagination.proto

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ message PaginationMetadata {
1515

1616
message Pagination {
1717
int32 page = 1;
18-
int32 limit = 2;
19-
repeated string fields = 3;
20-
repeated string values = 4;
18+
uint64 counter = 2;
19+
int32 limit = 3;
20+
repeated string fields = 4;
21+
repeated string values = 5;
2122
}

0 commit comments

Comments
 (0)