feat: add support for search after

This commit is contained in:
Hardy 2021-03-02 11:51:41 +08:00
parent 8661572bd5
commit 09acfd6a3d
3 changed files with 27 additions and 15 deletions

4
go.sum
View File

@ -1,9 +1,5 @@
github.com/elastic/go-elasticsearch v0.0.0 h1:Pd5fqOuBxKxv83b0+xOAJDAkziWYwFinWnBO0y+TZaA=
github.com/elastic/go-elasticsearch v0.0.0/go.mod h1:TkBSJBuTyFdBnrNqoPc54FN0vKf5c04IdM4zuStJ7xg=
github.com/elastic/go-elasticsearch/v7 v7.6.0 h1:sYpGLpEFHgLUKLsZUBfuaVI9QgHjS3JdH9fX4/z8QI8= github.com/elastic/go-elasticsearch/v7 v7.6.0 h1:sYpGLpEFHgLUKLsZUBfuaVI9QgHjS3JdH9fX4/z8QI8=
github.com/elastic/go-elasticsearch/v7 v7.6.0/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4= github.com/elastic/go-elasticsearch/v7 v7.6.0/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
github.com/elastic/go-elasticsearch/v8 v8.0.0-20200210103600-aff00e5adfde h1:Y9SZx8RQqFycLxi5W5eFmxMqnmijULVc3LMjBTtZQdM=
github.com/elastic/go-elasticsearch/v8 v8.0.0-20200210103600-aff00e5adfde/go.mod h1:xe9a/L2aeOgFKKgrO3ibQTnMdpAeL0GC+5/HpGScSa4=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/jgroeneveld/schema v1.0.0 h1:J0E10CrOkiSEsw6dfb1IfrDJD14pf6QLVJ3tRPl/syI= github.com/jgroeneveld/schema v1.0.0 h1:J0E10CrOkiSEsw6dfb1IfrDJD14pf6QLVJ3tRPl/syI=

View File

@ -15,16 +15,17 @@ import (
// Not all features of the search API are currently supported, but a request can // Not all features of the search API are currently supported, but a request can
// currently include a query, aggregations, and more. // currently include a query, aggregations, and more.
type SearchRequest struct { type SearchRequest struct {
aggs []Aggregation aggs []Aggregation
explain *bool explain *bool
from *uint64 from *uint64
highlight Mappable highlight Mappable
postFilter Mappable searchAfter []string
query Mappable postFilter Mappable
size *uint64 query Mappable
sort Sort size *uint64
source Source sort Sort
timeout *time.Duration source Source
timeout *time.Duration
} }
// Search creates a new SearchRequest object, to be filled via method chaining. // Search creates a new SearchRequest object, to be filled via method chaining.
@ -74,6 +75,12 @@ func (req *SearchRequest) Sort(name string, order Order) *SearchRequest {
return req return req
} }
// SearchAfter retrieve the sorted result
func (req *SearchRequest) SearchAfter(s ...string) *SearchRequest {
req.searchAfter = append(req.searchAfter, s...)
return req
}
// Explain sets whether the ElasticSearch API should return an explanation for // Explain sets whether the ElasticSearch API should return an explanation for
// how each hit's score was calculated. // how each hit's score was calculated.
func (req *SearchRequest) Explain(b bool) *SearchRequest { func (req *SearchRequest) Explain(b bool) *SearchRequest {
@ -105,7 +112,6 @@ func (req *SearchRequest) Highlight(highlight Mappable) *SearchRequest {
return req return req
} }
// Map implements the Mappable interface. It converts the request to into a // Map implements the Mappable interface. It converts the request to into a
// nested map[string]interface{}, as expected by the go-elasticsearch library. // nested map[string]interface{}, as expected by the go-elasticsearch library.
func (req *SearchRequest) Map() map[string]interface{} { func (req *SearchRequest) Map() map[string]interface{} {
@ -142,6 +148,9 @@ func (req *SearchRequest) Map() map[string]interface{} {
if req.highlight != nil { if req.highlight != nil {
m["highlight"] = req.highlight.Map() m["highlight"] = req.highlight.Map()
} }
if req.searchAfter != nil {
m["search_after"] = req.searchAfter
}
source := req.source.Map() source := req.source.Map()
if len(source) > 0 { if len(source) > 0 {

View File

@ -7,6 +7,13 @@ import (
func TestSearchMaps(t *testing.T) { func TestSearchMaps(t *testing.T) {
runMapTests(t, []mapTest{ runMapTests(t, []mapTest{
{
"a simple query with search after",
Search().SearchAfter("_id", "name"),
map[string]interface{}{
"search_after": []string{"_id", "name"},
},
},
{ {
"a simple match_all query with a size and no aggs", "a simple match_all query with a size and no aggs",
Search().Query(MatchAll()).Size(20), Search().Query(MatchAll()).Size(20),