-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
548 additions
and
1 deletion.
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
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.