-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(es): add elasticsearch client(v7) #95
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,62 @@ | ||
package elasticsearch | ||
|
||
import ( | ||
"crypto/tls" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
es "github.com/elastic/go-elasticsearch/v7" | ||
"github.com/elastic/go-elasticsearch/v7/esapi" | ||
) | ||
|
||
var ( | ||
ErrEsNotFound = errors.New("es: not found") | ||
) | ||
|
||
type EsClient struct { | ||
client *es.Client | ||
} | ||
|
||
func NewESClient(hosts []string, username, password string) (client *EsClient, err error) { | ||
c, err := es.NewClient(es.Config{ | ||
Addresses: hosts, | ||
Username: username, | ||
Password: password, | ||
Transport: &http.Transport{ | ||
MaxIdleConnsPerHost: http.DefaultMaxIdleConnsPerHost, | ||
ResponseHeaderTimeout: 5 * time.Second, | ||
DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext, | ||
TLSClientConfig: &tls.Config{ | ||
MinVersion: tls.VersionTLS11, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return &EsClient{client: c}, nil | ||
} | ||
|
||
func (es *EsClient) handleResponse(resp *esapi.Response) (map[string]interface{}, error) { | ||
var ( | ||
r map[string]interface{} | ||
) | ||
if resp.StatusCode == 404 { | ||
return r, ErrEsNotFound | ||
} | ||
// Check response status | ||
if resp.IsError() { | ||
return nil, errors.New(resp.String()) | ||
} | ||
// Deserialize the response into a map. | ||
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil { | ||
return nil, errors.New(fmt.Sprintf("Error parsing the response body: %s", err)) | ||
} | ||
return r, nil | ||
} |
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,191 @@ | ||
package elasticsearch | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
esClient *EsClient | ||
ctx context.Context | ||
) | ||
|
||
const ( | ||
indexName = "test-index" | ||
) | ||
|
||
func init() { | ||
var err error | ||
esClient, err = NewESClient([]string{"http://127.0.0.1:9200"}, "", "") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ctx = context.Background() | ||
} | ||
|
||
func TestNewESClient(t *testing.T) { | ||
t.Log(esClient.client.Info()) | ||
} | ||
|
||
func TestNewESClientWithContext(t *testing.T) { | ||
t.Log(esClient.client.Info(esClient.client.Info.WithContext(context.Background()))) | ||
} | ||
|
||
func TestCreateIndex(t *testing.T) { | ||
a := assert.New(t) | ||
bodyStr := `{ | ||
"mappings" : { | ||
"properties" : { | ||
"id" : { | ||
"index": true, | ||
"type" : "keyword" | ||
}, | ||
"age" : { | ||
"index": true, | ||
"type" : "keyword" | ||
}, | ||
"nickname" : { | ||
"index": true, | ||
"type" : "text", | ||
"fields": { | ||
"keyword": { | ||
"type": "keyword", | ||
"ignore_above": 256 | ||
} | ||
} | ||
}, | ||
"bio" : { | ||
"index": true, | ||
"type" : "keyword" | ||
} | ||
} | ||
} | ||
}` | ||
resp, err := esClient.CreateIndex(ctx, indexName, bodyStr) | ||
a.Nil(err) | ||
t.Log(resp) | ||
|
||
response, err := esClient.client.Indices.Get([]string{indexName}) | ||
a.Nil(err) | ||
t.Log(response) | ||
} | ||
|
||
func TestGetIndex(t *testing.T) { | ||
a := assert.New(t) | ||
response, err := esClient.GetIndex(ctx, indexName) | ||
a.Nil(err) | ||
t.Log(response) | ||
} | ||
|
||
func TestDeleteIndex(t *testing.T) { | ||
a := assert.New(t) | ||
err := esClient.DeleteIndex(ctx, indexName) | ||
a.Nil(err) | ||
} | ||
|
||
func TestCreateDocument(t *testing.T) { | ||
a := assert.New(t) | ||
doc := map[string]interface{}{ | ||
"id": "1", | ||
"nickname": "Tom", | ||
"age": "18", | ||
"bio": "I am a singer", | ||
} | ||
response, err := esClient.CreateDocument(ctx, indexName, "1", doc) | ||
a.Nil(err) | ||
t.Log(response) | ||
} | ||
|
||
func TestGetDocument(t *testing.T) { | ||
a := assert.New(t) | ||
response, err := esClient.GetDocument(ctx, indexName, "1") | ||
a.Nil(err) | ||
t.Log(response) | ||
} | ||
|
||
func TestUpdateDocument(t *testing.T) { | ||
a := assert.New(t) | ||
data := map[string]interface{}{ | ||
"bio": "I am a teacher", | ||
} | ||
response, err := esClient.UpdateDocument(ctx, indexName, "1", data) | ||
a.Nil(err) | ||
t.Log(response) | ||
} | ||
|
||
func TestUpdateDocumentByQuery(t *testing.T) { | ||
a := assert.New(t) | ||
query := map[string]interface{}{ | ||
"nickname": "Tom", | ||
} | ||
data := map[string]interface{}{ | ||
"bio": "I am a music teacher", | ||
} | ||
response, err := esClient.UpdateDocumentByQuery(ctx, indexName, query, data) | ||
a.Nil(err) | ||
t.Log(response) | ||
} | ||
|
||
func TestDeleteDocument(t *testing.T) { | ||
a := assert.New(t) | ||
err := esClient.DeleteDocument(ctx, indexName, "1") | ||
a.Nil(err) | ||
} | ||
|
||
// 单个字段的搜索 | ||
func TestSearch(t *testing.T) { | ||
a := assert.New(t) | ||
query := map[string]interface{}{ | ||
"bool": map[string]interface{}{ | ||
"must": []map[string]interface{}{ | ||
{ | ||
"match": map[string]interface{}{ | ||
"nickname": "Tom", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
sort := map[string]interface{}{ | ||
"_score": map[string]interface{}{ | ||
"order": "desc", | ||
}, | ||
} | ||
response, total, err := esClient.Search(ctx, indexName, query, sort, 0, 10) | ||
a.Nil(err) | ||
t.Log(total) | ||
t.Log(response) | ||
} | ||
|
||
// 多字段搜索 | ||
func TestSearchByMultiField(t *testing.T) { | ||
a := assert.New(t) | ||
query := map[string]interface{}{ | ||
"bool": map[string]interface{}{ | ||
"must": []map[string]interface{}{ | ||
{ | ||
"match": map[string]interface{}{ | ||
"nickname": "Tom", | ||
}, | ||
}, | ||
{ | ||
"match": map[string]interface{}{ | ||
"age": "18", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
sort := map[string]interface{}{ | ||
"_score": map[string]interface{}{ | ||
"order": "desc", | ||
}, | ||
} | ||
response, total, err := esClient.Search(ctx, indexName, query, sort, 0, 10) | ||
a.Nil(err) | ||
t.Log(total) | ||
t.Log(response) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Insecure TLS configuration