Skip to content

Commit

Permalink
Merge pull request #5 from maximelamure/latestVersionSupport
Browse files Browse the repository at this point in the history
Fix backward compat issues
  • Loading branch information
maximelamure authored Jun 30, 2022
2 parents 0596795 + 5f2e646 commit 208f2a2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
35 changes: 17 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Client interface {
InsertDocument(indexName, documentType, identifier string, data []byte) (*InsertDocument, error)
Document(indexName, documentType, identifier string) (*Document, error)
DeleteDocument(indexName, documentType, identifier string) (*Document, error)
Bulk(data []byte) (*Bulk, error)
Bulk(indexName string, data []byte) (*Bulk, error)
Search(indexName, documentType, data string, explain bool) (*SearchResult, error)
MSearch(queries []MSearchQuery) (*MSearchResult, error)
Suggest(indexName, data string) ([]byte, error)
Expand Down Expand Up @@ -56,11 +56,11 @@ func NewClientFromUrl(rawurl string) Client {
}

// CreateIndex instantiates an index
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
func (c *client) CreateIndex(indexName, mapping string) (*Response, error) {
url := c.Host.String() + "/" + indexName
reader := bytes.NewBufferString(mapping)
response, err := sendHTTPRequest("POST", url, reader)
response, err := sendHTTPRequest("PUT", url, reader)
if err != nil {
return &Response{}, err
}
Expand All @@ -75,7 +75,7 @@ func (c *client) CreateIndex(indexName, mapping string) (*Response, error) {
}

// DeleteIndex deletes an existing index.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
func (c *client) DeleteIndex(indexName string) (*Response, error) {
url := c.Host.String() + "/" + indexName
response, err := sendHTTPRequest("DELETE", url, nil)
Expand All @@ -93,7 +93,7 @@ func (c *client) DeleteIndex(indexName string) (*Response, error) {
}

// UpdateIndexSetting changes specific index level settings in real time
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
func (c *client) UpdateIndexSetting(indexName, mapping string) (*Response, error) {
url := c.Host.String() + "/" + indexName + "/_settings"
reader := bytes.NewBufferString(mapping)
Expand All @@ -112,7 +112,7 @@ func (c *client) UpdateIndexSetting(indexName, mapping string) (*Response, error
}

// IndexSettings allows to retrieve settings of index
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-settings.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html
func (c *client) IndexSettings(indexName string) (Settings, error) {
url := c.Host.String() + "/" + indexName + "/_settings"
response, err := sendHTTPRequest("GET", url, nil)
Expand All @@ -132,7 +132,7 @@ func (c *client) IndexSettings(indexName string) (Settings, error) {
}

// IndexExists allows to check if the index exists or not.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-exists.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html
func (c *client) IndexExists(indexName string) (bool, error) {
url := c.Host.String() + "/" + indexName
httpClient := &http.Client{}
Expand Down Expand Up @@ -162,9 +162,9 @@ func (c *client) Status(indices string) (*Settings, error) {
}

// InsertDocument adds or updates a typed JSON document in a specific index, making it searchable
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
func (c *client) InsertDocument(indexName, documentType, identifier string, data []byte) (*InsertDocument, error) {
url := c.Host.String() + "/" + indexName + "/" + documentType + "/" + identifier
url := c.Host.String() + "/" + indexName + "/_doc/" + identifier
reader := bytes.NewBuffer(data)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
Expand Down Expand Up @@ -219,8 +219,8 @@ func (c *client) DeleteDocument(indexName, documentType, identifier string) (*Do
// Bulk makes it possible to perform many index/delete operations in a single API call.
// This can greatly increase the indexing speed.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html
func (c *client) Bulk(data []byte) (*Bulk, error) {
url := c.Host.String() + "/_bulk"
func (c *client) Bulk(indexName string, data []byte) (*Bulk, error) {
url := c.Host.String() + "/" + indexName + "/_bulk"
reader := bytes.NewBuffer(data)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
Expand All @@ -239,11 +239,8 @@ func (c *client) Bulk(data []byte) (*Bulk, error) {
// Search allows to execute a search query and get back search hits that match the query
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
func (c *client) Search(indexName, documentType, data string, explain bool) (*SearchResult, error) {
if len(documentType) > 0 {
documentType = documentType + "/"
}

url := c.Host.String() + "/" + indexName + "/" + documentType + "/_search"
url := c.Host.String() + "/" + indexName + "/_search"
if explain {
url += "?explain"
}
Expand Down Expand Up @@ -366,9 +363,11 @@ func sendHTTPRequest(method, url string, body io.Reader) ([]byte, error) {
return nil, err
}

if method == "POST" || method == "PUT" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
// if method == "POST" || method == "PUT" {
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// }

req.Header.Set("Content-Type", "application/json")

newReq, err := client.Do(req)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestSearch(t *testing.T) {
buffer.WriteByte('\n')
}

_, err := client.Bulk(buffer.Bytes())
_, err := client.Bulk(IndexName, buffer.Bytes())
helper.OK(t, err)

//We have to wait after a bulk
Expand All @@ -163,7 +163,7 @@ func TestSearch(t *testing.T) {
//Search
search, err := client.Search(IndexName, ProductDocumentType, SearchByColorQuery("red"), false)
helper.OK(t, err)
helper.Assert(t, search.Hits.Total == 2, "The search doesn't return all matched items")
helper.Assert(t, search.Hits.Total.Value == 2, "The search doesn't return all matched items")

//MSearch

Expand All @@ -173,8 +173,8 @@ func TestSearch(t *testing.T) {

msresult, err := client.MSearch(mqueries)
helper.OK(t, err)
helper.Assert(t, msresult.Responses[0].Hits.Total == 1, "The msearch doesn't return all matched items")
helper.Assert(t, msresult.Responses[1].Hits.Total == 2, "The msearch doesn't return all matched items")
helper.Assert(t, msresult.Responses[0].Hits.Total.Value == 1, "The msearch doesn't return all matched items")
helper.Assert(t, msresult.Responses[1].Hits.Total.Value == 2, "The msearch doesn't return all matched items")

//Delete the index
deleteResponse, err := client.DeleteIndex(IndexName)
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/maximelamure/elasticsearch

go 1.18
14 changes: 12 additions & 2 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ type Bulk struct {
ID string `json:"_id"`
Version int `json:"_version"`
Status int `json:"status"`
Error string `json:"error"`
Error struct {
Type string `json:"status"`
Reason string `json:"reason"`
Index_UUID string `json:"index_uuid"`
Shard string `json:"shard"`
Index string `json:"index"`
} `json:"error"`
} `json:"index"`
} `json:"items"`
}
Expand All @@ -79,6 +85,7 @@ type SearchResult struct {
Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Skipped int `json:"skipped"`
Failed int `json:"failed"`
} `json:"_shards"`
Hits ResultHits `json:"hits"`
Expand All @@ -87,7 +94,10 @@ type SearchResult struct {

// ResultHits represents the result of the search hits
type ResultHits struct {
Total int `json:"total"`
Total struct {
Value int `json:"value"`
Relation string `json:"relation"`
} `json:"total"`
MaxScore float32 `json:"max_score"`
Hits []struct {
Index string `json:"_index"`
Expand Down

0 comments on commit 208f2a2

Please sign in to comment.