-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement using the official Elasticsearch library.
Create a new implementation using the official Elasticsearch library in a separate package, without impacting those who use the Olivere implementation. Refine the abstraction of Elasticsearch Search functionality even more, enabling the use of this library only.
- Loading branch information
Showing
29 changed files
with
2,713 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
elasticutil "github.com/arquivei/elasticutil/official/v7" | ||
"github.com/arquivei/elasticutil/official/v7/querybuilders" | ||
) | ||
|
||
type ExampleFilterMust struct { | ||
Names []string `es:"Name"` | ||
SocialNames []string `es:"SocialName"` | ||
Ages []uint64 `es:"Age"` | ||
HasCovid *bool | ||
CreatedAt *elasticutil.TimeRange | ||
AgeRange *elasticutil.IntRange `es:"Age"` | ||
CovidInfo elasticutil.Nested `es:"Covid"` | ||
NameOrSocialName elasticutil.FullTextSearchShould `es:"Name,SocialName"` | ||
MyCustomSearch elasticutil.CustomSearch | ||
} | ||
|
||
type ExampleFilterExists struct { | ||
HasCovidInfo elasticutil.Nested `es:"Covid"` | ||
HasAge *bool `es:"Age"` | ||
} | ||
|
||
type ExampleCovidInfo struct { | ||
HasCovidInfo *bool `es:"Covid"` | ||
Symptoms []string `es:"Covid.Symptom"` | ||
FirstSymptomDate *elasticutil.TimeRange `es:"Covid.Date"` | ||
} | ||
|
||
func createFilter() elasticutil.Filter { | ||
return elasticutil.Filter{ | ||
Must: ExampleFilterMust{ | ||
Names: []string{"John", "Mary"}, | ||
Ages: []uint64{16, 17, 18, 25, 26}, | ||
HasCovid: refBool(true), | ||
CovidInfo: elasticutil.NewNested( | ||
ExampleCovidInfo{ | ||
Symptoms: []string{"cough"}, | ||
FirstSymptomDate: &elasticutil.TimeRange{ | ||
From: time.Date(2019, time.November, 28, 15, 27, 39, 49, time.UTC), | ||
To: time.Date(2020, time.November, 28, 15, 27, 39, 49, time.UTC), | ||
}, | ||
}, | ||
), | ||
CreatedAt: &elasticutil.TimeRange{ | ||
From: time.Date(2020, time.November, 28, 15, 27, 39, 49, time.UTC), | ||
To: time.Date(2021, time.November, 28, 15, 27, 39, 49, time.UTC), | ||
}, | ||
AgeRange: &elasticutil.IntRange{ | ||
From: 15, | ||
To: 30, | ||
}, | ||
NameOrSocialName: elasticutil.NewFullTextSearchShould([]string{"John", "Mary", "Rebecca"}), | ||
MyCustomSearch: elasticutil.NewCustomSearch(func() (querybuilders.Query, error) { | ||
return querybuilders.NewBoolQuery().Must(querybuilders.NewTermQuery("Name", "John")), nil | ||
}), | ||
}, | ||
MustNot: ExampleFilterMust{ | ||
Names: []string{"Lary"}, | ||
AgeRange: &elasticutil.IntRange{ | ||
From: 29, | ||
To: 30, | ||
}, | ||
}, | ||
Exists: ExampleFilterExists{ | ||
HasCovidInfo: elasticutil.NewNested( | ||
ExampleCovidInfo{ | ||
HasCovidInfo: refBool(true), | ||
}, | ||
), | ||
HasAge: refBool(true), | ||
}, | ||
} | ||
} | ||
|
||
func refBool(b bool) *bool { | ||
return &b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
elasticutil "github.com/arquivei/elasticutil/official/v7" | ||
) | ||
|
||
func main() { | ||
client := elasticutil.MustNewClient("") | ||
|
||
response, err := client.Search( | ||
context.Background(), | ||
elasticutil.SearchConfig{ | ||
Filter: createFilter(), | ||
Indexes: []string{""}, | ||
Size: 5, | ||
IgnoreUnavailable: true, | ||
AllowNoIndices: true, | ||
TrackTotalHits: true, | ||
Sort: elasticutil.Sorters{ | ||
Sorters: []elasticutil.Sorter{ | ||
{ | ||
Field: "ID", | ||
Ascending: true, | ||
}, | ||
}, | ||
}, | ||
SearchAfter: "", | ||
}, | ||
) | ||
|
||
fmt.Println(response, err) | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package v7 | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/arquivei/elasticutil/official/v7/internal/retrier" | ||
es "github.com/elastic/go-elasticsearch/v7" | ||
) | ||
|
||
// Client represents the Elasticsearch's client of the official lib. | ||
type Client interface { | ||
Search(context.Context, SearchConfig) (SearchResponse, error) | ||
} | ||
|
||
type esClient struct { | ||
client *es.Client | ||
} | ||
|
||
// NewClient returns a new Client using the @urls. | ||
func NewClient(urls ...string) (Client, error) { | ||
client, err := es.NewClient(es.Config{ | ||
Addresses: urls, | ||
RetryBackoff: retrier.NewSimpleBackoff(10, 100), | ||
Transport: &http.Transport{ | ||
DisableCompression: false, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &esClient{ | ||
client: client, | ||
}, nil | ||
} | ||
|
||
// MustNewClient returns a new Client using the @urls. It panics | ||
// instead of returning an error. | ||
func MustNewClient(urls ...string) Client { | ||
client, err := NewClient(urls...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package v7 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type mockClient struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *mockClient) Search(_ context.Context, sc SearchConfig) (SearchResponse, error) { | ||
args := m.Called(sc) | ||
return args.Get(0).(SearchResponse), args.Error(1) | ||
} | ||
|
||
// MustNewClientMock returns a mocked Client that uses Search method and returns @expectedResponse | ||
// and @expectedError for the giving @input. | ||
func MustNewClientMockSearch(input SearchConfig, expectedResponse SearchResponse, expectedError error) Client { | ||
m := mockClient{} | ||
m.On("Search", input).Return(expectedResponse, expectedError) | ||
return &m | ||
} |
Oops, something went wrong.